package JDBCUtils; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; public class jdbcUtils { private static String url; private static String userName; private static String passWord; private static String driver; /** * 读取配置文件,通过静态代码块的方式来注册驱动,获取sql连接对象,保证程序中只执行一次 */ static { try { Properties pro = new Properties(); //获取src路径下文件的方式-->Classloader类加载器 ClassLoader jdbcCls = jdbcUtils.class.getClassLoader(); URL resource = jdbcCls.getResource("jdbc.properties"); //获取配置文件的全路径 String path = resource.getPath(); //读取配置文件 pro.load(new FileReader(path)); url = pro.getProperty("url"); userName = pro.getProperty("userName"); passWord = pro.getProperty("passWord"); //加载驱动 driver = pro.getProperty("driver"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,userName,passWord); } /** * 释放资源 * @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(); } } } /** * 通过重载的形式,释放资源 * @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(); } } } }
jdbc.properties
url=jdbc:mysql://localhost:3306/db4 userName=feifei passWord = 320419 driver = com.mysql.jdbc.Driver
测试代码
package feifei; import JDBCUtils.jdbcUtils; import org.junit.Test; import java.sql.*; import java.util.Scanner; public class jdbcDemo5 { public static void main(String[] args){ //创建键盘输入流 Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String name = sc.nextLine(); System.out.println("请输入密码:"); String password = sc.nextLine(); jdbcDemo5 j5 = new jdbcDemo5(); //判断是否登录成功 boolean flag = j5.register(name, password); if(flag){ System.out.println("登录成功"); }else{ System.out.println("用户名或密码错误"); } } public boolean register(String name,String password){ Connection conn=null; ResultSet rs = null; Statement stmt = null; PreparedStatement psmt = null; try { if(name==null||password==null){ return false; } conn = jdbcUtils.getConnection(); //String sql = "select * from user where username='"+name+"' and password ='"+password+"'"; //stmt = conn.createStatement(); //rs = stmt.executeQuery(sql); String sql = "select * from user where username=? and password =?"; //preparedStatement 可以防止sql注入 psmt = conn.prepareStatement(sql); /** * psmt.setXxx(int parameterIndex, String x) * 参数1:表示占位符的位置,从1开始 * 参数2:表示占位符的值 * Xxx代表参数类型 */ psmt.setString(1,name); psmt.setString(2,password); rs = psmt.executeQuery(); return rs.next(); } catch (SQLException e) { e.printStackTrace(); }finally { jdbcUtils.close(rs,stmt,conn); } return false; } }