1. JDBC介紹 JDBC(Java DataBase Connectivity),即Java資料庫的連接。JDBC是一種用於執行SQL語句(DML,DDL,DQL)的Java API,可以為多種關係資料庫(oracle,mysql,sqlserver)提供統一訪問,它由一組用Java語言編寫的類 ...
1. JDBC介紹
JDBC(Java DataBase Connectivity),即Java資料庫的連接。JDBC是一種用於執行SQL語句(DML,DDL,DQL)的Java API,可以為多種關係資料庫(oracle,mysql,sqlserver)提供統一訪問,它由一組用Java語言編寫的類和介面組成。JDBC提供了一種基準,據此可以構建更高級的工具和介面,使資料庫開發人員能夠編寫資料庫應用程式。
Java 通過JDBC訪問資料庫,資料庫廠家有很多,如Mysql、Oracle、SqlServer等,而JDBC提供了操作這些資料庫的統一的介面、基準,讓各大廠商去實現這組規範。各大資料庫廠商只需要提供對應的JDBC的jar包,使java代碼通過不同廠商實現的jar包訪問對應的資料庫。
2. JDBC完成CRUD規範步驟
2.1.規範DAO層的實現
DAO層:涉及到開發中的三層架構與MVC層級結構。DAO層叫數據訪問層,全稱data access object,屬於一種比較底層、比較基礎的操作,具體作用於某個表、某個實體的增刪改查。
DAO層的創建規範:
這裡包括界面層傳入數據封裝成為的域對象——Domain類對象。
2.2. DAO層具體實現
具體代碼如下:
DAO層:
介面:
1 public interface IStudentDao { 2 3 public void insert(Student student); 4 5 public void update(Student student); 6 7 public void delete(Integer id); 8 9 public Student queryOne(Integer id); 10 11 public List<Student> queryAll(); 12 }
實現類Impl:
1 package cn.yif.dao.impl; 2 3 import cn.yif.domain.Student; 4 import cn.yif.dao.IStudentDao; 5 import cn.yif.utils.JDBCUtil; 6 7 import java.sql.*; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 /** 12 * @author Administrator 13 * @create 2019-09-13-20:33 14 */ 15 public class StudentDaoImpl implements IStudentDao { 16 @Override 17 public void insert(Student student) { 18 Connection conn = null; 19 Statement statement = null; 20 try { 21 //①載入(註冊)驅動,這部分硬編碼可以寫也可以不寫,jdbc4.0版本自帶了 22 //Class.forName("com.mysql.jdbc.Driver"); 23 //②獲取連接 24 //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest01", "root", "admin"); 25 conn = JDBCUtil.getInstance().getConnection(); 26 //③獲取語句對象:用來執行SQL語句 27 statement = conn.createStatement(); 28 //④執行SQL語句 29 String insertSql = "insert into jdbcdemo01 (name, age) values ('"+student.getName()+"', "+student.getAge()+")"; 30 statement.execute(insertSql); 31 }catch (Exception ex){ 32 ex.printStackTrace(); 33 }finally { 34 //⑤釋放資源 35 JDBCUtil.getInstance().close(null, statement, conn); 36 } 37 } 38 39 @Override 40 public void update(Student student) { 41 Connection conn = null; 42 Statement statement = null; 43 try { 44 //①載入(註冊)驅動 45 //Class.forName("com.mysql.jdbc.Driver"); 46 //②獲取連接 47 //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest01", "root", "admin"); 48 conn = JDBCUtil.getInstance().getConnection(); 49 //③獲取語句對象:用來執行SQL語句 50 statement = conn.createStatement(); 51 //④執行SQL語句 52 String updateSql = "update jdbcdemo01 set name = '"+student.getName()+"', age = "+student.getAge()+" where id = "+student.getId()+""; 53 statement.execute(updateSql); 54 }catch (Exception ex){ 55 ex.printStackTrace(); 56 }finally { 57 //⑤釋放資源 58 JDBCUtil.getInstance().close(null, statement, conn); 59 } 60 } 61 62 @Override 63 public void delete(Integer id) { 64 Connection conn = null; 65 Statement statement = null; 66 try { 67 //①載入(註冊)驅動 68 //Class.forName("com.mysql.jdbc.Driver"); 69 //②獲取連接 70 //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest01", "root", "admin"); 71 conn = JDBCUtil.getInstance().getConnection(); 72 //③獲取語句對象:用來執行SQL語句 73 statement = conn.createStatement(); 74 //④執行SQL語句 75 String deleteSql = "delete from jdbcdemo01 where id = "+ id +""; 76 statement.execute(deleteSql); 77 }catch (Exception ex){ 78 ex.printStackTrace(); 79 }finally { 80 //⑤釋放資源 81 JDBCUtil.getInstance().close(null, statement, conn); 82 } 83 } 84 85 @Override 86 public Student queryOne(Integer id) { 87 Connection conn = null; 88 Statement statement = null; 89 ResultSet resultSet = null; 90 Student student = null; 91 try { 92 //①載入(註冊)驅動 93 //Class.forName("com.mysql.jdbc.Driver"); 94 //②獲取連接 95 //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest01", "root", "admin"); 96 conn = JDBCUtil.getInstance().getConnection(); 97 //③獲取語句對象:用來執行SQL語句 98 statement = conn.createStatement(); 99 //④執行SQL語句 100 String queryOneSql = "select * from jdbcdemo01 where id = "+ id +""; 101 resultSet = statement.executeQuery(queryOneSql); 102 if(resultSet.next()) 103 { 104 student = new Student(); 105 student.setId(resultSet.getInt("id")); 106 student.setName(resultSet.getString("name")); 107 student.setAge(resultSet.getInt("age")); 108 } 109 }catch (Exception ex){ 110 111 }finally { 112 //⑤釋放資源 113 JDBCUtil.getInstance().close(resultSet, statement, conn); 114 } 115 return student; 116 } 117 118 @Override 119 public List<Student> queryAll() { 120 Connection conn = null; 121 Statement statement = null; 122 ResultSet resultSet = null; 123 Student student = null; 124 List<Student> list = new ArrayList<Student>(); 125 try { 126 //①載入(註冊)驅動 127 //Class.forName("com.mysql.jdbc.Driver"); 128 //②獲取連接 129 //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest01", "root", "admin"); 130 conn = JDBCUtil.getInstance().getConnection(); 131 //③獲取語句對象:用來執行SQL語句 132 statement = conn.createStatement(); 133 //④執行SQL語句 134 String queryAllSql = "select * from jdbcdemo01"; 135 resultSet = statement.executeQuery(queryAllSql); 136 while (resultSet.next()) 137 { 138 student = new Student(); 139 student.setId(resultSet.getInt("id")); 140 student.setName(resultSet.getString("name")); 141 student.setAge(resultSet.getInt("age")); 142 list.add(student); 143 } 144 }catch (Exception ex){ 145 ex.printStackTrace(); 146 }finally { 147 //⑤釋放資源 148 JDBCUtil.getInstance().close(resultSet, statement, conn); 149 } 150 return list; 151 } 152 }
Domain層:
1 package cn.yif.domain; 2 3 /** 4 * @author Administrator 5 * @create 2019-09-13-20:23 6 */ 7 public class Student { 8 //ID表示編號 9 private Integer id; 10 11 //name表示名字 12 private String name; 13 14 //age表示年齡 15 private Integer age; 16 17 public Integer getId() { 18 return id; 19 } 20 21 public void setId(Integer id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public Integer getAge() { 34 return age; 35 } 36 37 public void setAge(Integer age) { 38 this.age = age; 39 } 40 41 public Student() { 42 } 43 44 public Student(Integer id, String name, Integer age) { 45 this.id = id; 46 this.name = name; 47 this.age = age; 48 } 49 50 @Override 51 public String toString() { 52 return "Student{" + 53 "id=" + id + 54 ", name='" + name + '\'' + 55 ", age=" + age + 56 '}'; 57 } 58 }
工具類JDBCUtil實現:
1 package cn.yif.utils; 2 3 import java.io.IOException; 4 import java.sql.*; 5 import java.util.Properties; 6 7 /** 8 * @author Administrator 9 * @create 2019-09-13-21:21 10 */ 11 public class JDBCUtil { 12 //使用靜態static類構造單例模式 13 private JDBCUtil(){ } 14 //在靜態代碼塊中創建instance的實例,這裡使用static靜態變數來保存 15 private static JDBCUtil instance = null; 16 //每次都要載入驅動,這裡定義一個Properties,把資源文件裡面的內容讀到Properties裡面 17 private static Properties properties = null; 18 //JDBCUtil類載入的時候,就載入註冊驅動 19 static { 20 properties = new Properties(); 21 try { 22 //當前線程類載入器載入獲取properties文件 23 properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties")); 24 try { 25 Class.forName(properties.getProperty("driverClassName")); 26 } catch (ClassNotFoundException e) { 27 e.printStackTrace(); 28 } 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 33 instance = new JDBCUtil(); 34 } 35 36 public static JDBCUtil getInstance(){ 37 return instance; 38 } 39 40 //抽取獲取連接Connection的方法 41 public Connection getConnection() throws SQLException { 42 return DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("username"), properties.getProperty("password")); 43 } 44 45 //抽取JDBC關閉資源的close方法 46 public void close(ResultSet resultSet, Statement statement, Connection conn){ 47 try { 48 if(resultSet != null){ 49 resultSet.close(); 50 } 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 try { 55 if(statement != null){ 56 statement.close(); 57 } 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 try { 62 if(conn != null){ 63 conn.close(); 64 } 65 } catch (SQLException e) { 66 e.printStackTrace(); 67 } 68 } 69 }
JDBC資源配置文件:
1 driverClassName = com.mysql.jdbc.Driver 2 url = jdbc:mysql://localhost:3306/jdbctest01 3 username = root 4 password = admin
或GitHub:https://github.com/devyf/JavaWorkSpace/tree/master/JDBC_0913_01