数据未在数据库中更新。 即时开发登录功能,我也注册用户。注册后,我想显示注册过程中使用的详细信息。 我发布了我使用的servlet的代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final int Id = 0;
private static final String Password = null;
private static final String Username = null;
private static final int Userid = 0;
private static String INSERT_OR_EDIT = "/user.jsp";
private static String LIST_USER = "/listUser.jsp";
public static String PARAM_USERNAME = "uname";
public static String PARAM_PASSWORD = "pass";
private UserDao dao;
private Connection connection;
public UserController() {
super();
dao = new UserDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String forward="";
String act = request.getParameter("act");
if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("login")) {
forward= "/Login.jsp";
}
else if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("delete"))
{
int userId = Integer.parseInt(request.getParameter("userId"));
dao.deleteUser(userId);
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
} else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("edit")){
forward = INSERT_OR_EDIT;
int userId = Integer.parseInt(request.getParameter("userId"));
User user1 = dao.getUserById(userId);
request.setAttribute("user", user1);
} else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("listUser")){
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
} else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("register")){
forward = "/reg.jsp";
// request.setAttribute("users", dao.getAllUsers());
} else
forward = "/Login.jsp";
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String act = request.getParameter("act");
if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("login")) {
String str=request.getParameter("username");
String str1=request.getParameter("password");
if(str.equalsIgnoreCase("shreya")&&str1.equalsIgnoreCase("singh"))
{
System.out.println("Login!");
request.setAttribute("users",dao.getAllUsers());
request.getRequestDispatcher("/listUser.jsp").forward(request, response);
}else
{
System.out.println("Login failed!");
}
}
else
{
}
User user = new User();
Details details = new Details();
user.setFirstName(request.getParameter("firstName"));
user.setLastName(request.getParameter("lastName"));
details.setUsername(request.getParameter("uname"));
details.setPassword(request.getParameter("pass"));
if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("register"))
{
String userid=request.getParameter("userid");
String username=request.getParameter("username");
String password=request.getParameter("password");
String id=request.getParameter("id");
if(userid.equals("") || username.equals("") || password.equals("") || id.equals(""))
{
out.println("Please insert valid data");
}
RequestDispatcher rd = request.getRequestDispatcher("/listUser.jsp");
rd.include(request, response);
}
else
{
RequestDispatcher rd = request.getRequestDispatcher("/listUser.jsp");
rd.include(request, response);
try {
PreparedStatement preparedStatement = connection.
prepareStatement("select * from details where id=?");
preparedStatement.setInt(1, Id);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
details.setUserid(rs.getInt("userid"));
details.setUsername(rs.getString("username"));
details.setPassword(rs.getString("password"));
details.setId(rs.getInt("id"));
}
PreparedStatement preparedStatement1 = connection.
prepareStatement( "insert into details values(?,?,?,?)");
preparedStatement1.setInt(1, Userid);
preparedStatement1.setString(2, Username);
preparedStatement1.setString(3, Password);
preparedStatement1.setInt(4, Id);
int i=preparedStatement1.executeUpdate();
if(i>0)
{
System.out.println("Data update sucessfully");
System.out.print("Student record successfully inserted");
RequestDispatcher rd1 = request.getRequestDispatcher("/registration.jsp");
rd1.include(request, response);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
由于我不记得什么时候,我一直坚持这个。帮助将不胜感激。