获取用户输入的用户和密码,然后使用封装后的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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)