Getting error while checking User Already Exists in MySQL database using Java
我尝试了一些代码来检查用户在注册时是否已经存在于数据库中。但我收到这样的错误。我哪里错了?
我使用的是 Netbeans 8.2、Apache Tomcat 和 MySQL 数据库 (XAMPP)。
这是我的registration.jsp页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> Registration JSP Page <style> body { background-color: lightblue; } #wgtmsrnew { width: 153px; } </style> function validateForm() { alert("User Registered Successfully"); return true; } </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>Please fill in this form to create an account.</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!! Login Here</td> </tr> </tbody> </table> </center> </form> </body> </html> |
您需要首先检查用户输入的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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(""); }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(""); } else { //not inserted have some error print here } } } catch(Exception e){ //print error if any e.printStackTrace(); } |
使用 try-catch 块。它会起作用。