今天就跟大家聊聊有關(guān)如何使用Jdbc連接不同數(shù)據(jù)庫(kù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1.Jdbc連接Access數(shù)據(jù)庫(kù)
①通過(guò)控制面板>>管理工具>>數(shù)據(jù)源 選擇系統(tǒng)DNS選項(xiàng)卡,添加Microsoft Access Driver (*.mdb),以數(shù)據(jù)源名稱sample為例,選擇數(shù)據(jù)庫(kù)文件。在程序的鏈接代碼如下:
public static Connection getConnection() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:sample", "", ""); //此種連接方式需要建立一個(gè)名為sample的數(shù)據(jù)源
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
②直接在程序中建立連接,代碼如下:
public static Connection getConnection() {
try {
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D://Documents and Settings//Administrator//workspace//sample.mdb";
//url中要給出數(shù)據(jù)庫(kù)的絕對(duì)路徑
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url, "", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
2.Jdbc連接MySQL數(shù)據(jù)庫(kù)(需要引入mysql的驅(qū)動(dòng)程序 )
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bookstore?user=root&password=&characterEncoding=utf-8");//bookstore是你的數(shù)據(jù)庫(kù)名稱
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
3.Jdbc連接Oracle數(shù)據(jù)庫(kù)(需要引入Oracle的驅(qū)動(dòng)程序classes12)
public static Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:oracle9i", "scott","tiger");//oracle9i是數(shù)據(jù)庫(kù)名
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
看完上述內(nèi)容,你們對(duì)如何使用Jdbc連接不同數(shù)據(jù)庫(kù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前標(biāo)題:如何使用Jdbc連接不同數(shù)據(jù)庫(kù)-創(chuàng)新互聯(lián)
文章鏈接:http://aaarwkj.com/article4/ccjgoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站設(shè)計(jì)公司、商城網(wǎng)站、微信小程序、關(guān)鍵詞優(yōu)化、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容