zoukankan      html  css  js  c++  java
  • Jaba_Web--JDBC 删除记录操作模板

    import java.sql.Connection;
    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();
    
    	}
    
    }
    
  • 相关阅读:
    JavaScript基础
    w3c网站案例
    CSS基础
    HTML基础
    MySQL--用户管理 pymysql 索引
    MySQL--高级
    MySQL--多表查询
    MySQL--单表查询
    直接插入排序与折半插入排序分析
    Nginx从安装到简单使用
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798294.html
Copyright © 2011-2022 走看看