Java prevents SQL Injection

Source: Internet
Author: User

1. Definition:

SQL injection is to insert SQL commands into web forms to submit or input query strings for domain names or page requests, and finally fool the server to execute malicious SQL commands.

2. methods to prevent SQL injection:

A: Use preparedstatement instead of statement.

1) Using preparedstatement is more readable and maintainability than statement code.
2) preparedstatement to maximize performance.
3) The most important thing is that preparedstatement greatly improves system security.

 sql="select * from admin where username=? and password=?"; PreparedStatement psmt= con.prepareStatement(sql); psmt.setString(1,username); psmt.setString(2,password); ResultSet rs = psmt.executeQuery(); if(rs.next){ rs.close(); con.close(); return false; } else{ rs.close(); con.close(); return true; }

B: Filter strings.

public static String filterContent(String content){   String flt ="'|and|exec|insert|select|delete|update|count|*|%                   |chr|mid|master|truncate|char|declare|;|or|-|+|,";   Stringfilter[] = flt.split("|"); for(int i=0;i<filter.length ; i++)    {      content.replace(filter[i], "");    }     return content;  }

Or use filter to filter global form parameters.

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.