JDBC用户登录案例
登录数据库:
package cn.chunzhi.jdbc; import cn.chunzhi.util.JDBCUtils; import java.sql.*; import java.util.Scanner; /** * 练习: * 1.通过键盘录入用户名和密码 * 2.判断是否登陆成功 */ public class Test09_JdbcLoginPreparedStatement { public static void main(String[] args) { // 1.键盘输入,接收用户名和密码 Scanner sc = new Scanner(System.in); System.out.println("请输入用户昵称:"); String username = sc.nextLine(); System.out.println("请输入用户密码:"); String password = sc.nextLine(); // 2.调用方法 boolean flag = new Test09_JdbcLoginPreparedStatement().login(username, password); // 3.判断结果 if (flag) { System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!!"); } } public boolean login(String username, String password) { if (username == null || password == null) { return false; } // 连接数据库判断是否成功 Connection conn = null; //Statement stmt = null; PreparedStatement pstmt = null; ResultSet rs = null; // 1.获取连接 try { conn = JDBCUtils.getConnection(); // 2.定义sql // String sql = "select * from user where username = '"+username+"' and password = '"+password+"' "; String sql = "select * from user where username = ? and password = ?"; // 3.获取执行sql语句的对象 //stmt = conn.createStatement(); pstmt = conn.prepareStatement(sql); // 给 ?赋值 pstmt.setString(1,username); pstmt.setString(2,password); // 4.执行查询,不需要传递sql //rs = stmt.executeQuery(sql); rs = pstmt.executeQuery(); // 5.判断 return rs.next(); // 如果有下一行返回true。判断当前行是否是最后一行的末尾。 } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.close(rs,pstmt,conn); } return false; } }
获取连接对象和释放资源封装类:
package cn.chunzhi.util; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; /** * JDBC工具类 */ public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver; /** * 文件的读取,只需要读取一次即可拿到这些值。使用静态代码块(静态代码块随着Class加载而加载,只执行一次) */ static { // 读取资源文件获取值 try { // 1.创建Properties集合类 // Properties集合是一个唯一和IO流相结合的集合。 Properties prop = new Properties(); // 获取src路径下的文件的方式 --> ClassLoader:类加载器 ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL resource = classLoader.getResource("jdbc.properties"); String path = resource.getPath(); //System.out.println("获取的是绝对路径:"+ path); // 获取的是绝对路径 // 2.加载文件 prop.load(new FileReader(path)); //prop.load(new FileReader("C:\\Other\\IdeaProjects\\jdbc\\day04_jdbc\\src\\jdbc.properties")); // 3.获取数据赋值 JDBCUtils.url = prop.getProperty("url"); user = prop.getProperty("user"); password = prop.getProperty("password"); driver = prop.getProperty("driver"); // 4.注册驱动 Class.forName(driver); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } /** * 获取连接 * @return 连接对象 */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } /** * 释放资源 * @param stmt * @param conn */ public static void close(Statement stmt, Connection conn) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 释放资源 * @param rs * @param stmt * @param conn */ public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
配置文件:
url = jdbc:mysql://localhost:3305/db3 user = root password = root driver = com.mysql.jdbc.Driver