Java原生操作JDBC連接以及原理詳解
一、簡(jiǎn)介
JDBC全稱又叫做Java DataBase Connectivity,也就是Java數(shù)據(jù)庫(kù)連接,說(shuō)白了就是用Java語(yǔ)言來(lái)操作數(shù)據(jù) 庫(kù),提供統(tǒng)一API訪問(wèn)數(shù)據(jù)庫(kù)操作。
二、原理
JDBC主要是用于java連接數(shù)據(jù)庫(kù)的,能連接什么數(shù)據(jù)庫(kù)不固定,其實(shí)能連接很多種數(shù)據(jù)庫(kù),而且一般來(lái)說(shuō)可以連接oracle和mysql,通常也是這兩種。但是既然JDBC能連接這么多的數(shù)據(jù)庫(kù),開(kāi)發(fā)起來(lái)太麻煩了,于是sun公司那些人想出了一個(gè)辦法,我定義一套規(guī)范,大家都按照這個(gè)規(guī)范來(lái),實(shí)現(xiàn)自己公司訪問(wèn)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)。這套規(guī)范就是JDBC,遵循了JDBC規(guī)范的,可以訪問(wèn)自己數(shù)據(jù)庫(kù)的API被稱之為數(shù)據(jù)庫(kù)驅(qū)動(dòng)。
三、數(shù)據(jù)庫(kù)操作
數(shù)據(jù)庫(kù)操作分為6個(gè)步驟(增、刪、改可跳過(guò)第5步,移步看第6步):
1、首先,項(xiàng)目中檢查是否已導(dǎo)入mysql連接jar包,如:mysql-connector-java-5.1.6-bin.jar
2、使用反射注冊(cè)數(shù)據(jù)庫(kù)驅(qū)動(dòng),Class.forName(name);
3、通過(guò)DriverManager.getConnection(url, username, password)構(gòu)造器獲取數(shù)據(jù)庫(kù)連接
4、通過(guò)Connection連接對(duì)象獲取數(shù)據(jù)庫(kù)操作對(duì)象PrepareStatement或者Statement
5、通過(guò)獲取的Statement對(duì)象執(zhí)行executeQuery()操作返回ResultSet結(jié)果集,需判斷是否為空
6、通過(guò)獲取的Statement對(duì)象執(zhí)行executeUpdate()操作返回受影響的行數(shù),判斷是否成功標(biāo)識(shí)
7、釋放連接資源,關(guān)閉順序?yàn)椋篟esultSet -> PrepareStatement / Statement -> Connection
解釋說(shuō)明關(guān)鍵類:
(1)DriverManager:該類管理數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序。
(2)Connection:管理數(shù)據(jù)庫(kù)建立的連接。
(3)Statement:負(fù)責(zé)將要執(zhí)行的sql體提交到數(shù)據(jù)庫(kù)。
(4)ResultSet:執(zhí)行sql查詢語(yǔ)句返回的結(jié)果集。
(5)PreparedStatement: 可以使用預(yù)編譯的SQL,防止SQL注入,而Statment只能使用靜態(tài)的SQL。
PreparedStatement 與 Statment 區(qū)別:
語(yǔ)法不同: PreparedStatement可以使用預(yù)編譯的sql,而Statment只能使用靜態(tài)的sql。
效率不同: PreparedStatement可以使用sql緩存區(qū),效率比Statment高。
安全性不同: PreparedStatement可以有效防止sql注入,而Statment不能防止sql注入。
四、數(shù)據(jù)庫(kù)增、刪、改、查示例
工具類(可做相對(duì)應(yīng)靈活替換):
package com.bnd.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Objects; /** * @author 作者 mbql: * @version 創(chuàng)建時(shí)間:2021年1月10日 下午5:14:16 * 類說(shuō)明 JDBC操作數(shù)據(jù)庫(kù)工具類 */ public class JdbcUtils { private static final String driverName = "com.mysql.jdbc.Driver"; // 數(shù)據(jù)庫(kù)驅(qū)動(dòng)名稱 private static final String url = "jdbc:mysql://39.108.146.20:3307/jsp?useUnicode=true&characterEncoding=utf-8"; // 數(shù)據(jù)庫(kù)url private static final String userName = "root"; // 用戶名 private static final String password = "123456"; // 用戶密碼 private static Connection conn = null; // 數(shù)據(jù)庫(kù)連接對(duì)象 private static PreparedStatement preparedStatement = null; // 執(zhí)行操作對(duì)象 static { try { Class.forName(driverName); // 2、注冊(cè)數(shù)據(jù)庫(kù)驅(qū)動(dòng) conn = DriverManager.getConnection(url, userName, password); // 3、獲取數(shù)據(jù)庫(kù)連接 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 查詢數(shù)據(jù) * @return 返回查詢到結(jié)果 */ public static ResultSet queryData() { ResultSet result = null; String sql = "select id, name, pwd from user"; // 定義預(yù)編譯SQL try { preparedStatement = conn.prepareStatement(sql); // 4、執(zhí)行預(yù)編譯SQL操作 result = preparedStatement.executeQuery(); // 5、執(zhí)行查詢操作 while (result.next()) { int id = result.getInt(1); String name = result.getString(2); String pwd = result.getString(3); System.out.println("id:" + id + "--> name:" + name + "--> pwd:" + pwd); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { // 7、釋放資源 close(conn, preparedStatement, result); } return result; } /** * 添加數(shù)據(jù) * @return 返回受影響行數(shù) */ public static int addData() { String sql = "insert into user(name,pwd) values(?, ?)"; try { preparedStatement = conn.prepareStatement(sql); // 4、執(zhí)行預(yù)編譯SQL操作 preparedStatement.setString(1, "tom"); preparedStatement.setString(2, "abc123"); int count = preparedStatement.executeUpdate(); // 6、執(zhí)行添加操作 if (count > 0) { return count; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { // 7、釋放資源 close(conn, preparedStatement, null); } return -1; } /** * 更新數(shù)據(jù) * @return 返回受影響行數(shù) */ public static int updateData() { String sql = "update user set name = ?, pwd = ? where id = ?"; try { preparedStatement = conn.prepareStatement(sql); // 4、執(zhí)行預(yù)編譯SQL操作 preparedStatement.setString(1, "sueno"); preparedStatement.setString(2, "qwe123"); preparedStatement.setInt(3, 10); int count = preparedStatement.executeUpdate(); // 6、執(zhí)行添加操作 if (count > 0) { return count; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { // 7、釋放資源 close(conn, preparedStatement, null); } return -1; } /** * 刪除數(shù)據(jù) * @return 返回受影響行數(shù) */ public static int deleteData() { String sql = "delete from user where id = ?"; try { preparedStatement = conn.prepareStatement(sql); // 4、執(zhí)行預(yù)編譯SQL操作 preparedStatement.setInt(1, 11); int count = preparedStatement.executeUpdate(); // 6、執(zhí)行添加操作 if (count > 0) { return count; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { // 7、釋放資源 close(conn, preparedStatement, null); } return -1; } /** * 釋放資源 * @param conn 關(guān)閉數(shù)據(jù)庫(kù)連接 * @param statement 關(guān)閉數(shù)據(jù)庫(kù)操作對(duì)象 * @param rs 關(guān)閉結(jié)果集操作對(duì)象 */ public static void close(Connection conn, Statement statement, ResultSet rs) { if (Objects.nonNull(rs)) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (Objects.nonNull(statement)) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (Objects.nonNull(conn)) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
測(cè)試類:
package com.bnd.util; import java.sql.ResultSet; /** * @author 作者 mbql: * @version 創(chuàng)建時(shí)間:2021年1月10日 下午6:00:30 * 類說(shuō)明 */ public class TestJdbc { public static void main(String[] args) { // 獲取用戶數(shù)據(jù) // ResultSet rs = JdbcUtils.queryData(); // System.out.println(rs); // 添加用戶數(shù)據(jù) // int count = JdbcUtils.addData(); // if (count > 0) { // System.out.println("添加成功!"); // } // 更新用戶數(shù)據(jù) // int count = JdbcUtils.updateData(); // if (count > 0) { // System.out.println("更新成功!"); // } // 刪除用戶數(shù)據(jù) // int count = JdbcUtils.deleteData(); // if (count > 0) { // System.out.println("刪除成功!"); // } } }
五、總結(jié)
1、通過(guò)簡(jiǎn)單Jdbc連接數(shù)據(jù)庫(kù),巧妙的實(shí)現(xiàn)了CRUD;其實(shí)實(shí)現(xiàn)并不難,記一下數(shù)據(jù)庫(kù)連接步驟就行。
2、數(shù)據(jù)庫(kù)操作本身就是一套數(shù)據(jù)庫(kù)規(guī)范,根據(jù)不同驅(qū)動(dòng)能夠達(dá)到連接不同的數(shù)據(jù)庫(kù),實(shí)現(xiàn)可實(shí)行操作。
3、劇上述連接可知,數(shù)據(jù)庫(kù)連接是非常耗性能的,頻繁的數(shù)據(jù)庫(kù)操作,不及時(shí)釋放資源,會(huì)導(dǎo)致IO阻塞。
到此這篇關(guān)于Java原生操作JDBC連接以及原理的文章就介紹到這了,更多相關(guān)Java原生操作JDBC連接及原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud hystrix服務(wù)降級(jí)概念介紹
什么是服務(wù)降級(jí)?當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實(shí)際業(yè)務(wù)情況及流量,對(duì)一些服務(wù)和頁(yè)面有策略的不處理或換種簡(jiǎn)單的方式處理,從而釋放服務(wù)器資源以保證核心交易正常運(yùn)作或高效運(yùn)作2022-09-09Hibernate對(duì)數(shù)據(jù)庫(kù)刪除、查找、更新操作實(shí)例代碼
本篇文章主要介紹了Hibernate對(duì)數(shù)據(jù)庫(kù)刪除、查找、更新操作實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法
這篇文章主要介紹了idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Spring注解驅(qū)動(dòng)之ApplicationListener異步處理事件說(shuō)明
這篇文章主要介紹了Spring注解驅(qū)動(dòng)之ApplicationListener異步處理事件說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09MyBatis limit分頁(yè)設(shè)置的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis limit分頁(yè)設(shè)置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng)
這篇文章主要介紹了基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06java實(shí)現(xiàn)文件夾上傳功能實(shí)例代碼(SpringBoot框架)
在web項(xiàng)目中上傳文件夾現(xiàn)在已經(jīng)成為了一個(gè)主流的需求,下面這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)文件夾上傳功能(springBoot框架)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04