Estableces conexión con una base de datos MYSQL, PosgreSQL, DB2 desde Java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Leyer
 */
public class DataBase {

	private  String  driverClassName = "";
    private  String  type            = "";
	private Connection connection = null;
	
	public static final String MYSQL        = "com.mysql.jdbc.Driver";
	public static final String DB2          = "COM.ibm.db2.jdbc.app.DB2Driver";
	public static final String POSTGRESQL   = "org.postgresql.Driver";
	/*------------------------------*/
	private String user     = null;
	private String password = null;
	private String url      = null;
	/*------------------------------*/
	/**
	 * @param DataBase.MYSQL
	 *        DataBase.DB2
	 *        DataBase.POSTGRESQL
	 */
	public DataBase(String jdbc){
		if(jdbc.equals(MYSQL)){
			this.driverClassName = MYSQL;
			this.type            = "jdbc:mysql:";
			try {
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(jdbc.equals(DB2)){
			this.driverClassName = DB2;
			this.type            = "jdbc:db2:";
			try {
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(jdbc.equals(POSTGRESQL)){
			this.driverClassName = POSTGRESQL;
			this.type            = "jdbc:postgresql";
			try {
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * @param "jdbc"     -----> DataBase.MYSQL, DataBase.DB2, DataBase.POSTGRESQL
	 * @param "url"      -----> Base de datos
	 * @param "user"     -----> Usuario
	 * @param "password" -----> Contraseña
	 */
	public DataBase(String jdbc,String url,String user,String password){
		if(jdbc.equals(MYSQL)){
			this.driverClassName = MYSQL;
			this.type     = "jdbc:mysql:"+url;
			this.url       = url;
			this.user     = user;
			this.password = password;
			try {
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(jdbc.equals(DB2)){
			this.driverClassName = DB2;
			this.type     = "jdbc:db2:"+url;
			this.url       = url;
			this.user     = user;
			this.password = password;
			try {
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(jdbc.equals(POSTGRESQL)){
			this.driverClassName = POSTGRESQL;
			this.type     = "jdbc:postgresql:"+url;
			this.url       = url;
			this.user     = user;
			this.password = password;
			try {
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void closeConnection() {
		if (connection != null)
			try {
				connection.setAutoCommit(true);
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		connection = null;
	}
	public PreparedStatement preparedStatement(String stringSql) {
		if (connection != null) {
			try {
				PreparedStatement preparedStatement = connection
						.prepareStatement(stringSql);
				return preparedStatement;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public ResultSet executeQuery(String stringSql) {
		ResultSet rs = null;
		if (connection != null)
			try {
				Statement statement = connection.createStatement();
				rs = statement.executeQuery(stringSql);
			} catch (SQLException e) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		return rs;
	}
	public boolean executeUpdate(String stringSql) {
		if (connection != null)
			try {
				Statement statement = connection.createStatement();
				if (statement.executeUpdate(stringSql) > 0)
					return true;
			} catch (SQLException e) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		return false;
	}
	public boolean openConnection() {
		try {
			Class.forName(driverClassName);
			connection = DriverManager.getConnection(type,user, password);
			connection.createStatement();
			connection.setAutoCommit(false);
			return true;
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
		} catch (ClassNotFoundException classNotFoundException) {
			classNotFoundException.printStackTrace();
		}
		return false;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
}
Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: