这篇文章主要介绍了java获得MysqL和oracle链接的类,可实现基于jdbc的MysqL与oracle数据库连接,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了java获得MysqL和oracle链接的类。分享给大家供大家参考。具体如下:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.sqlException; public class ConnectDB { private static final String MysqL = "jdbc:MysqL://"; private static final String ORACLE = "jdbc:oracle:thin:@"; private ConnectDB() { } public static Connection getInstance(String DBType, String url) throws NoSuchDBException, sqlException { if ("MysqL".equalsIgnoreCase(DBType)) return getMysqLConn(url); if ("oracle".equalsIgnoreCase(DBType)) return getoracleConn(url); return null; } public static void closeConn(Connection conn) { if (conn != null) { try { conn.close(); } catch (sqlException e) { e.printstacktrace(); } } } private static Connection getMysqLConn(String url) throws sqlException { Connection conn = null; try { Class.forName("com.MysqL.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printstacktrace(); } conn = DriverManager.getConnection(MysqL + url, "root", "root"); return conn; } private static Connection getoracleConn(String url) throws sqlException { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printstacktrace(); } conn = DriverManager.getConnection(ORACLE + url, "scott", "tiger"); return conn; } }
希望本文所述对大家的java程序设计有所帮助。