📄 userdaoimpl_jdbc.java
字号:
package demo;
import java.sql.*;
public class UserDaoImpl_Jdbc implements UserDaoIf{
//根据用户名查找用户
public User findUserByName(String userName){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
con = getConnection();
String sql = "select * from ajax_user where username = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1,userName);
rs = pstmt.executeQuery();
if(rs.next()){
User user = new User();
user.setUserId(rs.getInt(1));
user.setUserName(rs.getString(2));
user.setPassWord(rs.getString(3));
return user;
}else{
return null;
}
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
close(con,pstmt,rs);
}
}
//封装了获取连接的动作
public Connection getConnection(){
ConnectionFactory factory = ConnectionFactory.getInstance();
return factory.getConnection();
}
//释放资源
public void close(Connection con,PreparedStatement pstmt,ResultSet res){
try{
if(con != null) con.close();
if(pstmt != null) pstmt.close();
if(res != null) res.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -