jdbc獲取資料庫連線

不設限發表於2011-12-10
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionsManager {
private static String url;
private static String user="root";
private static String password="dada";
private static Connection conn;


public static Connection getOracleConnection() {
try {
// 初始化連結
url = "jdbc:oracle:thin:@localhost:1521:newsRelease";
user = "dada";
password = "dada";
// 載入驅動
Class.forName("oracle.jdbc.driver.OracleDriver");
// 獲取連結
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}


public static Connection getMysqlConnection() {
try {
// 初始化連結
url = "jdbc:mysql://127.0.0.1/newsRelease?characterEncoding=utf-8";
//String url="jdbc:mysql://127.0.0.1/dandan?user=root&password=dada&characterEncoding=utf-8";
// 載入驅動
Class.forName("org.gjt.mm.mysql.Driver");
// 獲取連結
conn = DriverManager.getConnection(url,user,password);
//conn= DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}

相關文章