### JDBC Java Database Connectivity 是一個獨立於特定數據庫的管理系統,通用的 SQL 數據庫存取和操做的公共接口。 定義了一組標準,爲訪問不一樣數據庫提供了統一途徑。 JDBC 體系結構 兩個層面: - 面向應用的 API,供開發人員調用。 - 面向數據庫的 API,供數據庫開發廠商開發數據庫驅動程序。 JDBC API 提供者:Java 官方 內容:供開發者調用的接口 java.sql 或者 javax.sql 包中 DriverManager 類:管理數據庫驅動 Connection 接口:鏈接數據庫 Statement 接口:執行 SQL ResultSet 接口:封裝結果集 Driver Manager 提供者:Java 官方 做用:爲不一樣的數據庫產品提供統一的接入標準。 JDBC 驅動 提供者:數據庫廠商 做用:讓 Java 完成與特定數據庫的對接。 ### 使用 JDBC 原理 1、加載數據庫驅動,Java Application 和 數據庫的橋樑。 2、獲取 Connection,一次鏈接。 3、經過 Connection 對象產生 Statement,執行 SQL 語句。 4、ResultSet 保存 Statment 執行後所產生的結果。 ```java package com.southwind.test; import com.southwind.entity.User; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { delete(); } public static void save(){ try { Connection connection = getConn(); //三、定義 SQL String sql = "insert into t_user(username,password,age) values('conn','000',18)"; //四、執行 SQL Statement statement = connection.createStatement(); int result = statement.executeUpdate(sql); System.out.println(result); statement.close(); connection.close(); } catch (SQLException e){ e.printStackTrace(); } } public static void find(){ try { Connection connection = getConn(); String sql = "select * from t_user where id = 2"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); User user = new User(); if(resultSet.next()){ long id = resultSet.getLong(1); String username = resultSet.getString(2); String password2 = resultSet.getString(3); int age = resultSet.getInt(4); user.setId(id); user.setUsername(username); user.setPassword(password2); user.setAge(age); } System.out.println(user); statement.close(); connection.close(); } catch (SQLException e){ e.printStackTrace(); } } public static void findAll(){ Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = getConn(); String sql = "select * from t_user"; statement = connection.createStatement(); resultSet = statement.executeQuery(sql); List<User> list = new ArrayList<>(); User user = null; while(resultSet.next()){ long id = resultSet.getLong(1); String username = resultSet.getString(2); String password2 = resultSet.getString(3); int age = resultSet.getInt(4); user = new User(id,username,password2,age); list.add(user); } for(User user1 :list){ System.out.println(user1); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if(connection != null){ connection.close(); } if(statement != null){ statement.close(); } if(resultSet != null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static void update(){ Connection connection = null; Statement statement = null; try { connection = getConn(); String sql = "update t_user set username = 'tom',password = '100',age = 16"; statement = connection.createStatement(); System.out.println(statement.executeUpdate(sql)); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(connection!=null){ connection.close(); } if(statement!=null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static void delete(){ Connection connection = null; Statement statement = null; try{ connection = getConn(); String sql = "delete from t_user where id = 2"; statement = connection.createStatement(); System.out.println(statement.executeUpdate(sql)); }catch (Exception e){ e.printStackTrace(); }finally { try { if(connection != null){ connection.close(); } if(statement != null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static Connection getConn(){ Connection connection = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mbtest?useUnicode=true&characterEncoding=UTF-8"; String user = "root"; String password = "root"; connection = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } } ``` 根據數據表建立相應的類,叫作實體類。 ```java package com.southwind.entity; public class User { private Long id; private String username; private String password; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } public User(Long id, String username, String password, Integer age) { this.id = id; this.username = username; this.password = password; this.age = age; } public User() { } } ``` ### 加載驅動 Class.forName("com.mysql.cj.jdbc.Driver"); 反射機制,獲取運行時類,什麼是運行時類?Java 程序是由類組成的,運行時,會將全部的類添加到 JVM 內存中,而且每一個類只有一份,保證在 JVM 內存中的類就叫作運行時類。 運行時類是一個動態概念,只有當程序運行的時候,纔有運行時類。 Statement 的方法,Statement 是經過 Connection 產生的,是用來執行 SQL 語句的,經常使用的方法: - ResultSet executeQuery(String sql) 用來執行查詢操做。 - int executeUpdate(String sql) 用來執行新增,修改,刪除操做。 - boolean execute(String sql) 能夠執行任意的 CRUD 操做。 true 表示返回結果是 ResultSet,執行的是查詢操做。 false 表示返回結果不是 ResultSet,執行的是新增、修改、刪除操做。
/entity/User.javajava
package com.southwind.entity; public class User { private Long id; private String username; private String password; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } public User(Long id, String username, String password, Integer age) { this.id = id; this.username = username; this.password = password; this.age = age; } public User() { } }
test/Test.javamysql
package com.southwind.test; import com.southwind.entity.User; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { test(); } public static void save(){ try { Connection connection = getConn(); //三、定義 SQL String sql = "insert into t_user(username,password,age) values('conn','000',18)"; //四、執行 SQL Statement statement = connection.createStatement(); int result = statement.executeUpdate(sql); System.out.println(result); statement.close(); connection.close(); } catch (SQLException e){ e.printStackTrace(); } } public static void find(){ try { Connection connection = getConn(); String sql = "select * from t_user where id = 2"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); User user = new User(); if(resultSet.next()){ long id = resultSet.getLong(1); String username = resultSet.getString(2); String password2 = resultSet.getString(3); int age = resultSet.getInt(4); user.setId(id); user.setUsername(username); user.setPassword(password2); user.setAge(age); } System.out.println(user); statement.close(); connection.close(); } catch (SQLException e){ e.printStackTrace(); } } public static void findAll(){ Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = getConn(); String sql = "select * from t_user"; statement = connection.createStatement(); resultSet = statement.executeQuery(sql); List<User> list = new ArrayList<>(); User user = null; while(resultSet.next()){ long id = resultSet.getLong(1); String username = resultSet.getString(2); String password2 = resultSet.getString(3); int age = resultSet.getInt(4); user = new User(id,username,password2,age); list.add(user); } for(User user1 :list){ System.out.println(user1); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if(connection != null){ connection.close(); } if(statement != null){ statement.close(); } if(resultSet != null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static void update(){ Connection connection = null; Statement statement = null; try { connection = getConn(); String sql = "update t_user set username = 'tom',password = '100',age = 16"; statement = connection.createStatement(); System.out.println(statement.executeUpdate(sql)); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(connection!=null){ connection.close(); } if(statement!=null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static void delete(){ Connection connection = null; Statement statement = null; try{ connection = getConn(); String sql = "delete from t_user where id = 2"; statement = connection.createStatement(); System.out.println(statement.executeUpdate(sql)); }catch (Exception e){ e.printStackTrace(); }finally { try { if(connection != null){ connection.close(); } if(statement != null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static Connection getConn(){ Connection connection = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mbtest?useUnicode=true&characterEncoding=UTF-8"; String user = "root"; String password = "root"; connection = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static void test(){ Connection connection = null; Statement statement = null; ResultSet resultSet = null; try{ connection = getConn(); // String sql = "select * from t_user where id = 3"; // String sql = "insert into t_user(username,password,age) values('aaa','111',22)"; // String sql = "update t_user set username = 'aa',password='11',age=11"; String sql = "delete from t_user"; statement = connection.createStatement(); boolean flag = statement.execute(sql); System.out.println(flag); }catch (Exception e){ e.printStackTrace(); }finally { try { if(connection!=null){ connection.close(); } if(statement!=null){ statement.close(); } if(resultSet!=null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }