資料庫中資訊刪除
阿新 • • 發佈:2020-11-04
程式碼作用:刪除資料庫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");
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();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
執行截圖:
刪除資料之前:
刪除資料之後: