public class UserDao {
//判断注册的用户是否存在
public boolean checkUser(String name){
try {
//数据库链接(匹配src文件夹下的c3p0-config.xml文件,这是固定程式)
ComboPooledDataSource dataSource = new ComboPooledDataSource();
QueryRunner queryRunner = new QueryRunner(dataSource);
//传入参数,通过sql语句进入数据库看是否有此参数,如果有则将查询的值赋予user,如果没有则user为null
String sql="select name from user where name=?";
User user = queryRunner.query(sql,new BeanHandler<User>(User.class),name);
//如果user为空,说明这个用户名没有被注册过
if(user==null){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}