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();
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 = "select * from stu where cj>=0 and cj<=100"; PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();// 执行,得到查询结果集合 System.out.println("记录号 学号 姓名 成绩"); while (rs.next()) { int a = rs.getRow(); int b = rs.getInt("xh"); String c = rs.getString("name"); int d = rs.getInt("cj"); System.out.println(a + " | " + b + " | " + c + " | " + d); }
Class.forName(driverName); // 加载并注册驱动程序 Connectionconn= DriverManager.getConnection(url, userName, userPwd);// 创建连接对象 if (!conn.isClosed()) System.out.println("Succeeded connecting to the Database!"); Statementstmt= conn.createStatement();// 在桥conn上直接创建一辆汽车
// 更新(添加、删除、修改)数据库操作 Stringsql="update stu set cj=? where xh=3"; PreparedStatementpstmt= conn.prepareStatement(sql); intn= stmt.executeUpdate(sql);// 返回记录操作条数
// pstmt.setInt(1,50); // pstmt.setInt(2,3);
if (n > 0) { System.out.println("修改记录成功,共修改了" + n + "条记录"); } else { System.out.println("修改不成功!"); } pstmt.close(); stmt.close(); conn.close();