Today to help a friend to do project design problems encountered,
Look at DAO first, there is no single quotation mark in DAO, cause database query error, because Java will automatically stitch into string
Public admindto Selectadminbyid (String name) { Connection conn = Dbhelper.getconnection (); String SQL = "SELECT * from admin where username=" +name; Admindto admin = null; try { PreparedStatement PST = conn.preparestatement (SQL); ResultSet rst = Pst.executequery (); while (Rst.next ()) { admin = new admindto (); Admin.setid (Rst.getint ("id")); Admin.setusername (rst.getstring ("username")); Admin.setuserpwd (rst.getstring ("userpwd")); } Rst.close (); Pst.close (); } catch (SQLException e) { e.printstacktrace (); }
And in the database need to have single quotation marks, it should have been such a query result
resulted in
Throws a SQL error, the workaround is
String sql= "SELECT * from admin where username = '" +name+ "'";
Use this statement to add single quotation marks to solve, but this is not perfect, we should define a more perfect way to execute the database query,
public static ResultSet executeQuery (String sql, Object ... params) throws SQLException {Connection conn = ds.getconnection (); try {return executeQuery (conn, SQL, params);} catch (SQLException ex) {closequietly (conn); throw ex;}}
Use this method to execute a query
Parameter params is a variable-length parameter that can be passed in as the field required by the query for the database
Jdbcutils.executequery ("select * from T_cities where id=?") and isdeleted=0 ", id);
The other one is the redirect problem.
We want to redirect the request of the officer servlet to the servlet
Response.sendredirect ("/showservlet");
Of course, sometimes light redirects to the primary server is not enough, we need to forward the response and requests to the primary server
Request.getrequestdispatcher ("/showservlet"). Forward (request, response);
There's another way, but it's not recommended.
Response.sendredirect (Request.getcontextpath () + "/servlet/servletb");
To write the most effective code, you should be aware of the differences between the two redirection techniques. The forward method works inside the Web container. The Sendredirect method requires a roundtrip to the client. So the forward method is faster than the sendredirect. However, using the forward method has limitations, you can only redirect to one resource in the same Web application. The Sendredirect method allows you to redirect to any URL. Conclusion: If you can solve your problem, then use the forward method. Use the Sendredirect method only if you cannot use the forward method.
Forward does not change the path in the current browser address bar
Problems with Java internal forwarding and databases