Home > Back-end >  To solve the
To solve the

Time:09-28

Write a registration page to the database
Error has been
Null pointer exception

Java. Lang. NullPointerException
Production. JDBC. JDBCUtil. Edit (JDBCUtil. Java: 28)
Production. Service. UserService. AddUser (UserService. Java: 19)
Production. UserServlet. Register (62) UserServlet. Java:
Production. UserServlet. DoGet (UserServlet. Java: 39)
Production. UserServlet. DoPost (UserServlet. Java: 48)
Javax.mail. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 650)
Javax.mail. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 731)
Org, apache tomcat, websocket server. WsFilter. DoFilter (WsFilter. Java: 52)
Package production. JDBC;

Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;

Public class JDBCUtil {
Public static ResultSet queryForAll (String, SQL Object [] objs) {
The Connection conn=getConnect ();
The ResultSet rs=null;
Try {
PreparedStatement PSMT=conn. PrepareStatement (SQL);
For (int I=0; I & lt; Objs. Length; I++) {
PSMT. SetString (I + 1, objs [I] the toString ());
}
Rs=PSMT. ExecuteQuery ();
} the catch (SQLException e) {
System. The out. Println (" the exception occurs when executing SQL statements: error code "+ um participant etErrorCode () +", "+ um participant etMessage ());
}
Return the rs;
}
Public static int edit (String, SQL Object [] params) {
The Connection conn=getConnect ();
Int result=0;
Try {
PreparedStatement PSMT=conn. PrepareStatement (SQL);
For (int I=0; I & lt; Params. Length; I++) {
PSMT. SetString (I + 1, params [I] the toString ());
}
Result=PSMT. ExecuteUpdate ();
} the catch (SQLException e) {
System. The out. Println (" the exception occurs when executing SQL statements: error code "+ um participant etErrorCode () +", "+ um participant etMessage ());
}
Return the result.
}

Private static Connection getConnect () {
Connection conn=null;
Try {
Class.forname (". Com. Mysql. JDBC Driver ");
Conn=DriverManager. GetConnection (" JDBC: mysql://localhost: 3306/company "
+ "? CharacterEncoding=utf-8 ", "root", "password");
} the catch (a ClassNotFoundException e) {
System. The out. Println (" load database driven exception: "+ um participant etMessage ());
} the catch (SQLException e) {
System. The out. Println (" access database link exception: "+ um participant etMessage ());
}
return conn;
}

}


Package production. Service;

Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

The import production. Production;
The import production. JDBC. JDBCUtil;
The import production. The User;


Public class UserService {

Public void addUser (User User) {
String SQL="insert into user (username, password, phone, email)" + "values (?,?,?,?,? ,? ,? ,?) ";
JDBCUtil. Edit (SQL, the new Object [] {user. GetUsername (), the user, getPassword (), the user. The getPhone (), the user, getEmail ()});
}

Public Boolean checkUser (String username, String password) throws SQLException {
//TODO Auto - generated method stub
String SQL="select * from the user where the username=? And password=? ";
The ResultSet rs=JDBCUtil. QueryForAll (SQL, the new Object [] {the username, password, "});
While (rs), next ()) {
return true;
}
return false;
}
public List QueryUserList (Map hashMap) throws SQLException {
List The userList=new ArrayList<> (a);
String SQL="select * from the user where 1=1";
String whereSql="";
Object [] params=new Object [hashMap. The size ()];
Set EntrySet=hashMap. EntrySet ();
Int index=0;
For (Map. Entry Entry: entrySet) {
WhereSql +="and" + entry. GetKey () + "like? ";
Params [index]=entry. The getValue ();
Index++;
}
The ResultSet rs=JDBCUtil. QueryForAll (SQL + whereSql, params);
While (rs), next ()) {
Production user=new Production ();
User. SetId (rs. Get int (" id "));
User. SetProname (rs. Get string (" proname "));
User. SetUsername (rs. Get string (" username "));
User. SetProyield (rs. GetDouble (" proyield "));
User. SetEvaluate (rs. Get string (" evaluate "));
User. SetChoose (rs. Get string (" choose "));
The userList. Add (user);
}
Return the userList;
}

}
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;

The import production. The User;
The import production. Service. UserService;

/* *
* the Servlet implementation class UserServlet
*/
@ WebServlet ("/UserServlet ")
Public class UserServlet extends the HttpServlet {
private static final long serialVersionUID=1L;

/* *
* @ see HttpServlet# HttpServlet ()
*/
Public UserServlet () {
super();
//TODO Auto - generated constructor stub
}

/* *
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related