获取用户输入的用户和密码,然后使用封装后的jdbc来查库进行验证用户名和密码是否正确
public class Test02 { // 获取用户输入的用户和密码,然后使用封装后的jdbc来查库进行验证用户名和密码是否正确 public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection coon = DriverManager.getConnection( "jdbc:mysql://localhost:3306/db4?useSSL=false&useUnicode=true&characterEncoding=UTF-8", "root", "123456"); Scanner scanner = new Scanner(System.in); System.out.print("请输入用用户名:"); String name = scanner.next(); System.out.print("请输入密码:"); int pwd = scanner.nextInt(); String sql = "select * from admin where name ='" + name +"' and pwd = " + pwd ; Statement statement = coon.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()){ System.out.println("密码输入正确"); }else { System.out.println("输入错误"); } resultSet.close(); statement.close(); coon.close(); } }

封装jdbc然后 改造
public class JdbcUtils { private static String url; private static String user; private static String pwd; private static String driver; static { ClassLoader classLoader = JdbcUtils.class.getClassLoader(); Properties properties = new Properties(); try { properties.load(classLoader.getResourceAsStream("jdbc.properties")); url = properties.getProperty("url"); user = properties.getProperty("user"); pwd = properties.getProperty("pwd"); driver = properties.getProperty("driver"); } catch (IOException e) { e.printStackTrace(); } } //获得连接对象 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,pwd); } //关闭连接 public static void close(ResultSet resultSet, Statement statement,Connection conn){ try { if (resultSet != null){ resultSet.close(); } if (statement != null){ statement.close(); } if (conn != null){ conn.close(); } }catch (SQLException e){ e.printStackTrace(); } } public static void close(Statement statement,Connection conn){ try { if (statement != null){ statement.close(); } if (conn != null){ conn.close(); } }catch (SQLException e){ e.printStackTrace(); } } }
url=jdbc:mysql://localhost:3306/db4?useSSL=false&useUnicode=true&characterEncoding=utf-8 user=root pwd=123456 driver=com.mysql.jdbc.Driver
public class Test02 { public static void main(String[] args) throws SQLException { Scanner scanner = new Scanner(System.in); System.out.print("请输入用户名:"); String name = scanner.next(); System.out.print("请输入密码:"); int pwd = scanner.nextInt(); Connection conn = JdbcUtils.getConnection(); Statement statement = conn.createStatement(); String sql = "select * from admin where name ='" + name +"' and pwd = " + pwd ; ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()){ System.out.println("密码正确"); }else { System.out.println("密码错误"); } JdbcUtils.close(statement,conn); } }

浙公网安备 33010602011771号