登录操作:
Scanner scanner = new Scanner(System.in); System.out.println("用户名"); String username = scanner.next(); System.out.println("密码"); String password = scanner.next(); String find_username_password_sql = "select * from student where username=? and password=?"; Connection connection = jdbcutils.getConnection(); PreparedStatement statement = connection.prepareStatement(find_username_password_sql); statement.setString(1, username); statement.setString(2, password); ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { System.out.println("登录成功"); } else { System.out.println("登录失败"); } connection.close(); statement.close(); resultSet.close(); } 添加操作: public void addtext() throws ClassNotFoundException, SQLException { Scanner scanner1 = new Scanner(System.in); System.out.println("id:"); int id = scanner1.nextInt(); System.out.println("name:"); String name = scanner1.next(); System.out.println("price:"); int price = scanner1.nextInt(); System.out.println("shu:"); int shu = scanner1.nextInt(); String addsql = "insert into shang (id,name,price,shu) values (?,?,?,?)"; Connection connecxtion = jdbcutils.getConnection(); PreparedStatement statement1 = connecxtion.prepareStatement(addsql); statement1.setInt(1, id); statement1.setString(2, name); statement1.setInt(3, price); statement1.setInt(4, shu); statement1.executeUpdate(); jdbcutils.clo(null, statement1, connecxtion); } 删除操作: public static void shan() throws SQLException, ClassNotFoundException { Scanner scanner = new Scanner(System.in); System.out.println("id:"); int id = scanner.nextInt(); String shansql = "delete from student where id = ?"; Connection connection = jdbcutils.getConnection(); PreparedStatement statement = connection.prepareStatement(shansql); statement.setInt(1, id); statement.executeUpdate(); jdbcutils.clo(null, statement, connection); } 更改操作: public static void gai() throws SQLException, ClassNotFoundException { Scanner scanner1 = new Scanner(System.in); System.out.println("name:"); String name = scanner1.next(); System.out.println("price:"); int price = scanner1.nextInt(); System.out.println("shu:"); int shu = scanner1.nextInt(); System.out.println("id:"); int id = scanner1.nextInt(); String gaisql = "update shang set name = ? ,price = ?, shu = ? where id = ? "; Connection connection1 = jdbcutils.getConnection(); PreparedStatement statement1 = connection1.prepareStatement(gaisql); statement1.setString(1, name); statement1.setInt(2, price); statement1.setInt(3, shu); statement1.setInt(4, id); statement1.executeUpdate(); jdbcutils.clo(null, statement1, connection1); }