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;
}
——————————————————————————————————
麻烦大家帮我看看,我写的这个验证方法问题在哪里?可否帮我修改修改,谢谢!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;
}
ps.setString(1,userName);
rs=ps.executeQuery();
if (rs.next()){
if(rs.getInt(1)>0){
return true;
}
}
return false;
if (rs.equals(userName)) {
return true;
}
怎么直接用 rs 判断呢
应该改为userName.equals(rs.getString(1))
——————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; }————————————————————
.prepareStatement("select count(user_name) from user_Table where user_name=?");——————————————————————————————
但还是不行,
建议楼主:数据库的userName上建unique index。插入前查询,不存在告诉它可用,存在告诉它不可以,插入的时候数据库报不满足唯一索引列的条件的时候,告诉它,来不及了,就在几秒钟之前,有人把你这个用户名给抢喽挖~~~~
建议楼主:数据库的userName上建unique index。插入前查询,不存在告诉它可用,存在告诉它不可以,插入的时候数据库报不满足唯一索引列的条件的时候,告诉它,来不及了,就在几秒钟之前,有人把你这个用户名给抢喽挖~~~~
————————————————————————————————————
麻烦再问下:唯一索引建完之后怎么响应到前台呢?(具体说就是有重名,数据库执行插入不了,但怎么在前台响应这个消息呢?)
————————————————————
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; }