JSP web pages prevent SQL injection attacks

Source: Internet
Author: User

SQL injection attacks are introduced to Web applications by constructing special input as parameters. These inputs are mostly combinations in SQL syntax, attackers can execute SQL statements to perform the operations they want. The main reason is that the program does not carefully filter user input data, resulting in illegal data intrusion into the system.

The prepareStatement method is a simple and effective method to prevent SQL injection.

Differences between preparedStatement and statement
1. preparedStatement is a sub-Method of statement.
2. preparedStatement can prevent SQL Injection Problems
3. preparedStatement: It can pre-compile the SQL statements it represents to reduce the pressure on the server.

Example:

public User find(String username, String password) {Connection conn = null;PreparedStatement st = null;ResultSet rs = null;try{conn = JdbcUtils.getConnection();String sql = "select * from users where username=? and password=?";st = conn.prepareStatement(sql);   st.setString(1, username);st.setString(2, password);rs = st.executeQuery();  //if(rs.next()){User user = new User();user.setId(rs.getString("id"));user.setUsername(rs.getString("username"));user.setPassword(rs.getString("password"));user.setEmail(rs.getString("email"));user.setBirthday(rs.getDate("birthday"));return user;}return null;}catch (Exception e) {throw new DaoException(e);}finally{JdbcUtils.release(conn, st, rs);}}


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.