2022-11-05
一、自增長的鍵值問題
1、說明:
將在資料庫表單中添加資料的自增長的欄位回傳給用戶
2、使用方式:
在預編譯陳述句中,除了要傳入sql陳述句外,還要傳入一個引數“Statement.RETURN_GENERATED_KEYS”,其中“Statement”表示的是一個介面,“RETURN_GENERATED_KEYS”表示在執行sql陳述句的同時,回傳自增長的鍵值對,
之后,輸出自增長鍵值對使用的方式是“先創建一個預編譯物件的自增長結果集(如:rs)使用的陳述句是“
ResultSet rs = pst.getGeneratedKeys();
”,之后輸出陳述句為自增長結果集物件的getObject(放要獲取的列數編號)”,注意,要將輸出的陳述句放置到判斷獲得的結果集是否有下一個的判斷陳述句中,例如:if(rs.next()){陳述句放置位置},其中rs表示的是結果集的物件,使用的陳述句是
ResultSet rs = pst.getGeneratedKeys();
if(rs.next()){
System.out.println("您的員工編號是" + rs.getObject(1));
}
package com.haha.problem; import org.junit.Test; import java.sql.*; import java.util.Date; import java.util.Scanner; public class Problem4 { @Test public void test04() throws SQLException { Scanner input = new Scanner(System.in); System.out.println("請輸入姓名:"); String ename = input.next(); System.out.println("請輸入薪資:"); double salary = input.nextDouble(); System.out.println("請輸入出生日期:"); String birthday = input.next(); System.out.println("請輸入性別:"); String gender = input.next(); System.out.println("請輸入手機號碼:"); String tel = input.next(); System.out.println("請輸入郵箱:"); String email = input.next(); input.close(); String url = "jdbc:mysql://localhost:3306/資料庫名稱?serverTimezone=UTC"; Connection conn = DriverManager.getConnection(url, "資料庫用戶名", "資料庫密碼"); String sql = "INSERT INTO t_employee(ename,salary,birthday,gender,tel,email,hiredate) VALUES(?,?,?,?,?,?,?)"; PreparedStatement pst = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pst.setObject(1,ename); pst.setObject(2,salary); pst.setObject(3,birthday); pst.setObject(4,gender); pst.setObject(5,tel); pst.setObject(6,email); pst.setObject(7,new Date()); int len = pst.executeUpdate(); System.out.println(len>0?"添加成功":"添加失敗"); ResultSet rs = pst.getGeneratedKeys(); if(rs.next()){ System.out.println("您的員工編號是" + rs.getObject(1)); } pst.close(); conn.close(); } }
二、批處理
1、說明:
批處理處理一般用于大批量的輸入資料中使用,批處理中不是設定了一條資料就網資料庫中添加一條資料,而是將添加的一些資料放置到緩沖區中,添加完后再放置到資料庫中,如果緩沖池中滿了以后,就會自動將資料放置到資料庫中,清慷訓沖池,
2、使用實體:
設定url(此處URL要多加一個引數,添加的引數是
rewriteBatchedStatements=true
,因為MySQL中batch沒有自動開啟,所以要告訴資料庫開啟緩沖),設定驅動管理類的連接,設定sql陳述句,設定連接物件的預編譯,設定占位符的取值(將預編譯物件先放置到緩沖池中),執行批處理,關閉流物件(連接物件、預編譯物件)
3、代碼實體:
package com.haha.problem; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class Problem5 { @Test public void test05() throws SQLException { long start = System.currentTimeMillis(); String url = "jdbc:mysql://localhost:3306/資料庫名稱?serverTimezone=UTC&rewriteBatchedStatements=true"; Connection conn = DriverManager.getConnection(url, "資料庫用戶名", "資料庫密碼"); String sql = "INSERT INTO t_department VALUES(NULL,?,?)"; PreparedStatement pst = conn.prepareStatement(sql); for(int i=1;i<=1000;i++){ pst.setObject(1,"測驗部門名稱"+i); pst.setObject(2,"測驗部門簡介"+i); pst.addBatch(); } pst.executeBatch(); pst.close(); conn.close(); long end = System.currentTimeMillis(); System.out.println("耗時:"+(end - start)); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/528037.html
標籤:其他
