欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java获得mysql和oracle链接的类

程序员文章站 2024-03-04 22:19:00
本文实例讲述了java获得mysql和oracle链接的类。分享给大家供大家参考。具体如下: import java.sql.connection; impor...

本文实例讲述了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程序设计有所帮助。