dao接口:
//检测用户名是否重复
public User checkUserName(String userName);
dao实现类:
//检测用户名是否重复
@Override
public User checkUserName(String userName) {
Connection conn = DBUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
User user=null;
String sql="SELECT USER_NAME FROM T_USER WHERE USER_NAME = ? ";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, userName);
rs=ps.executeQuery();
while(rs.next()) {
user=new User();
user.setUserName(rs.getString("USER_NAME"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeConn(conn, ps, rs);
}
return user;
}
建一个类来测试:
package com.cyzy.dao;
import com.cyzy.bean.User;
public class UserDaoTest {
public static void main(String[] args) {
UserDao userDao=new UserDaoImpl();
User user=new User(0, "zhangxiaoxiao", null, null, 0, null, 0);
User users=userDao.checkUserName(user.getUserName());
if(users !=null) {
System.out.println("用户存在!!!");
}else {
System.out.println("用户不存在!!!");
}
}
}
检测到zhangxiaoxiao用户名已存在
检测到zhangsan用户名不存在
来源:CSDN
作者:走到天涯海角
链接:https://blog.csdn.net/weixin_42995083/article/details/104591739