1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| * 目的:简化书写 * 分析: 1. 注册驱动也抽取 2. 抽取一个方法获取连接对象 * 需求:不想传递参数(麻烦),还得保证工具类的通用性。 * 解决:配置文件 jdbc.properties url= user= password= 3. 抽取一个方法释放资源
* 代码实现: public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver;
static{
try { Properties pro = new Properties();
ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL res = classLoader.getResource("jdbc.properties"); String path = res.getPath(); System.out.println(path); pro.load(new FileReader(path));
url = pro.getProperty("url"); user = pro.getProperty("user"); 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, user, password); }
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(); } } }
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(); } } }
}
* 练习: * 需求: 1. 通过键盘录入用户名和密码 2. 判断用户是否登录成功 * select * from user where username = "" and password = ""; * 如果这个sql有查询结果,则成功,反之,则失败
* 步骤: 1. 创建数据库表 user CREATE TABLE USER( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(32), PASSWORD VARCHAR(32)
);
INSERT INTO USER VALUES(NULL,'zhangsan','123'); INSERT INTO USER VALUES(NULL,'lisi','234');
2. 代码实现: public class JDBCDemo9 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String username = sc.nextLine(); System.out.println("请输入密码:"); String password = sc.nextLine(); boolean flag = new JDBCDemo9().login(username, password); 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; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql = "select * from user where username = '"+username+"' and password = '"+password+"' "; stmt = conn.createStatement(); rs = stmt.executeQuery(sql);
return rs.next();
} catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtils.close(rs,stmt,conn); } return false; } }
|