Java使用JDBC連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
本文實(shí)例講述了Java使用JDBC連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,是Java數(shù)據(jù)庫(kù)程序設(shè)計(jì)里非常實(shí)用的重要技巧。分享給大家供大家參考。具體如下:
JDBC(Java Data Base Connectivity)數(shù)據(jù)庫(kù)連接,通常我們?cè)诰帉憌eb應(yīng)用或java應(yīng)用程序要連接數(shù)據(jù)庫(kù)時(shí)就要使用JDBC。使用JDBC連接數(shù)據(jù)庫(kù)一般步驟有:
1、加載驅(qū)動(dòng)程序
Class.forName(driver);
2、創(chuàng)建連接對(duì)象
Connection con = DriverManager.getConnection(url,username,password);
3、創(chuàng)建sql語(yǔ)句執(zhí)行對(duì)象
4、執(zhí)行sql語(yǔ)句
5、對(duì)執(zhí)行結(jié)果進(jìn)行處理
6、關(guān)閉相關(guān)的連接對(duì)象(順序跟聲明的順序相反)
下面是以建立與MySQL數(shù)據(jù)庫(kù)連接的例子,其他數(shù)據(jù)庫(kù)的過(guò)程類似:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConnection { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; //localhost指本機(jī),也可以用本地ip地址代替,3306為MySQL數(shù)據(jù)庫(kù)的默認(rèn)端口號(hào),“user”為要連接的數(shù)據(jù)庫(kù)名 String url = "jdbc:mysql://localhost:3306/user"; //填入數(shù)據(jù)庫(kù)的用戶名跟密碼 String username = "test"; String password = "test"; String sql = "select * from user";//編寫要執(zhí)行的sql語(yǔ)句,此處為從user表中查詢所有用戶的信息 try { Class.forName(driver);//加載驅(qū)動(dòng)程序,此處運(yùn)用隱式注冊(cè)驅(qū)動(dòng)程序的方法 } catch(ClassNotFoundException e) { e.printStackTrace(); } try { Connection con = DriverManager.getConnection(url,username,password);//創(chuàng)建連接對(duì)象 Statement st = con.createStatement();//創(chuàng)建sql執(zhí)行對(duì)象 ResultSet rs = st.executeQuery(sql);//執(zhí)行sql語(yǔ)句并返回結(jié)果集 while(rs.next())//對(duì)結(jié)果集進(jìn)行遍歷輸出 { System.out.println("username: "+rs.getString(1));//通過(guò)列的標(biāo)號(hào)來(lái)獲得數(shù)據(jù) System.out.println("useradd: "+rs.getString("useradd"));//通過(guò)列名來(lái)獲得數(shù)據(jù) System.out.println("userage: "+rs.getInt("userage")); } //關(guān)閉相關(guān)的對(duì)象 if(rs != null) { try { rs.close(); } catch(SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch(SQLException e) { e.printStackTrace(); } } if(con !=null) { try { con.close(); } catch(SQLException e) { e.printStackTrace(); } } } catch(SQLException e) { e.printStackTrace(); } } }
相信本文所述對(duì)大家的Java數(shù)據(jù)庫(kù)程序設(shè)計(jì)有一定的借鑒價(jià)值。
- java jdbc連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查操作
- java連接MySQl數(shù)據(jù)庫(kù)實(shí)例代碼
- java連接mysql數(shù)據(jù)庫(kù)亂碼的解決方法
- java連接mysql數(shù)據(jù)庫(kù)及測(cè)試是否連接成功的方法
- java 獲取數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)代碼
- Java連接MYSQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
- java連接數(shù)據(jù)庫(kù)知識(shí)點(diǎn)總結(jié)以及操作應(yīng)用
相關(guān)文章
攔截Druid數(shù)據(jù)源自動(dòng)注入帳密解密實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了攔截Druid數(shù)據(jù)源自動(dòng)注入帳密解密實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼
CRUD是Create(創(chuàng)建)、Read(讀?。?、Update(更新)和Delete(刪除)的縮寫,它是普通應(yīng)用程序的縮影。接下來(lái)通過(guò)本文給大家介紹Struts2實(shí)現(xiàn)CRUD(增 刪 改 查)功能實(shí)例代碼,感興趣的朋友一起看看吧2016-06-06基于 IntelliJ IDEA 模擬 Servlet 網(wǎng)絡(luò)請(qǐng)求示例
這篇文章主要介紹了基于 IntelliJ IDEA 模擬 Servlet 網(wǎng)絡(luò)請(qǐng)求示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Java實(shí)現(xiàn)的數(shù)字簽名算法RSA完整示例
這篇文章主要介紹了Java實(shí)現(xiàn)的數(shù)字簽名算法RSA,結(jié)合完整實(shí)例形式詳細(xì)分析了RSA算法的相關(guān)概念、原理、實(shí)現(xiàn)方法及操作技巧,需要的朋友可以參考下2019-09-09