gpt4 book ai didi

java - 使用 Java 检查 MySQL 数据库中的用户是否已存在时出现错误

转载 作者:行者123 更新时间:2023-11-29 15:12:30 24 4
gpt4 key购买 nike

我尝试了一些代码来检查用户在注册时是否已存在于数据库中。但我收到这样的错误。我错了什么?

我正在使用 Netbeans 8.2、Apache Tomcat 和 MySQL 数据库 (XAMPP)。

这是我的registration.jsp页面

<%@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>

最佳答案

您需要首先使用select查询检查用户输入的zid是否已经存在或不存在,然后如果user存在则输入一个警报,否则在数据库中插入记录,您的代码将如下所示:

   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 - 使用 Java 检查 MySQL 数据库中的用户是否已存在时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59960659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com