publicclassTestPreparedStatement1{ publicstaticvoidmain(String[] args)throws Exception { Scanner input = new Scanner(System.in); System.out.println("请输入待修改的客户编号:"); int id = input.nextInt(); System.out.println("请输入新的客户姓名:"); String name = input.next();
publicclassTestPrepasredStatementByUtils{ publicstaticvoidmain(String[] args)throws Exception{ Scanner input = new Scanner(System.in); System.out.println("请输入待修改的客户编号:"); int id = input.nextInt(); System.out.println("请输入新的客户姓名:"); String name = input.next();
//----------------------连接数据库的步骤---------------- //1.获取连接 Connection connection = JDBCUtils.getConnection(); //2.执行修改 String sql = "UPDATE customers SET name = ? WHERE id = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, name); statement.setInt(2, id); int i = statement.executeUpdate();//执行增删改,返回受影响的行数 System.out.println(i > 0 ? "修改成功!" : "修改失败!");
publicstaticvoidclose(ResultSet set, Statement statement,Connection connection){ try { if (set != null) set.close(); if (statement != null) statement.close(); if (connection != null) connection.close(); } catch (SQLException e){ thrownew RuntimeException(e); } }
最终代码为:
publicclassTestPrepasredStatementByUtils{ publicstaticvoidmain(String[] args){ Scanner input = new Scanner(System.in); System.out.println("请输入待修改的客户编号:"); int id = input.nextInt(); System.out.println("请输入新的客户姓名:"); String name = input.next();