servlet(或Java)里面实现用户名唯一性验证问题??

pen6688 2009-12-22 08:40:01
servlet(或Java)里面实现用户名唯一性验证问题??
——————————————————————————————————
麻烦大家帮我看看,我写的这个验证方法问题在哪里?可否帮我修改修改,谢谢!

public boolean validateUserName(String userName) {
//UserDto userDto = null;
try {
boolean flag = false;
// 获得Connection连接对象
conn = DBConnection.getConnection();
// 创建prepareStatement对象
ps = conn.prepareStatement("select user_name from user_Table ");
rs = ps.executeQuery();
if (rs.next()) {
//for(i =0;i<rs.length();i++)
if (rs.equals(userName)) {
return true;
//break;
} else{
return false;
}

}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库操作错误");
} finally {
// 释放资源
DBConnection.clear(conn, ps, rs);
}
//return false;
//return true;
return false;

}
...全文
430 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanhui123456 2010-02-26
  • 打赏
  • 举报
回复
哦,正需要,谢谢啦,各位
ouyangyh 2009-12-24
  • 打赏
  • 举报
回复
最后一个 flag=false,不用它就可以了
pen6688 2009-12-23
  • 打赏
  • 举报
回复
应用还是不成功?
pen6688 2009-12-23
  • 打赏
  • 举报
回复
麻烦大家再看看有没有逻辑错误,谢谢!
————————————————————
public boolean validateUserName(String userName) {
// UserDto userDto = null;
boolean flag = false;
try {

// 获得Connection连接对象
conn = DBConnection.getConnection();
// 创建prepareStatement对象
ps = conn
.prepareStatement("select count(user_name) from user_Table where user_name =?");
ps.setString(1, userName);
rs = ps.executeQuery();
if (rs.next()) {
//if (userName.equals(rs.getString(1))) {
if(rs.getInt(1)>0){

flag = true;
// break;
}
}
flag = false;

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库操作错误");
} finally {
// 释放资源
DBConnection.clear(conn, ps, rs);
}

return flag;

}
pen6688 2009-12-23
  • 打赏
  • 举报
回复
不管怎么样,你这个无法保证多线程并发下的用户名唯一性。
建议楼主:数据库的userName上建unique index。插入前查询,不存在告诉它可用,存在告诉它不可以,插入的时候数据库报不满足唯一索引列的条件的时候,告诉它,来不及了,就在几秒钟之前,有人把你这个用户名给抢喽挖~~~~
————————————————————————————————————
麻烦再问下:唯一索引建完之后怎么响应到前台呢?(具体说就是有重名,数据库执行插入不了,但怎么在前台响应这个消息呢?)
crazylaa 2009-12-23
  • 打赏
  • 举报
回复
不管怎么样,你这个无法保证多线程并发下的用户名唯一性。
建议楼主:数据库的userName上建unique index。插入前查询,不存在告诉它可用,存在告诉它不可以,插入的时候数据库报不满足唯一索引列的条件的时候,告诉它,来不及了,就在几秒钟之前,有人把你这个用户名给抢喽挖~~~~
pen6688 2009-12-22
  • 打赏
  • 举报
回复
有一行应该为:

ps = conn
.prepareStatement("select count(user_name) from user_Table where user_name=?");

——————————————————————————————
但还是不行,
pen6688 2009-12-22
  • 打赏
  • 举报
回复
经过朋友们的帮助,改成如下代码,好像还是不能起作用啊,麻烦再帮助分析分析下,谢谢!
——————public boolean validateUserName(String userName) {
// UserDto userDto = null;
boolean flag = false;
try {

// 获得Connection连接对象
conn = DBConnection.getConnection();
// 创建prepareStatement对象
ps = conn
.prepareStatement("select count(user_name) from user_Table ");
ps.setString(1, userName);
rs = ps.executeQuery();
if (rs.next()) {
if (userName.equals(rs.getString(1))) {
flag = true;
//break;
}
}
flag = false;

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库操作错误");
} finally {
// 释放资源
DBConnection.clear(conn, ps, rs);
}

return flag;

}————————————————————
xiezhifu 2009-12-22
  • 打赏
  • 举报
回复
if (rs.equals(userName)) {

rs应该是ResultSet接口吧,你这样比较怎么能进行比较呢?
应该改为userName.equals(rs.getString(1))
千游 2009-12-22
  • 打赏
  • 举报
回复
if (rs.next()) {
if (rs.equals(userName)) {
return true;
}
怎么直接用 rs 判断呢
ouyangyh 2009-12-22
  • 打赏
  • 举报
回复
ps=conn.prepareStatement("select count(user_name) from user_Table where user_name=?"); 
ps.setString(1,userName);
rs=ps.executeQuery();
if (rs.next()){
if(rs.getInt(1)>0){
return true;
}
}
return false;

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧