import java.sql.Connection;标签:JDBC,java,String,删除,模板,sql,println,import,conn From: https://blog.51cto.com/u_14682436/6057431
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class Delete {
public static void main(String[] args) throws Exception {
String driverName = "com.mysql.cj.jdbc.Driver"; // 驱动程序名
String userName = "root"; // 数据库用户名
String userPwd = null; // 密码
String dbName = "students"; // 数据库名
String url = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&serverTimezone=UTC"; // 形成带数据库读写编码的数据库连接字
Class.forName(driverName); // 加载并注册驱动程序
Connection conn = DriverManager.getConnection(url, userName, userPwd);// 创建连接对象
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement stmt = conn.createStatement();// 在桥conn上直接创建一辆汽车
String sql = "delete from stu where cj<?"; //用占位符设置SQL操作的模板
PreparedStatement pstmt = conn.prepareStatement(sql); //预处理相关SQL语句
int n = stmt.executeUpdate(sql);// 返回记录操作条数
// pstmt.setInt(1,60);
if (n > 0) {
System.out.println("删除记录成功,共删除了" + n + "条记录");
} else {
System.out.println("删除不成功!");
}
pstmt.close();
stmt.close();
conn.close();
}
}