java程序有连接数据库_Java程序连接数据库
/**
* 了解: 利用 Driver 接口的 connect 方法獲取連接
*/
// 第一種實(shí)現(xiàn)
/**
* 了解: 利用 Driver 接口的 connect 方法獲取連接
*/
@Test
public void oracleJdbcTest() throws Exception {
Driver driver = null; // sun提供的接口
String url = "jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info = null;
info = new Properties();
info.put("user", "scott");
info.put("password", "tiger");
driver = new OracleDriver(); // Oracle數(shù)據(jù)庫(kù)廠商自己實(shí)現(xiàn)sun提供的接口Driver
Connection connect = driver.connect(url, info); // 獲取連接
System.out.println(connect);
}
======================================
/**
* 了解: 使用 DriverManager 來獲取數(shù)據(jù)庫(kù)連接
* 版本1:
* 好處: 不需要使用原生的 Driver 方法來獲取連接.
* 缺點(diǎn): 還是耦合了具體的實(shí)現(xiàn)類.
*/
//第二種實(shí)現(xiàn)
@Test
public void oracleJdbcTest1() throws Exception{
Connection connection=null;
DriverManager.registerDriver(new OracleDriver()); //驅(qū)動(dòng)管理器注冊(cè)O(shè)racle驅(qū)動(dòng),實(shí)現(xiàn)Java程序可以連接Oracle數(shù)據(jù)庫(kù),如果想要連接不同的數(shù)據(jù)庫(kù)則需要注冊(cè)不同的數(shù)據(jù)庫(kù)驅(qū)動(dòng)
String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info=null;
info=new Properties();
info.put("user", "scott");
info.put("password", "tiger");
connection=DriverManager.getConnection( url, info) ; //DriverManager驅(qū)動(dòng)管理器類,里面的方法都是靜態(tài)的,類調(diào)用獲取到一個(gè)連接
System.out.println(connection);
}
====================================
/**
* 了解: 更進(jìn)一步, 不需要關(guān)聯(lián)任何 JDBC 驅(qū)動(dòng)的實(shí)現(xiàn)類。
* 但需要提供 JDBC 驅(qū)動(dòng)中 Driver 接口的實(shí)現(xiàn)類的全類名的字符串.
*/
//第三種實(shí)現(xiàn)
@Test
public void oracleJdbcTest2() throws Exception, InstantiationException, IllegalAccessException, ClassNotFoundException{
Connection connection=null;
String className="oracle.jdbc.driver.OracleDriver";
DriverManager.registerDriver((Driver)Class.forName(className).newInstance()); //注冊(cè)驅(qū)動(dòng)
String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info=null;
info=new Properties();
info.put("user", "scott");
info.put("password", "tiger");
connection=DriverManager.getConnection(url,info);
System.out.println(connection);
}
===========================================================
/**
* 能創(chuàng)建一個(gè)不和具體 Driver 耦合的獲取數(shù)據(jù)庫(kù)連接的方法. 即在方法中不再關(guān)聯(lián)任何數(shù)據(jù)庫(kù)驅(qū)動(dòng)的 JDBC 實(shí)現(xiàn).
*@throws SQLException
*/
/**
* final version: 若需要手動(dòng)獲取數(shù)據(jù)庫(kù)連接:
*
* 更進(jìn)一步, 不需要關(guān)聯(lián)任何 JDBC 驅(qū)動(dòng)的實(shí)現(xiàn)類。
* 但需要提供 JDBC 驅(qū)動(dòng)中 Driver 接口的實(shí)現(xiàn)類的全類名的字符串.
*
* 實(shí)際上, 在驅(qū)動(dòng)的實(shí)現(xiàn)類中有一個(gè)靜態(tài)代碼塊: 創(chuàng)建了 Driver 實(shí)現(xiàn)類的對(duì)象, 并把其注冊(cè)給 DriverManager
* static {
* try {
* java.sql.DriverManager.registerDriver(new Driver());
* } catch (SQLException E) {
* throw new RuntimeException("Can't register driver!");
* }
* }
*
* 而調(diào)用 Class 的 forName 方法在加載類實(shí)例時(shí), 會(huì)調(diào)用靜態(tài)代碼塊.
*
*/
//第四種實(shí)現(xiàn)(最常用)
@Test
public void oracleJdbcTest3() throws Exception{
Connection connection=null;
String className="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info=null;
info=new Properties();
info.put("user", "scott");
info.put("password", "tiger");
Class.forName(className).newInstance();//加載驅(qū)動(dòng)
connection=DriverManager.getConnection(url,info);
System.out.println(connection);
}
總結(jié)
以上是生活随笔為你收集整理的java程序有连接数据库_Java程序连接数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 辽宁师范大学java_辽宁师范大学心理学
- 下一篇: java reduce.mdn_redu