用jsp连接MYSQL数据库,怎么验证用户名是否存在,如果存在,则注册失败,如果不存在,则写入数据库中

jsp连接MYSQL数据库,怎么验证用户名是否存在,如果存在,则注册失败,如果不存在,则写入数据库中。目前我写的代码是这样的:<body><%Stringstr1=req... jsp连接MYSQL数据库,怎么验证用户名是否存在,如果存在,则注册失败,如果不存在,则写入数据库中。
目前我写的代码是这样的:
<body>

<%
String str1=request.getParameter("name");
str1=new String(str1.getBytes("iso-8859-1"), "UTF-8");
String str2=request.getParameter("pwd");
str2=new String(str2.getBytes("iso-8859-1"), "UTF-8");
String str3=request.getParameter("email");
str3=new String(str3.getBytes("iso-8859-1"), "UTF-8");
Connection conn1 = dbconn.getConn();
Statement stmt1 = conn1.createStatement();
stmt1.executeUpdate("insert into work (name,pwd,email) values('"+str1 + "','"+str2+"','"+str3+"'");
stmt1.close();
conn1.close();
%>
接下来怎么写呢?
展开
 我来答
若以下回答无法解决问题,邀请你更新回答
youbl
2012-08-29 · TA获得超过2510个赞
知道大有可为答主
回答量:1844
采纳率:62%
帮助的人:1636万
展开全部
Statement stmt1 = conn1.createStatement();
String value="0";
try {
ResultSet rs = stmt1.executeQuery("select count(1) from work where name = '"+str1 + "'");
if(rs != null){
rs.next();
value = rs.getObject(1).toString();
}
if(value != "0"){
// 该用户名已经注册了
return;
}
stmt1.executeUpdate("insert into work (name,pwd,email) values('"+str1 + "','"+str2+"','"+str3+"'");

}
finally {
close(rs);
stmt1.close();
conn1.close();

}
追问
为什么我写进去后,第一行Statement stmt1 = conn1.createStatement();
stmt1出现个大叉呢
追答
这个命令是你自己写的,我只是抄一下
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友4b68195
2012-08-29 · TA获得超过1520个赞
知道大有可为答主
回答量:1773
采纳率:100%
帮助的人:1696万
展开全部
<%
String str1=request.getParameter("name");
str1=new String(str1.getBytes("iso-8859-1"), "UTF-8");
String str2=request.getParameter("pwd");
str2=new String(str2.getBytes("iso-8859-1"), "UTF-8");
String str3=request.getParameter("email");
str3=new String(str3.getBytes("iso-8859-1"), "UTF-8");
Connection conn1 = dbconn.getConn();
Statement stmt1 = conn1.createStatement();
String sql="select * from work where name='"+str1+"'" ;
ResultSet rs = stmt1.executeQuery(sql);

if(rs.absolute(1)){
out.print("注册失败!");
}else{
stmt1.executeUpdate("insert into work (name,pwd,email) values('"+str1 + "','"+str2+"','"+str3+"'");
}
rs.close();
stmt1.close();
conn1.close();
%>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式