如何判断用户名是否在数据库中已经存在呢JSP

<%
String ID = request.getParameter("ID");
String password = request.getParameter("password");
String rpassword = request.getParameter("rpassword");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String year = request.getParameter("year");
if(year.length() ==1) year = "0"+year;
String mouth = request.getParameter("mouth");
if(mouth.length() == 1) mouth = "0"+mouth;
String day = request.getParameter("day");
if(day.length() == 1) day = "0"+day;
String date = year+"-"+mouth+"-"+day;
String description = request.getParameter("description");

Connection con = null;
PrepareStatement psm = null;
ResultSet rs = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/mydb";
con = DriverManager.getConnection(url,"root","000000");
psm = con.prepareStatement("insert into users values(?,?,?,?,?,?)");
psm.setString(1,ID);
psm.setString(2,password);
psm.setString(3,name);
psm.setString(4,sex);
psm.setLong(5,getDate(date));
psm.setString(6,description);
psm.executeUpdate();
session.setAttribute("user",ID);
response.sendRedirect("index.jsp");
}
catch(Exception e){
response.sendRedirect("register.jsp");
}
finally
{
if(rs!=null){
try{ rs.close();}catch(Exception e){e.printStackTrace();}
}
if(psm!=null){
try{ psm.close();}catch(Exception e){e.printStackTrace();}
}
if(con!=null){
try{ con.close();}catch(Exception e){e.printStackTrace();}
}
}
%>
我想在插入操作之前先判断要插入记录的用户名是否在数据库中已经存在了,怎么写呢,我是新手,还请高手多帮忙啊~

这样:

psm = con.prepareStatement("select * from users where username=?");

psm.setString(1,name);

rs = psm.executeQuery();

if(rs!=null){

out.println("该用户名已经被注册!");

response.sendRedirect("register.jsp");

}

else{

在这里写插入数据的语句

}

扩展资料:

注意事项

访问数据库类文件写法

//根据传过来的用户名查询数据库中是否存在此用户

public String tblUserExist(String uName){

String username = "";

String sql="select uName from tbl_user where uName=?";

try {

pst = con.prepareStatement(sql);

pst.setString(1,uName);

ResultSet rst = pst.executeQuery();

if(rst.next()){

username=rst.getString(uName);

}

} catch (SQLException e)

{

e.printStackTrace();

}finally

{

this.closepst();//关闭pst对象

this.closecon();//关闭数据库对象

}

return username;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-21
在执行这句:psm = con.prepareStatement("insert into users values(?,?,?,?,?,?)");之前,根据用户名到数据库里搜索一下,如果有结果集返回就说明该用户名已经被注册了。。。
假设:用户名在数据库中的字段名为username,你可以这样写:
psm = con.prepareStatement("select * from users where username=?");
psm.setString(1,name);
rs = psm.executeQuery();
if(rs!=null){
out.println("该用户名已经被注册!");
response.sendRedirect("register.jsp");
}
else{
在这里写插入数据的语句
}
这只是一个大体思路,一些细节需要你自己完善,祝好运!本回答被提问者采纳
第2个回答  2009-06-04
大概的思路 2 3楼讲的很明白了!你按他们写就可以实现你要的功能了。

3楼代码有个问题
如果我输入英文的’号
你的程序就会抛异常
因为你拼成的SQL语句是 select gh from glyb where gh =‘’‘

应该在拼之前对一些敏感字符进行转义
第3个回答  2009-06-04
这是我的以前的代码,你参考下吧,希望能帮到你。

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 这里加载驱动,数据库
Connection con=DriverManager.getConnection("jdbc:odbc:xtsjy","sa","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select gh from glyb where gh ="+"'"+yhm+"'");
if(rs.next())//上面两句代码这里是判断工号是否存在的
{
out.println("<div align=\"center\"><font color=red size=5>"+yhm+"工号已经存在,请重新输入! </font></div>");
response.setHeader("Refresh","1;URL=g_xinzeng.jsp");
rs.close();
return;
}
PreparedStatement s=con.prepareStatement("insert into glyb values(?,?,?,?,?)");
s.setString(1,yhm);
s.setString(2,mm);
s.setString(3,xm);
s.setString(4,nl);
s.setString(5,zy);
第4个回答  2009-06-04
进行添加之前 写一个查询方法 根据用户来判断是否有这条数据
相似回答