使用Java检查Mysql数据库中已存在的用户时出错 [英] getting error while checking User Already Exists in Mysql database using java
问题描述
我尝试了一些代码来检查用户在注册时是否已经存在于数据库中.但是我得到这样的错误.请向我建议我哪里做错了.提前致谢.我正在使用Netbeans 8.2,Apache Tomcat,MySQL数据库(XAMPP)
I have tried some code to check that user is already exist in database or not while doing registration. But I am getting error like this. Please suggest me where I am getting wrong. Thanks in advance. I am using netbeans 8.2, apache tomcat, MySQL database(XAMPP)
这是我的registration.jsp页面
This is my registration.jsp page
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration JSP Page</title>
<style>
body {
background-color: lightblue;
}
#wgtmsrnew {
width: 153px;
}
</style>
<script>
function validateForm() {
alert("User Registered Successfully");
return true;
}
</script>
</head>
<body>
<form method="post" action="registrationJSPScript.jsp" onsubmit="return validateForm()">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Register</th>
<p><b>Please fill in this form to create an account.</b></p>
</tr>
</thead>
<tbody>
<tr>
<td>Z ID</td>
<td><input type="text" name="zid" value="" required="" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" value="" required="" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="" required="" /></td>
</tr>
<tr>
<td>Mail ID</td>
<td><input type="email" name="mailid" value="" required="" /></td>
</tr>
<td>Password</td>
<td><input type="password" name="pass" required="" value="" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{8,12}$" required title="8 characters minimum,At least 1 Uppercase,At least 1 Lowercase,At least 1 number,At least 1 Symbol, symbol allowed --> !@#$%^&*_=+-"
/></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Already registered!! <a href="loginJSP.jsp">Login Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
推荐答案
您需要首先检查是否已经存在用户输入的zid
或不使用select
查询,然后检查user
是否存在.警报,否则将insert
记录在db中,您的代码将如下所示:
You need to check first if your zid
enter by user is already present or not using select
query and then if user
exist just put an alert ,else insert
record in db and your code will look like somewhat below:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dmsqms","root", "");
Statement st = con.createStatement();
String sql = "SELECT * FROM dmsmembers WHERE zid=?";//getting username
PreparedStatement ps=con.prepareStatement(sql);
//setting value for ?
ps.setString(1,zid);
ResultSet rs = ps.executeQuery();
//checking if record with zid exist if yes do below
if(rs.next()){
//put alert here
out.println("<script type=\"text/javascript\">");
out.println("alert('User Already Exists');");
out.println("</script>");
}else{
//if zid doesn't exist insert new record
int i = st.executeUpdate("insert into dmsmembers(zid, first_name, last_name, mailid, department, division, location, pass, regdate) values ('" + zid + "','" + fname + "','" + lname + "','" + email + "','" + department + "','" + division + "','" + location + "','" + pwd + "', CURDATE())");
if (i > 0) {
//response.sendRedirect("loginJSP.jsp");
//insert successfull alert message and redirect
out.println("<script type=\"text/javascript\">");
out.println("alert('Registration Successfull!');");
out.println("location='loginJSP.jsp';");
out.println("</script>");
} else {
//not inserted have some error print here
}
}
} catch(Exception e){
//print error if any
e.printStackTrace();
}
这篇关于使用Java检查Mysql数据库中已存在的用户时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!