黑马程序员技术交流社区

标题: 关于PreparedStatement类防SQL注入问题? [打印本页]

作者: 黄茂霖    时间: 2013-4-24 21:43
标题: 关于PreparedStatement类防SQL注入问题?
本帖最后由 shenqi 于 2013-4-24 21:44 编辑

  1. package com.itcast.itheima;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;

  6. public class PreparedStatmentTest {

  7.         /**
  8.          * @param args
  9.          */
  10.         public static void main(String[] args) {
  11.                 // TODO Auto-generated method stub
  12.                
  13.                 Connection ct = null;
  14.                 ResultSet rs =null;
  15.                 PreparedStatement ps = null;
  16.                
  17.                 try {
  18.                         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  19.                         
  20.                         ct = DriverManager.getConnection
  21.                                 ("jdbc:microsoft:sqlserver://127.0.0.1:1433;database=db_webApps", "sa", "999999");
  22.                         
  23.                         
  24.                         //ps = ct.prepareStatement("select pass from tb_user where name = ?");
  25.                         
  26.                         ps = ct.prepareStatement("select top ? * from tb_user where id not in(select top ?s id from tb_user)");
  27.                         
  28.                         //select pass from tb_user where name = ? or 1 = 1
  29.                         ps.setString(1, "6");
  30.                         ps.setString(2, "3");
  31.                         
  32.                         
  33.                         rs = ps.executeQuery();
  34.                         
  35.                         while(rs.next()){
  36.                                 
  37.                         /*        if("999999".equals(rs.getString(1))){
  38.                                        
  39.                                         System.out.println("存在该账号");
  40.                                 }*/
  41.                                 System.out.println(rs.getString(1));
  42.                         }
  43.                         
  44.                 } catch (Exception e) {
  45.                         // TODO Auto-generated catch block
  46.                         e.printStackTrace();
  47.                 }finally{
  48.                         try {
  49.                                 if(rs != null){
  50.                                         rs.close();
  51.                                 }
  52.                                 if(ps != null){
  53.                                         ps.close();
  54.                                 }
  55.                                 if(ct != null){
  56.                                         ct.close();
  57.                                 }
  58.                         } catch (Exception e2) {
  59.                                 // TODO: handle exception
  60.                                 e2.printStackTrace();
  61.                         }
  62.                         
  63.                 }
  64.                
  65.         }

  66. }
复制代码
这是一个分页程序,为什么这样运行确报告SQL错误?

  1. java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]第 1 行: '@P1' 附近有语法错误。
  2.         at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
  3.         at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
  4.         at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
  5.         at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
  6.         at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
  7.         at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
  8.         at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
  9.         at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
  10.         at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
  11.         at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
  12.         at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
  13.         at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
  14.         at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
  15.         at com.itcast.itheima.PreparedStatmentTest.main(PreparedStatmentTest.java:35)
复制代码
而我把SQL语句换成select pass from tb_user where name = ? 就没问题。这是怎么回事?这个问题纠结很久了!~







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2