我正在使用JDBC创建注册应用程序。但是我得到了这个错误。我想我的java.bean有问题我的字段是空的。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
version="2.5">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.pfa.Servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>com.pfa.Servlets.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/registerServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
User.java
package com.pfa.bean;
public class User {
private String uname,upass,uemail;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public String getUemail() {
return uemail;
}
public void setUemail(String uemail) {
this.uemail = uemail;
}
}
Register.Dao
package com.pfa.Dao;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.pfa.bean.User;
public class RegisterDao {
public static int register(User u) throws ClassNotFoundException{
int status=0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = (Connection) DriverManager
.getConnection("jdbc:mysql://localhost:3306/form", "root", "");
Statement ps = (Statement) conn
.createStatement();
Statement ps1 = (Statement) conn
.createStatement();
ResultSet res = ps
.executeQuery("Select * from user");
// Process the result set
boolean test = false;
while (res.next()) {
if ((res.getString("userName").equals(u.getUname()))
|| (res.getString("password")
.equals(u.getUpass()))|| (res.getString("email").equals(u.getUemail()))) {
test = true;
status=0;
System.out.print("<p style=\"color:red\">Y ou ve a lreadyy Logged in</p>");
}
}
if (!test) {
String query = "insert into user"
+ " (userName ,password,email)"
+ " values ('" + u.getUname() + "','"
+ u.getUpass() + "','" +u.getUemail()+")";
ps1.executeUpdate(query);
System.out.println("Insert complete");
status=1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return status;
}
}
RegisterServlet.java
package com.pfa.Servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.pfa.Dao.LoginDao;
import com.pfa.Dao.RegisterDao;
import com.pfa.bean.User;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 102831973239L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("username");
//String p = request.getParameter("userpass");
//String q = request.getParameter("useremail");
HttpSession session = request.getSession(false);
if (session != null)
session.setAttribute("name",n);
User u = new User();
try {
if (RegisterDao.register(u)!=0) {
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
} else {
out.print("<p style=\"color:red\">Sorry u ve already Logged in</p>");
RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
rd.include(request, response);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
}
}
register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859- 1">
<title>Login Application</title>
</head>
<body>
<form action="Process.jsp" method="post">
<fieldset style="width: 300px">
<legend> Register to App </legend>
<table>
<tr>
<td>User ID</td>
<td><input type="text" name="username" required="required" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="userpass" required="required" /></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="useremail" required="required" /></td>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
process.jsp
<%@page import="com.pfa.Dao.*"%>
<jsp:useBean id="obj" class="com.pfa.bean.User"/>
<jsp:setProperty property="*" name="obj"/>
<%
int status=RegisterDao.register(obj);
if(status>0)
out.print("You are successfully registered");
%>
答案 0 :(得分:0)
用户名,应该使用setUsername而不是setUname,你可以找到一些关于java的反映的资源,有些推荐给你,你应该更好地使用preparedstatement查找结果,如&#34; select * from user where uname ='?'&#34;,(避免sql注入,效率更高)