代码作用:删除数据库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();
}
}
}
运行截图:
删除数据之前:
删除数据之后: