我开发了一个简单的CRUD应用程序。使用javabean并在用户文件中获取空指针异常但无法解决它。任何人都可以告诉我这是什么问题吗?
这是代码,
User.jsp <%@ page language =“java”contentType =“text / html; charset = ISO-8859-1”pageEncoding =“ISO-8859-1”%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add New User</title>
</head>
<body>
<form method="POST" action='UserHandler' name="frmAddUser"><input type="hidden" name="action" value="insert" />
<p><b><center>Add New Record<center></b></p>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Roll Number</td>
<td><input type="text" name="rollno" /></td>
</tr>
<tr>
<td>Class</td>
<td><input type="text" name="clas" /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type="text" name="mono" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<p><a href="UserHandler?action=listUser">View-All-Records</a></p>
</body>
</html>
UserHandler.java
package handler;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import dao.UserDao;
import bean.UserBean;
public class UserHandler extends HttpServlet {
private static String INSERT = "/user.jsp";
private static String Edit = "/edit.jsp";
private static String UserRecord = "/listUser.jsp";
private UserDao dao;
public UserHandler() {
this.dao = new UserDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String redirect = "";
String roll = request.getParameter("rollno");
String mo = request.getParameter("mono");
String action = request.getParameter("action");
if ((roll == null) && (action.equalsIgnoreCase("insert"))) {
int rollno = Integer.parseInt(roll);
long mono = Integer.parseInt(mo);
UserBean user = new UserBean();
user.setName(request.getParameter("name"));
user.setRollno(rollno);
user.setClas(request.getParameter("clas"));
user.setMono(mono);
this.dao.addUser(user);
redirect = UserRecord;
request.setAttribute("student", this.dao.getAllUsers());
System.out.println("Record Added Successfully");
}
else if (action.equalsIgnoreCase("delete")) {
String roll1 = request.getParameter("rollno");
int rl = Integer.parseInt(roll1);
this.dao.removeUser(rl);
redirect = UserRecord;
request.setAttribute("student", this.dao.getAllUsers());
System.out.println("Record Deleted Successfully");
}
else if (action.equalsIgnoreCase("editform")) {
redirect = Edit;
}
else if (action.equalsIgnoreCase("edit")) {
String roll2 = request.getParameter("rollno");
String monu = request.getParameter("mobileno");
int uid = Integer.parseInt(roll2);
long mono = Integer.parseInt(monu);
UserBean user = new UserBean();
user.setName(request.getParameter("name"));
user.setRollno(uid);
user.setClas(request.getParameter("clas"));
user.setMono(mono);
this.dao.editUser(user);
request.setAttribute("user", user);
redirect = UserRecord;
System.out.println("Record updated Successfully");
}
else if (action.equalsIgnoreCase("listUser")) {
redirect = UserRecord;
request.setAttribute("student", this.dao.getAllUsers());
}
else {
redirect = INSERT;
}
RequestDispatcher rd = request.getRequestDispatcher(redirect);
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
UserDao.java
package dao;
import java.sql.*;
import java.util.*;
import dbconnection.ConnectionProvider;
import bean.UserBean;
public class UserDao {
private Connection conn;
public UserDao() {
this.conn = ConnectionProvider.getConnection();
}
public void addUser(UserBean userBean) {
try {
String sql = "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ? )";
PreparedStatement ps = this.conn.prepareStatement(sql);
ps.setString(1, userBean.getName());
ps.setInt(2, userBean.getRollno());
ps.setString(3, userBean.getClas());
ps.setLong(4, userBean.getMono());
ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void removeUser(int rollno) {
try {
String sql = "DELETE FROM student WHERE rollno=?";
PreparedStatement ps = this.conn.prepareStatement(sql);
ps.setInt(1, rollno);
ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void editUser(UserBean userBean) {
try {
String sql = "UPDATE student SET name=?, class=?, mobileno=? WHERE rollno=?";
PreparedStatement ps = this.conn.prepareStatement(sql);
ps.setString(1, userBean.getName());
ps.setString(2, userBean.getClas());
ps.setLong(3, userBean.getMono());
ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public List<UserBean> getAllUsers() {
List student = new ArrayList();
try {
/* if(this.conn.isClosed()) {
System.out.println("Connection is closed");
}
else {
*/ String sql = "SELECT * FROM student WHERE name IS NOT NULL ORDER BY name";
PreparedStatement ps = this.conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
UserBean userBean = new UserBean();
userBean.setName(rs.getString("name"));
userBean.setRollno(rs.getInt("rollno"));
userBean.setClas(rs.getString("clas"));
userBean.setMono(rs.getLong("mono"));
student.add(userBean);
}
// }
}
catch (SQLException e) {
e.printStackTrace();
}
return student;
}
public UserBean getUserById(int rollno) {
UserBean userBean = new UserBean();
try {
String sql = "SELECT * FROM student WHERE rollno=?";
PreparedStatement ps = this.conn.prepareStatement(sql);
ps.setInt(2, rollno);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
userBean.setName(rs.getString("name"));
userBean.setRollno(rs.getInt("rollno"));
userBean.setClas(rs.getString("clas"));
userBean.setMono(rs.getLong("mono"));
}
}
catch (SQLException e) {
e.printStackTrace();
}
return userBean;
}
}
这是错误
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
dao.UserDao.getAllUsers(UserDao.java:69)
handler.UserHandler.doGet(UserHandler.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.39
答案 0 :(得分:0)
创建连接是否成功创建
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:port/db";
String user = "xx";
String password = "xxt";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Connection"+con);
}
catch(Exception e) {
System.out.println("Exception when creating connection "+e.getMessage());
}