用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();
%>
接下来怎么写呢? 展开
目前我写的代码是这样的:
<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();
%>
接下来怎么写呢? 展开
若以下回答无法解决问题,邀请你更新回答
2个回答
展开全部
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();
}
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出现个大叉呢
追答
这个命令是你自己写的,我只是抄一下
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%
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();
%>
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();
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询