Java backend methods for preventing SQL injection

Source: Internet
Author: User
Tags sql injection

1. Using a precompiled statement set, it has the ability to handle SQL injection, as long as it uses its SetString method to pass values:

        String sql= "SELECT * from Users where username=? and password=?;        PreparedStatement prestate = conn.preparestatement (sql);        Prestate.setstring (1, userName);        Prestate.setstring (2, password);         = Prestate.executequery ();

2. The regular expression will contain a single quotation mark ('), a semicolon (;) and comment symbols (--) are replaced by statements to prevent SQL injection

     Public Static string SQL (String str)    {        return str.replaceall (". *" ([';] +| (--)+).*", " ");    }    UserName=SQL(userName);    Password=SQL(password);    String SQL= "SELECT * from Users where username= '" +username+ "' and password= '" +password+ "'"    = conn.createstatement ();     = Sta.executequery (SQL);

Java backend methods for preventing SQL injection

Related Article

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.