Para conectar a Oracle, Postgres, Mysql y SQL Server es necesaria unicamente una clase de Conexiòn y Una clase ObjetoConexiòn donde definir los valores de la Conexion.
Tener en cuenta la estructura de paquetes, para no tener errror en el momento de ejecutar.
Clase Conexion:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.develtrex.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author 007M
*/
public class Conexion {
private Connection cnx;
private String ipHost;
private String puerto;
private String baseDatos;
private String usuario;
private String contrasena;
private String cadenaConexion;
private String gestorBD;
private String driver;
public Conexion(
String paramIpHost,
String paramPuerto,
String paramBaseDatos,
String paramUsuario,
String paramContrasena,
String paramGestorBD) {
this.ipHost = paramIpHost;
this.puerto = paramPuerto;
this.baseDatos = paramBaseDatos;
this.usuario = paramUsuario;
this.contrasena = paramContrasena;
this.gestorBD = paramGestorBD;
if (gestorBD.equalsIgnoreCase("ORACLE")) {
this.cadenaConexion = "jdbc:oracle:thin:@" + ipHost + ":" + puerto + ":" + baseDatos;
this.driver = "oracle.jdbc.OracleDriver";
} else if (gestorBD.equalsIgnoreCase("MYSQL")) {
this.cadenaConexion = "jdbc:mysql://" + ipHost + ":" + puerto + "/" + baseDatos;
this.driver = "com.mysql.jdbc.Driver";
} else if(gestorBD.equalsIgnoreCase("POSTGRES")){
this.cadenaConexion = "jdbc:postgresql://" + ipHost + ":" + puerto + "/" + baseDatos;
this.driver = "org.postgresql.Driver";
}else if(this.gestorBD.equalsIgnoreCase("SQLSERVER")){
this.cadenaConexion = "jdbc:sqlserver://" + ipHost + ":" + puerto + ";DatabaseName=" + baseDatos;
this.driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}else {
System.err.println("No existe implementacion para ese gestor");
}
}
public boolean conectarBD() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
try {
Class.forName(this.driver).newInstance();
cnx = DriverManager.getConnection(cadenaConexion, usuario, contrasena);
cnx.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
cnx.setAutoCommit(false);
return true;
} catch (ClassNotFoundException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
} catch (InstantiationException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
} catch (IllegalAccessException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public boolean desconectarBD() throws SQLException {
try {
cnx.close();
return true;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public boolean confirmar() throws SQLException {
try {
cnx.commit();
return true;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public boolean deshacer() throws SQLException {
try {
cnx.rollback();
return true;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public Connection getCnx() {
return cnx;
}
public void setCnx(Connection cnx) {
this.cnx = cnx;
}
public String getIpHost() {
return ipHost;
}
public void setIpHost(String ipHost) {
this.ipHost = ipHost;
}
public String getPuerto() {
return puerto;
}
public void setPuerto(String puerto) {
this.puerto = puerto;
}
public String getBaseDatos() {
return baseDatos;
}
public void setBaseDatos(String baseDatos) {
this.baseDatos = baseDatos;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getContrasena() {
return contrasena;
}
public void setContrasena(String contrasena) {
this.contrasena = contrasena;
}
public String getCadenaConexion() {
return cadenaConexion;
}
public void setCadenaConexion(String cadenaConexion) {
this.cadenaConexion = cadenaConexion;
}
public String getGestorBD() {
return gestorBD;
}
public void setGestorBD(String gestorBD) {
this.gestorBD = gestorBD;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
}
Clase: ObjetoConexion:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.develtrex.dao;
import java.sql.SQLException;
/**
*
* @author 007M
*/
public class ObjetoConexion {
private Conexion oracle;
private Conexion mysql;
private Conexion postgres;
private Conexion sqlServer;
public boolean conectarOracle() throws ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, SQLException{
oracle=new Conexion(
"192.168.1.124",
"1521",
"orcl",
"sysferronor",
"sysferronor",
"ORACLE"
);
return oracle.conectarBD();
}
public boolean conectarMysql() throws ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, SQLException{
mysql=new Conexion(
"192.168.1.124",
"3306",
"celltower",
"root",
"toor",
"MYSQL"
);
return mysql.conectarBD();
}
public boolean conectarPostgres() throws ClassNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
postgres=new Conexion(
"192.168.1.124",
"5432",
"tumi",
"postgres",
"toor",
"POSTGRES"
);
return postgres.conectarBD();
}
public boolean conectarSQLServer() throws ClassNotFoundException, ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, IllegalAccessException, SQLException{
sqlServer=new Conexion(
"192.168.1.70",
"1433",
"appdevel",
"sa",
"",
"SQLSERVER"
);
return sqlServer.conectarBD();
}
public Conexion getOracle() {
return oracle;
}
public void setOracle(Conexion oracle) {
this.oracle = oracle;
}
public Conexion getMysql() {
return mysql;
}
public void setMysql(Conexion mysql) {
this.mysql = mysql;
}
public Conexion getPostgres() {
return postgres;
}
public void setPostgres(Conexion postgres) {
this.postgres = postgres;
}
}
Clase Principal o de Ejecuciòn:
package com.develtrex.tester;
import com.develtrex.dao.ObjetoConexion;
import java.sql.SQLException;
/**
*
* @author 007M
*/
public class Main {
public static void main(String arg[]) throws ClassNotFoundException, ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, SQLException {
ObjetoConexion objCnx=new ObjetoConexion();
System.out.println("Postgres: "+objCnx.conectarPostgres());
System.out.println("Mysql: "+objCnx.conectarMysql());
System.out.println("Oracle: "+objCnx.conectarOracle());
System.out.println("SqlServer: "+objCnx.conectarSQLServer());
}
}
Tener en cuenta la estructura de paquetes, para no tener errror en el momento de ejecutar.
Clase Conexion:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.develtrex.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author 007M
*/
public class Conexion {
private Connection cnx;
private String ipHost;
private String puerto;
private String baseDatos;
private String usuario;
private String contrasena;
private String cadenaConexion;
private String gestorBD;
private String driver;
public Conexion(
String paramIpHost,
String paramPuerto,
String paramBaseDatos,
String paramUsuario,
String paramContrasena,
String paramGestorBD) {
this.ipHost = paramIpHost;
this.puerto = paramPuerto;
this.baseDatos = paramBaseDatos;
this.usuario = paramUsuario;
this.contrasena = paramContrasena;
this.gestorBD = paramGestorBD;
if (gestorBD.equalsIgnoreCase("ORACLE")) {
this.cadenaConexion = "jdbc:oracle:thin:@" + ipHost + ":" + puerto + ":" + baseDatos;
this.driver = "oracle.jdbc.OracleDriver";
} else if (gestorBD.equalsIgnoreCase("MYSQL")) {
this.cadenaConexion = "jdbc:mysql://" + ipHost + ":" + puerto + "/" + baseDatos;
this.driver = "com.mysql.jdbc.Driver";
} else if(gestorBD.equalsIgnoreCase("POSTGRES")){
this.cadenaConexion = "jdbc:postgresql://" + ipHost + ":" + puerto + "/" + baseDatos;
this.driver = "org.postgresql.Driver";
}else if(this.gestorBD.equalsIgnoreCase("SQLSERVER")){
this.cadenaConexion = "jdbc:sqlserver://" + ipHost + ":" + puerto + ";DatabaseName=" + baseDatos;
this.driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}else {
System.err.println("No existe implementacion para ese gestor");
}
}
public boolean conectarBD() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
try {
Class.forName(this.driver).newInstance();
cnx = DriverManager.getConnection(cadenaConexion, usuario, contrasena);
cnx.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
cnx.setAutoCommit(false);
return true;
} catch (ClassNotFoundException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
} catch (InstantiationException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
} catch (IllegalAccessException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public boolean desconectarBD() throws SQLException {
try {
cnx.close();
return true;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public boolean confirmar() throws SQLException {
try {
cnx.commit();
return true;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public boolean deshacer() throws SQLException {
try {
cnx.rollback();
return true;
} catch (SQLException ex) {
System.err.println("causa: " + ex.getCause());
System.err.println("Mensaje: " + ex.getMessage());
System.err.println("Localizacion: " + ex.getLocalizedMessage());
System.err.println("Traza: " + ex.getStackTrace());
throw ex;
}
}
public Connection getCnx() {
return cnx;
}
public void setCnx(Connection cnx) {
this.cnx = cnx;
}
public String getIpHost() {
return ipHost;
}
public void setIpHost(String ipHost) {
this.ipHost = ipHost;
}
public String getPuerto() {
return puerto;
}
public void setPuerto(String puerto) {
this.puerto = puerto;
}
public String getBaseDatos() {
return baseDatos;
}
public void setBaseDatos(String baseDatos) {
this.baseDatos = baseDatos;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getContrasena() {
return contrasena;
}
public void setContrasena(String contrasena) {
this.contrasena = contrasena;
}
public String getCadenaConexion() {
return cadenaConexion;
}
public void setCadenaConexion(String cadenaConexion) {
this.cadenaConexion = cadenaConexion;
}
public String getGestorBD() {
return gestorBD;
}
public void setGestorBD(String gestorBD) {
this.gestorBD = gestorBD;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
}
Clase: ObjetoConexion:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.develtrex.dao;
import java.sql.SQLException;
/**
*
* @author 007M
*/
public class ObjetoConexion {
private Conexion oracle;
private Conexion mysql;
private Conexion postgres;
private Conexion sqlServer;
public boolean conectarOracle() throws ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, SQLException{
oracle=new Conexion(
"192.168.1.124",
"1521",
"orcl",
"sysferronor",
"sysferronor",
"ORACLE"
);
return oracle.conectarBD();
}
public boolean conectarMysql() throws ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, SQLException{
mysql=new Conexion(
"192.168.1.124",
"3306",
"celltower",
"root",
"toor",
"MYSQL"
);
return mysql.conectarBD();
}
public boolean conectarPostgres() throws ClassNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
postgres=new Conexion(
"192.168.1.124",
"5432",
"tumi",
"postgres",
"toor",
"POSTGRES"
);
return postgres.conectarBD();
}
public boolean conectarSQLServer() throws ClassNotFoundException, ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, IllegalAccessException, SQLException{
sqlServer=new Conexion(
"192.168.1.70",
"1433",
"appdevel",
"sa",
"",
"SQLSERVER"
);
return sqlServer.conectarBD();
}
public Conexion getOracle() {
return oracle;
}
public void setOracle(Conexion oracle) {
this.oracle = oracle;
}
public Conexion getMysql() {
return mysql;
}
public void setMysql(Conexion mysql) {
this.mysql = mysql;
}
public Conexion getPostgres() {
return postgres;
}
public void setPostgres(Conexion postgres) {
this.postgres = postgres;
}
}
Clase Principal o de Ejecuciòn:
package com.develtrex.tester;
import com.develtrex.dao.ObjetoConexion;
import java.sql.SQLException;
/**
*
* @author 007M
*/
public class Main {
public static void main(String arg[]) throws ClassNotFoundException, ClassNotFoundException, InstantiationException, InstantiationException, IllegalAccessException, SQLException {
ObjetoConexion objCnx=new ObjetoConexion();
System.out.println("Postgres: "+objCnx.conectarPostgres());
System.out.println("Mysql: "+objCnx.conectarMysql());
System.out.println("Oracle: "+objCnx.conectarOracle());
System.out.println("SqlServer: "+objCnx.conectarSQLServer());
}
}
Comentarios
Publicar un comentario