Problems with Java internal forwarding and databases

Source: Internet
Author: User
Tags sql error

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.