我开发了一个CRUD应用程序,它正在运行,但我无法更新已插入的数据。这是我的代码,告诉我是否需要任何其他文件.. 我没有得到任何错误..... 所以我无法找到问题出在哪里......
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 (action.equalsIgnoreCase("insert")) {
// if (action.equalsIgnoreCase("insert")) {
// 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")) {
int rollno = Integer.parseInt(roll);
this.dao.removeUser(rollno);
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")) {
int rollno = Integer.parseInt(roll);
long mono = Long.parseLong(mo);
UserBean user = new UserBean();
user.setName(request.getParameter("name"));
user.setRollno(rollno);
user.setClas(request.getParameter("clas"));
user.setMono(mono);
this.dao.editUser(user);
request.setAttribute("student", this.dao.getAllUsers());
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 con;
public UserDao() {
this.con = ConnectionProvider.getConnection();
System.out.println("Value of conn is "+con);
}
/* public UserDao() {
this.conn = ConnectionProvider.getConnection();
}
*/
// Insert Data
public void addUser(UserBean userBean) {
try {
String sql = "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?)";
PreparedStatement ps = this.con.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();
}
}
// Delete Data
public void removeUser(int rollno) {
try {
String sql = "DELETE FROM student WHERE rollno=?";
PreparedStatement ps = this.con.prepareStatement(sql);
ps.setInt(1, rollno);
ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
// Edit Data
public void editUser(UserBean userBean) {
try {
String sql = "UPDATE student SET rollno=?, class=?, mobileno=? WHERE rollno=?";
// String sql = "UPDATE student SET rollno=?, class=?, mobileno=? WHERE name=?";
// String sql = "UPDATE student SET name=?, rollno=?, class=?, mobileno=? WHERE rollno=?";
PreparedStatement ps = this.con.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();
}
}
// Viewing Data
public List<UserBean> getAllUsers() {
List<UserBean> student = new ArrayList<UserBean>();
try {
if(this.con.isClosed()) {
System.out.println("Connection is closed");
}
else {
String sql = "SELECT * FROM student ORDER BY name";
PreparedStatement ps = this.con.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("class"));
userBean.setMono(rs.getLong("mobileno"));
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.con.prepareStatement(sql);
ps.setInt(1, rollno);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
userBean.setName(rs.getString("name"));
userBean.setRollno(rs.getInt("rollno"));
userBean.setClas(rs.getString("class"));
userBean.setMono(rs.getLong("mobileno")); }
}
catch (SQLException e) {
e.printStackTrace();
}
return userBean;
}
}