1. 程式人生 > 資料庫 >資料庫中資訊刪除

資料庫中資訊刪除

程式碼作用:刪除資料庫db中表tb_stu裡出生日期在2001-05-01之前的學生

程式碼示例:

import java.sql.*;

public class DeleteStu {
static Connection con;
static PreparedStatement sql;
static ResultSet res;
public Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useSSL=false&serverTimezone=GMT","root","020714");

} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
DeleteStu c = new DeleteStu();
con = c.getConnection();
try {
sql = con.prepareStatement("delete from tb_stu where birthday < ?");
sql.setString(1, "2001-05-01");//刪除出生日期在2001-05-01之前的學生
sql.executeUpdate();
System.out.println("資料刪除完畢");
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}

}
}

執行截圖

刪除資料之前:

 

 

 

刪除資料之後: