Override public User queryUserByUnameAndPwdUser formUser try 获取连接 Connection conn = JdbcUtil2getConnection; 准备sql模板 String sql = select from
@Override public User queryUserByUnameAndPwd(User formUser) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { // 获取连接 conn = JdbcUtil2.getConnection(); // 准备sql模板 String sql = "select * from user where uname=? and pwd=?"; // 获取数据库执行对象 ps = conn.prepareStatement(sql); // 给问号赋值 ps.setString(1, formUser.getUname()); ps.setString(2, formUser.getPwd()); // 执行查询 rs = ps.executeQuery(); if (rs.next()) { //把结果集中的数据封装到user对象中 User user = new User(); user.setId(rs.getInt("id")); user.setUname(rs.getString("uname")); user.setPwd(rs.getString("pwd")); return user; } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } return null;
原文地址: http://www.cveoy.top/t/topic/hv4N 著作权归作者所有。请勿转载和采集!