我正在为大学开发项目,我是新手,需要帮助解决这个问题,我在eclipse上使用SQLyog和动态网页项目在mysql上创建了一个DB。在注册方面,我使用了以下代码。
register.jsp
<form action="register1.jsp" method="post">
<div class="firstname">
<label for="firstname"></label> <input type="text"
placeholder="My first name is" name="firstname" required>
</div>
<div class="lastname">
<label for="lastname"></label> <input type="text"
placeholder="My last name is" name="lastname" required>
</div>
<div class="email">
<label for="email"></label> <input type="email"
placeholder="My e-mail ID is" name="email_id" required>
</div>
<div class="username">
<label for="username"></label> <input type="text"
placeholder="My username is" name="username" required>
</div>
<div class="pwd">
<label for="pwd"></label> <input type="password"
placeholder="My password is" name="pwd" required>
</div>
<div class="submit">
<input type="submit" value="Register" id="form_button" />
</div>
</form>
注册1.jsp
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
String username=request.getParameter("username");
session.putValue("username",username);
String pwd=request.getParameter("pwd");
String email_id=request.getParameter("email_id");
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university","CinthiyaSingh", "Cinthiya@098");
Statement st=con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into users values('"+ username+ "','"+pwd+ "','"+firstname+ "','"+ lastname+ "','"+ email_id+ "')");
if (i > 0) {
response.sendRedirect("login.jsp");
} else {
request.setAttribute("errorMessage", "Already Existing Username/Email");
request.getRequestDispatcher("Register.jsp").forward(request, response);
}%>
我希望当查询试图违反主键(用户名)属性时,重载注册页面,并显示错误: "用户名已经在使用中"。但是,服务器的错误页面却加载了错误的详细信息。
请研究一下这个问题.我不想使用任何其他文件最好。
使用 select
语句,以检查 username
已经存在于你的表中,那么如果存在显示 error
并重定向,否则 insert
所以你的代码会像下面一样。
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university", "CinthiyaSingh", "Cinthiya@098");
//selecting username from users table
PreparedStatement ps = con.prepareStatement("select *from users where username=?");
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
//if true
if (rs.next()) {
//sending error message on Register page
request.setAttribute("errorMessage", "Already Existing Username/Email");
request.getRequestDispatcher("Register.jsp").forward(request, response);
} else {
//If not exist performing insert
PreparedStatement ps1 = con.prepareStatement("insert into users values(?,?,?,?,?)");
//setting value for "?"
ps1.setString(1, username);
ps1.setString(2, pwd);
ps1.setString(3, firstname);
ps1.setString(4, lastname);
ps1.setString(5, email_id);
int i = ps1.executeUpdate();
//if updated successfully
if (i > 0) {
//redirect
response.sendRedirect("login.jsp");
}
//close connection
}
然后得到上面的错误信息 Register.jsp
使用 ${errorMessage}
.