摘要:由來與描述沒有前開發(fā)者想操作數(shù)據(jù)庫,必須需要了解每個數(shù)據(jù)庫對應(yīng)的數(shù)據(jù)庫驅(qū)動程序,由于每個數(shù)據(jù)庫驅(qū)動程序的都不同,所以當(dāng)需要遷移數(shù)據(jù)庫時(shí),根本不平滑,需要大量修改與重寫有了后公司也知道問題之后,就提出一種約束規(guī)范,讓所有數(shù)據(jù)庫廠商都按照這個規(guī)
JDBC由來與描述
沒有JDBC前
開發(fā)者想操作數(shù)據(jù)庫,必須需要了解每個數(shù)據(jù)庫對應(yīng)的數(shù)據(jù)庫驅(qū)動程序API,由于每個數(shù)據(jù)庫驅(qū)動程序的API都不同,所以當(dāng)需要遷移數(shù)據(jù)庫時(shí),根本不平滑,需要大量修改與重寫
有了JDBC后
Sum公司也知道問題之后,就提出一種約束 —— JDBC規(guī)范,讓所有數(shù)據(jù)庫廠商都按照這個JDBC規(guī)范來開發(fā)數(shù)據(jù)庫驅(qū)動程序,以便于Java程序能用統(tǒng)一方式操作不同的數(shù)據(jù)庫,數(shù)據(jù)庫遷移變成可行
JDBC描述
JDBC是一種Java web的規(guī)范,需要具體開發(fā)者(數(shù)據(jù)庫廠商)來實(shí)現(xiàn)JDBC常用四大API
DriverManager
作用: 1、注冊驅(qū)動 Class.forName("com.jdbc.mysql.Driver"); 2、獲取連接 Connection conn = DriverManager.getConnection();
Connection
作用: 1、創(chuàng)建執(zhí)行SQL語句的對象(3種) Statement createStatement() // 執(zhí)行SQL語句 PreparedStatement prepareStatement() // 預(yù)編譯SQL并執(zhí)行 CallableStatement prepareCall() // 執(zhí)行SQL存儲過程 2、進(jìn)行事務(wù)管理 setAutoCommit(boolean b) commit() rollback()
Statement
作用: 1、執(zhí)行SQL語句 boolean execute(String sql) // 執(zhí)行SQL語句,執(zhí)行select就返回true,否則返回false ResultSet executeQuery(String sql) // 執(zhí)行SQL中的select語句 int executeUpdate(String sql) // 執(zhí)行SQL中的insert/update/delete語句 2、執(zhí)行批量SQL語句 addBatch(String sql) // 添加到批量處理 executeBatch() // 執(zhí)行批量處理 clearBatch() // 清空批量處理
ResultSet
(select語句)查詢結(jié)果的集合 作用:獲取到查詢的結(jié)果 next() //判斷是否有下一條數(shù)據(jù) getXXX() //根據(jù)數(shù)據(jù)類型獲取查詢記錄的數(shù)據(jù) getObject() //通用獲取數(shù)據(jù)方法JDBC資源釋放
釋放原則:晚創(chuàng)建,早釋放;資源稀有,不釋放很快會阻塞
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from user"); rs.close(); stmt.close(); conn.close();JDBC CURD操作
增加
Connection conn = null; Statement stmt = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 獲取執(zhí)行SQL語句對象 stmt = conn.createStatement(); // 編寫SQL String sql = "insert into user values ("xiaomin","女人",12)"; // 執(zhí)行SQL語句 int i = stmt.executeUpdate(sql); if(i > 0){ System.out.println("插入成功!"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(stmt != null){ try{ stmt .close(); }catch(SQLException e){ e.printStackTrace(); }finally{ stmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }
更新
Connection conn = null; Statement stmt = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 獲取執(zhí)行SQL語句對象 stmt = conn.createStatement(); // 編寫SQL String sql = "update user set name = "wt""; // 執(zhí)行SQL語句 int i = stmt.executeUpdate(sql); if(i > 0){ System.out.println("更新成功!"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(stmt != null){ try{ stmt .close(); }catch(SQLException e){ e.printStackTrace(); }finally{ stmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }
刪除
Connection conn = null; Statement stmt = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 獲取執(zhí)行SQL語句對象 stmt = conn.createStatement(); // 編寫SQL String sql = "delete from user where id = 3"; // 執(zhí)行SQL語句 int i = stmt.executeUpdate(sql); if(i > 0){ System.out.println("刪除成功!"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(stmt != null){ try{ stmt .close(); }catch(SQLException e){ e.printStackTrace(); }finally{ stmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }
查詢
Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 獲取執(zhí)行SQL語句對象 stmt = conn.createStatement(); // 編寫SQL String sql = "select * from user"; // 執(zhí)行SQL語句 rs = stmt.executeQuery(sql); if(rs.next()){ System.out.print(rs.getString("name")); } }catch(Exception e){ e.printStackTrace(); }finally{ if(rs != null){ try{ rs .close(); }catch(SQLException e){ e.printStackTrace(); }finally{ rs = null; } } if(stmt != null){ try{ stmt .close(); }catch(SQLException e){ e.printStackTrace(); }finally{ stmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }JDBC SQL注入解決方案
開發(fā)時(shí),使用 PreparedStatement對象 取代 Statement對象 來執(zhí)行SQL,有效避免SQL注入
增加
Connection conn = null; PreparedStatement pstmt = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 編寫SQL String sql = "insert into user values (?,?,?)"; // 獲取執(zhí)行SQL語句對象 pstmt = conn.preparedStatement(sql); // 設(shè)置參數(shù) pstmt.setString(1, "qqq"); pstmt.setString(2, "bbb"); pstmt.setString(3, "ccc"); // 執(zhí)行SQL語句 int i = pstmt.executeUpdate(); if(i > 0){ System.out.println("插入成功!"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(pstmt != null){ try{ pstmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ pstmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }
更新
Connection conn = null; PreparedStatement pstmt = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 編寫SQL String sql = "update user set name = ?, age = ?, pwd = ? where id = ?"; // 獲取執(zhí)行SQL語句對象 pstmt = conn.preparedStatement(sql); // 設(shè)置參數(shù) pstmt.setString(1, "wt"); pstmt.setString(2, 15); pstmt.setString(3, "basdcx"); pstmt.setString(4, 10); // 執(zhí)行SQL語句 int i = pstmt.executeUpdate(); if(i > 0){ System.out.println("更新成功!"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(pstmt != null){ try{ pstmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ pstmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }
刪除
Connection conn = null; PreparedStatement pstmt = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 編寫SQL String sql = "delete from user where id = ?"; // 獲取執(zhí)行SQL語句對象 pstmt = conn.preparedStatement(sql); // 設(shè)置參數(shù) pstmt.setString(1, 16); // 執(zhí)行SQL語句 int i = pstmt.executeUpdate(); if(i > 0){ System.out.println("刪除成功!"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(pstmt != null){ try{ pstmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ pstmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }
查詢
Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ // 注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); // 獲得連接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","root"); // 編寫SQL String sql = "select * from user where sex = ?"; // 獲取執(zhí)行SQL語句對象 pstmt = conn.preparedStatement(sql); // 設(shè)置參數(shù) pstmt.setString(1, "男"); // 執(zhí)行SQL語句 rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString("name")); } }catch(Exception e){ e.printStackTrace(); }finally{ if(rs != null){ try{ rs.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ rs = null; } } if(pstmt != null){ try{ pstmt.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ pstmt = null; } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); }finally{ conn = null; } } }連接池(數(shù)據(jù)源)JDBC優(yōu)化技術(shù)
數(shù)據(jù)庫連接頻繁創(chuàng)建與消耗,在資源使用上是一種浪費(fèi)
常用連接池
C3P0、HikariCP、Druid、Tomcat、Dbcp
用法(以C3P0為例)
ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 只需一個對象 // 獲取連接 Connection conn = dataSource.getConnection();
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72313.html
摘要:通過這個驅(qū)動程序,我們就能夠兩個數(shù)據(jù)類型的相互轉(zhuǎn)化了。和和方法可以將特定的類型轉(zhuǎn)換為特定的數(shù)據(jù)類型。和可以將幾乎任何數(shù)據(jù)類型映射到數(shù)據(jù)類型。時(shí)間與日期類型類映射到類型,和類分別映射到和數(shù)據(jù)類型。 概述 我們知道Java的數(shù)據(jù)類型和數(shù)據(jù)庫中的類型并不是一一對應(yīng)的,我們在使用JDBC在與數(shù)據(jù)庫進(jìn)行交互的時(shí)候,比如我們向數(shù)據(jù)庫中插入一條數(shù)據(jù),或者從數(shù)據(jù)庫中查詢一個數(shù)據(jù),為什么我們能夠正常的讀...
摘要:知識點(diǎn)總結(jié)概要知識點(diǎn)總結(jié)簡介為開發(fā)者使用數(shù)據(jù)庫提供了統(tǒng)一的編程接口,它由一組類和接口組成主要在包中。跟蹤可用的驅(qū)動程序,并在數(shù)據(jù)庫和相應(yīng)的驅(qū)動程序之間建立連接。接口與特定數(shù)據(jù)庫的連接會話,在連接上下文中執(zhí)行語句并返回結(jié)果。 Java知識點(diǎn)總結(jié)(JDBC-概要) @(Java知識點(diǎn)總結(jié))[Java, JDBC] 簡介 JDBC(Java Database Connection)為Java...
摘要:軟件開發(fā)體系架構(gòu)兩層架構(gòu)傳統(tǒng)的客戶服務(wù)器系統(tǒng)僅只簡單地基于兩層體系來構(gòu)建,即客戶端前臺和企業(yè)信息系統(tǒng)后臺,沒有任何中間件,業(yè)務(wù)邏輯層與表示層或數(shù)據(jù)層混在一起。 showImg(https://segmentfault.com/img/remote/1460000007090113); 理想的建筑師應(yīng)該既是文學(xué)家又是數(shù)字家,他還應(yīng)通曉歷史,熱衷于哲學(xué)研究,精通音樂,懂得醫(yī)藥知識,具有法學(xué)...
閱讀 3777·2023-04-25 20:00
閱讀 3122·2021-09-22 15:09
閱讀 519·2021-08-25 09:40
閱讀 3428·2021-07-26 23:38
閱讀 2214·2019-08-30 15:53
閱讀 1103·2019-08-30 13:46
閱讀 2801·2019-08-29 16:44
閱讀 2056·2019-08-29 15:32