您的位置:首页 > 其它

使用prepareStatement进行模糊查询时的单引号问题

2015-06-29 16:25 176 查看
写道 ■Statement#executeQueryを使う場合

  ⇒ SQLに埋め込むので、シングルクォートのエスケープが必須

    入力=[']
    変換=[%''%]


■Connection#prepareStatement を使う場合
■JdbcTemplate#queryForList を使う場合

  ⇒ 基本的にSQLインジェクション対策がされているので、
    シングルクォートのエスケープをしてはいけない

    入力=[']
    変換=[%'%]

■如果使用executeQuery进行查询

     需要在SQL文中对单引号进行转义

■如果使用Connection#prepareStatement或JdbcTemplate#queryForList进行查询的话

     基本的SQL注入处理中已包含转义处理,所以不需要在程序中对单引号进行转义。

 

代码实例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class SQLServerTest {
public static void main(String[] srg) throws SQLException {

BasicDataSource ds = null;

try {
ds = new BasicDataSource();
ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ds.setUrl("jdbc:sqlserver://localhost:1433;databaseName=LOCAL;");
ds.setUsername("user");
ds.setPassword("password");

System.out.println("normalQuery start*******************");
normalQuery(ds);
System.out.println("normalQuery end*********************");

System.out.println("templateQuery start*******************");
templateQuery(ds);
System.out.println("templateQuery end*******************");

System.out.println("prepareQuery start*******************");
prepareQuery(ds);
System.out.println("prepareQuery end*******************");

} catch (Exception e) {
e.printStackTrace();
}finally{
if(ds != null){
ds.close();
}
}
}

public static void normalQuery(BasicDataSource ds) throws SQLException{
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM  CLIENT C WITH (NOLOCK) WHERE C.CLIENT_NAME LIKE ('%''%')");

int length = 0;
while (rs.next()) {
System.out.println(rs.getLong(1) + "\t" + rs.getString(2)+ "\t"  + rs.getString(3)+ "\t"  + rs.getLong(4));
length++;
}

System.out.println("result size = " + length);
}

public static void prepareQuery(BasicDataSource ds) throws SQLException{
Connection conn = ds.getConnection();
PreparedStatement perstmt = conn.prepareStatement("SELECT * FROM  CLIENT C WITH (NOLOCK) WHERE C.CLIENT_NAME LIKE (?)");
perstmt.setString(1,"%'%");
ResultSet rs = perstmt.executeQuery();
int length = 0;
while (rs.next()) {
System.out.println(rs.getLong(1) + "\t" + rs.getString(2)+ "\t"  + rs.getString(3)+ "\t"  + rs.getLong(4));
length++;
}

System.out.println("result size = " + length);
}

public static void templateQuery(BasicDataSource ds) throws NamingException, SQLException{

JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
List result = jdbcTemplate.queryForList("SELECT * FROM  CLIENT C WITH (NOLOCK) WHERE C.CLIENT_NAME LIKE (?)", new Object[] {"%'%"});
System.out.println("result size = " + result.size());

}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: