userdaoimpl_jdbc.java
来自「一个ajax源码」· Java 代码 · 共 67 行
JAVA
67 行
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 + =
减小字号Ctrl + -
显示快捷键?