Java8下連線資料庫進行增刪改查(CRDU)操作
阿新 • • 發佈:2019-02-16
原始碼
步驟
1.獲取資料庫連線
- 此處直接用寫好的DBUtil操作,這裡檢視詳解
Connection conn = DBUtil.getConnection();
2.建立會話,有兩種方式
1.使用Statement建立(因為有注入問題,現今幾乎不使用)
Statement stmt = conn.createStatement();
2.使用PreparedStatement建立(常用)
PreparedStatement ps = conn.prepareStatement(sql);
3.進行具體的增刪改查操作
兩種情況:
1. 執行增刪改
注意:它的返回值是受影響的行數,我們通常用這個返回值來判斷對資料的增上哎是否成功
ps.executeUpdate();
- 執行查詢操作:需要用到以下語句:它相當於在資料庫中按下了執行sql語句的按鈕。
ps.executeQuery();
3.1書寫sql語句
分兩種情況:
1.步驟2用PreparedStatement建立的會話
2.步驟2用Statement建立的會話
由於Statement已經不怎麼使用,本文主要介紹第二種。
- 在Java中,sql中的問號表示佔位符,表示需要有引數來替代這個佔位符
- 佔位符可以設定值,用PreparedStatement的物件設定,假如要設定的值是int型別,應該這樣寫:
ps.setInt(1, id);
- 類似地,還能設定String、Double等其他型別:
ps.setString(2, name);
ps.setDouble(3, salary);
- 第一個引數代表第幾個佔位符(也就是第幾個問號),只能傳遞int型別
- 第二個引數代表要傳入的具體的值
1)增加
insert into employee values(?,?,?,?);
完整的新增過程:
int id = 5;
String name = "張三" ;
double salalry = 5000.00;
String gender = "男";
// sql 語句
String insertSql= insert into employee values(?,?,?,?);
ps.setInt(1, id);
ps.setString(2, name);
ps.setDouble(3, salary);
ps.setString(4, gender);
// 在資料庫中執行增加操作
int count = ps.executeUpdate();
return count;
2)修改
update employee set name=? where id =?
完整的修改過程:
int id = 5;
String name = "張三";
// sql 語句
String updateSql = "update employee set name=? where id =? ";
ps = conn.prepareStatement(sql);
// 設定第1個佔位符的欄位為傳遞過來的name
ps.setString(1, name);
// 設定第2個佔位符的值為傳遞過來的id
ps.setInt(2, 1);
// 在資料庫中執行修改操作
int count = ps.executeUpdate();
return count;
3)刪除
delete form employee where id = ?
完整的刪除過程:
int id = 5;
// sql 語句
String deleteSql = "delete form employee where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
// 在資料庫中執行刪除操作
int count = ps.executeUpdate();
return count;
4)查詢
在資料庫中查詢的結果需要使用Java中的ResultSet類將進行儲存。宣告如下:
ResultSet rs = ps.executeQuery();
查詢分兩種情況:
①簡單查詢
②帶引數查詢
Ⅰ.簡單查詢語句:
select * from employee
完整的查詢過程:
int id = 5;
// sql 語句
String selectSql = "select * from employee";
ps = conn.prepareStatement(sql);
// 在資料庫中執行查詢語句,並將結果(查詢到的多條記錄)賦值給rs
rs = ps.executeQuery();
// 輸出查詢結果的表頭
System.out.println("\tID\tName\tSlary\tGender");
// 將結果集的值取出,並迴圈輸出
while(rs.next()) {
id = rs.getInt("id");
name = rs.getString("name");
salary = rs.getDouble("salary");
gender = rs.getString("gender");
// 輸出查詢結果
System.out.println("\t"+id+"\t"+name+"\t"+salary+"\t"+gender);
}
Ⅰ.帶引數的查詢語句(以按id查詢為例,其他查詢以此類推):
select * from employee where id = ?
完整的查詢過程:
String selectSql1 = "select * from employee where id = ?";
ps = conn.prepareStatement(sql);
// 在資料庫中執行查詢語句,並將結果(查詢到的多條記錄)賦值給rs
rs = ps.executeQuery();
// 設定 id
ps.setInt(1, id);
// 輸出查詢結果的表頭
System.out.println("\tID\tName\tSlary\tGender");
// 將結果集的值取出,並迴圈輸出
while(rs.next()) {
id = rs.getInt("id");
name = rs.getString("name");
salary = rs.getDouble("salary");
gender = rs.getString("gender");
// 輸出查詢結果
System.out.println("\t"+id+"\t"+name+"\t"+salary+"\t"+gender);
}
完整工具類
這樣的操作應該封裝成一個工具類重複使用(程式碼重用原則)
package beans;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import utils.DBUtil;
public class CRUDDemo {
private Connection conn = DBUtil.getConnection();
private PreparedStatement ps;
private ResultSet rs;
// 宣告結果集中欄位
int id;
String name;
double salary;
String gender;
/*
* 新增
*/
public int insert(String sql,int id,String name,double salary,String gender) throws SQLException {
ps = conn.prepareStatement(sql);
// 設定引數
ps.setInt(1, id);
ps.setString(2, name);
ps.setDouble(3, salary);
ps.setString(4, gender);
// 在資料庫中執行增加操作
int count = ps.executeUpdate();
System.out.println("新增了"+count+"條記錄");
return count;
}
/*
* 修改
*/
public int update(String sql,String name,int id)throws SQLException {
ps = conn.prepareStatement(sql);
// 設定第1個佔位符的欄位為傳遞過來的name
ps.setString(1, name);
// 設定第2個佔位符的值為傳遞過來的id
ps.setInt(2, 1);
// 判斷資料是否更新成功
int count = ps.executeUpdate();
if(count!=0) {
System.out.println("資料修改成功!");
System.out.println("修改了了"+count+"條記錄");
}
return 0;
}
/*
* 刪除
*/
public int delete(String sql,int id) throws SQLException {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
int count = ps.executeUpdate();
System.out.println("刪除了"+count+"條記錄");
return 0;
}
/*
* 簡單查詢
*/
public void select(String sql)throws SQLException {
ps = conn.prepareStatement(sql);
// 在資料庫中執行查詢語句
rs = ps.executeQuery();
// 輸出查詢結果的表頭
System.out.println("\tID\tName\tSlary\tGender");
// 儲存結果集到Java變數中
while(rs.next()) {
id = rs.getInt("id");
name = rs.getString("name");
salary = rs.getDouble("salary");
gender = rs.getString("gender");
// 輸出查詢結果
System.out.println("\t"+id+"\t"+name+"\t"+salary+"\t"+gender);
}
}
/*
* 帶條件的查詢(以按id查詢為例,其他查詢以此類推)
*/
public void select(String sql,int id)throws SQLException {
ps = conn.prepareStatement(sql);
// 設定 id
ps.setInt(1, id);
// 在資料庫中執行查詢語句
rs = ps.executeQuery();
// 輸出查詢結果的表頭
System.out.println("\tID\tName\tSlary\tGender");
// 儲存結果集到Java變數中
while(rs.next()) {
id = rs.getInt("id");
name = rs.getString("name");
salary = rs.getDouble("salary");
gender = rs.getString("gender");
// 輸出查詢結果
System.out.println("\t"+id+"\t"+name+"\t"+salary+"\t"+gender);
}
}
}
測試類
package test;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import beans.CRUDDemo;
class TestCRUDDemo {
CRUDDemo cd = new CRUDDemo();
@Test
void testInsert() throws SQLException {
// 測試增加
String insertSql = "insert into employee values(?,?,?,?)";
cd.insert(insertSql,3,"謝春花",12000.00,"女");
}
@Test
void testUpdate() throws SQLException {
// 測試修改(更新),假設更新姓名,更新其他欄位類似
String updateSql = "update employee set name=? where id =? ";
cd.update(updateSql, "銀臨", 3);
}
@Test
void testDelete() throws SQLException {
// 測試刪除
String deleteSql = "delete from employee where id = ?";
cd.delete(deleteSql, 3);
}
@Test
void testSelectString() throws SQLException {
// 測試查詢表中所有資料
String selectSql = "select * from employee";
cd.select(selectSql);
}
@Test
void testSelectStringInt() throws SQLException {
// 測試帶條件的查詢(以按id查詢為例,其他查詢以此類推)
String selectSql1 = "select * from employee where id = ?";
cd.select(selectSql1,1);
}
}