Mysql資料庫的JDBC查詢例項
阿新 • • 發佈:2020-12-09
之前我們使用JDBC操作過Oracle資料庫(參見Oracle資料庫的JDBC查詢例項),這次我們來操作一下Mysql,使用Statement物件的子類PreparedStatement:
1、在pom.xml中引入mysql依賴jar包:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency>
2、再呼叫JDBC的API跟Mysql資料庫互動:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Test { private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //資料庫驅動的具體實現類 // 資料庫連結 private static final String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=UTC"; private static final String USER = "root"; // 資料庫使用者名稱 private static final String PASSWORD = "Wlf12345!"; // 資料庫密碼 public static void main(String[] args) { String sql= "select NAME from test_wlf where id = ?"; Connection conn = null; PreparedStatement ps = null; try { Class.forName(JDBC_DRIVER); // 0、註冊mysql資料庫驅動程式,由具體實現類的靜態方法呼叫DriverManager.registerDriver conn = DriverManager.getConnection(DB_URL, USER, PASSWORD); // 1、獲取資料庫連結物件 ps = conn.prepareStatement(sql); // 2、獲取PreparedStatement物件 ps.setInt(1, 2); // 給第一個?問號(引數下標,這裡只有一個引數)填值,我們取id為2的name值 ResultSet rs = ps.executeQuery(); // 3、執行SQL while (rs.next()) { String paramName = rs.getString("NAME"); System.out.println("NAME: " + paramName); } rs.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) { conn.close(); } } catch (SQLException se) { } try { if (conn != null) { conn.close(); } } catch (SQLException se) { se.printStackTrace(); } } }
執行結果:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. NAME: lulu
我們的資料庫表裡資料如下:
E:\BaiduNetdiskDownload>mysql -uroot -pWlf12345! mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.7.26-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use test; Database changed mysql> desc test_wlf; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(25) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.02 sec) mysql> select * from t_test_wlf; ERROR 1146 (42S02): Table 'test.t_test_wlf' doesn't exist mysql> select * from test_wlf; +------+------+ | id | name | +------+------+ | 1 | wulf | | 2 | lulu | +------+------+ 2 rows in set (0.00 sec)
我們拿Mysql的程式碼跟之前Oracle比較一番,除了資料庫驅動類、資料庫連結URL這些之外,就是使用了PreparedStatement。PreparedStatement 介面擴充套件了 Statement,添加了為語句中包含的引數標記設定值的功能。引數標記(由SQL字串中的“?”表示)用於指定語句的輸入值,這些值可能在執行時發生變化。
PreparedStatement 介面定義 setter 方法,這些方法用於替換預編譯 SQL 字串中每個引數標記的值。如上面例子中setInt方法,第一個引數始終是一個 int,等於要設定的引數標記的序號位置,從1開始(第一個引數標記是id)。第二個引數指定要分配的引數的具體值(id=2)。
如果對JDBC的資料庫驅動註冊過程感興趣,比如我們配置的com.mysql.jdbc.Driver是個什麼東東,Class.forName(JDBC_DRIVER)有什麼用?這些問題可以參見橋接模式和JDBC