📄 userdaoimpl_jdbc.java
字号:
package demo;
import java.sql.*;
public class UserDaoImpl_Jdbc implements UserDaoIf{
public boolean checkUser(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);
re=pstmt.executeQuery();
if(rs.next())
{
return true;
}else{
return false;
}
}catch(Exception e)
{
e.printStackTrace();
return false;
}finally{
close(con,pstmt,rs);
}
}
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.prepareStatment(sql);
pstmt.setString(1,userName);
rs = pstmt.executeQuery();
if(rs.next()){
User user=new User();
user.setUserId(rs.getInt(1));
user.setUserName(rs.getStrng(2));
user.setPassWord(rs.getString(3));
user.setGender(rs.getString(4));
return user;
}else{
return null;
}
}catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public Connection getConnection(){
Class.forName("com.mysql.jdbc.Driver");
return
DriverManager.getConnection("jdbc:mysql://localhost:3306/camp","root","root");
}
public void close(Connection con,PreparedStatement pstmt,ResultSet rs)
try{
if(con != null)con.close();
if(pstmt != null)pstmt.close();
if(rs !=null) re.close();
}catch(Exception e){
e.printStackTrace();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -