JDBC的概述
数据的持久化
持久化(persistence):主要应用是将内存中的数据存储在关系型数据库中
JDBC的介绍
- JDBC(java database connectivity):是一个独立于特定数据库管理系统,通用的SQL数据库存取和操作的公共接口,(API(java.sql.javax.sql)一种标准的方法去访问数据库
 
获取数据库的连接
连接数据库方式一
package com.miao.connection; import org.junit.jupiter.api.Test; import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; public class ConnectionTest {
      @Test     public void test() throws SQLException {                  Driver driver = new com.mysql.cj.jdbc.Driver();         
 
 
 
 
          String url = "jdbc:mysql://localhost:3306/school";                  Properties info = new Properties();         info.setProperty("user","root");         info.setProperty("password","123456");
          Connection connection = driver.connect(url,info);         System.out.println(connection);     } }
 
   | 
 
方式二:连接数据库(迭代)
 @Test public void test2() throws Exception {          Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");     Driver driver = (Driver) clazz.newInstance();          String url = "jdbc:mysql://localhost:3306/school";          Properties info = new Properties();     info.setProperty("user","root");     info.setProperty("password","123456");          Connection connect = driver.connect(url, info);     System.out.println(connect); }
 
  | 
 
方式三
 @Test public void test3() throws Exception {          Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");     Driver driver = (Driver) clazz.newInstance();          String url = "jdbc:mysql://localhost:3306/school";     String user = "root";     String password = "123456";          DriverManager.registerDriver(driver);          Connection connection = DriverManager.getConnection(url, user, password);     System.out.println(connection); }
 
  | 
 
方式四
         @Test     public void test4() throws Exception {
                   String url = "jdbc:mysql://localhost:3306/school";         String user = "root";         String password = "123456";
                    Class.forName("com.mysql.cj.jdbc.Driver");
 
 
 
          
 
 
 
 
 
 
 
 
                   Connection connection = DriverManager.getConnection(url, user, password);         System.out.println(connection);     }
   | 
 
方式五
         @Test     public void test5() throws Exception {
          
 
 
                  File file = new File("src/main/resources/db.properties");         FileInputStream is = new FileInputStream(file);         Properties pros = new Properties();         pros.load(is);
          String url = pros.getProperty("url");         String user = pros.getProperty("user");         String password = pros.getProperty("password");         String driver = pros.getProperty("driver");                  Class.forName(driver);                  Connection connection = DriverManager.getConnection(url, user, password);         System.out.println(connection);     }
   | 
 
PreparedStatement操作数据库
普通操作数据库
@Test public void testInsert(){     Connection connection = null;     PreparedStatement ps = null;     try {                           InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("db.properties");         Properties properties = new Properties();         properties.load(is);         String user = properties.getProperty("user");         String password = properties.getProperty("password");         String url = properties.getProperty("url");         String driver = properties.getProperty("driver");         Class.forName(driver);         connection = DriverManager.getConnection(url, user, password);         System.out.println(connection);                  String sql = "insert into books(bookName,bookDetail) values(?,?)";         ps = connection.prepareStatement(sql);                  ps.setString(1,"动物世界");         ps.setString(2,"中国作家");                  ps.execute();     } catch (Exception e) {         e.printStackTrace();     }finally {         try {             if (connection != null){                                  connection.close();             }         } catch (SQLException e) {             e.printStackTrace();         }         try {             if (ps != null){                 ps.close();             }         } catch (SQLException e) {             e.printStackTrace();         }     } }
   | 
 
连接驱动
# 配置文件:连接数据库 # db.properties user=root password=123456 url=jdbc:mysql://localhost:3306/school driver=com.mysql.cj.jdbc.Driver
   | 
 
封装更新操作
工具类-JDBCUtil
public class JDBCUtil {
           public static Connection getConnection() throws Exception {                           InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("db.properties");         Properties properties = new Properties();         properties.load(is);
          String user = properties.getProperty("user");         String password = properties.getProperty("password");         String url = properties.getProperty("url");         String driver = properties.getProperty("driver");
          Class.forName(driver);         Connection conn = DriverManager.getConnection(url, user, password);
          return conn;     }          public static void closeResource(Connection con, Statement st){         try {             if (con != null){                                  con.close();             }         } catch (SQLException e) {             e.printStackTrace();         }         try {             if (st != null){                 st.close();             }         } catch (SQLException e) {             e.printStackTrace();         }     } }
  | 
 
更新操作封装
 public void update(String sql,Object ...args){     Connection conn = null;     PreparedStatement ps = null;     try {                  conn = JDBCUtil.getConnection();                  ps = conn.prepareStatement(sql);                  for (int i = 0; i < args.length; i++) {             ps.setObject(i+1,args[i]);         }                  ps.execute();     } catch (Exception e) {         e.printStackTrace();     }finally {                  JDBCUtil.closeResource(conn,ps);     } }
 
  | 
 
测试
@Test public void testCommon(){     String sql = "delete from books where bookId = ?";     update(sql,8); }
   | 
 
简单封装查询操作
         public Book queryForBook(String sql,Object ...args) {         Connection conn = null;         PreparedStatement ps = null;         ResultSet rs = null;         try {                          conn = JDBCUtil.getConnection();                          ps = conn.prepareStatement(sql);             for (int i = 0; i < args.length; i++) {                 ps.setObject(i+1,args[i]);             }             rs = ps.executeQuery();                          ResultSetMetaData rsmd = rs.getMetaData();                          int columnCount = rsmd.getColumnCount();             if (rs.next()){
                                   Book book = new Book();                                  for (int i = 0; i < columnCount; i++) {                                          Object columnValue = rs.getObject(i + 1);                     
                                                                String columnName = rsmd.getColumnName(i + 1);                                          Field field = Book.class.getDeclaredField(columnName);                     field.setAccessible(true);                     field.set(book,columnValue);                 }                 return book;             }         } catch (Exception e) {             e.printStackTrace();         }finally {             JDBCUtil.closeResource(conn,ps,rs);         }         return null;     }
   | 
 
测试
@Test public void testQuery() {     String sql = "select bookId,bookName,bookDetail from books where bookId = ?";     Book book = queryForBook(sql, 1);     System.out.println(book);
  }
   |