자바 MySql 과 Oracle 을 연결 하여 간단하게 실현

2115 단어
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySqlDbManager {
    private static MySqlDbManager instance = null;
    private MySqlDbManager(){}
    public static MySqlDbManager getInstance(){
        if(null ==instance){
            instance = new MySqlDbManager();
        }
        return instance;
    }
    public Connection getConnection() {
        Connection connection = null;
        String url = "jdbc:mysql://localhost/test";
        String usr = "root";
        String pwd = "123456";
        String driver ="com.mysql.jdbc.Driver";
        try {
            Class.forName(driver);
            connection =DriverManager.getConnection(url, usr, pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

}
/**
 * @author HU-XU
 *      
 */
public class ToolDButil {

	static{
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");//"com.mysql.jdbc.Driver"
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
	}
	
	//    
	public static Connection getConnection(){
		String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";//"jdbc:mysql://localhost/test?user=root&password=root"
		String userid = "scott";
		String password = "huxu";
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(jdbcUrl, userid, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn; 
	}
	
	//      
	public static void free01(Connection cn,Statement st,ResultSet rs){
		try {
			if(rs!=null) rs.close();
			if(st!=null) st.close();
			if(cn!=null) cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void free02(PreparedStatement ps, Connection cn){
		try {
			if(ps!=null) ps.close();
			if(cn!=null) cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기