Java PreparedStatement:com.microsoft.sqlserver.jdbc.SQLServerException:参数索引超出范围

时间:2017-11-11 17:38:58

标签: sql-server jdbc

我尝试在JAVA 8中使用java PreparedStatement执行SQL查询并编写代码

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection(URl,user,"");
        String sql = "Select Grade from XS_KC where Snum=? and Cnum=?";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, "020101");
        pst.setString(2, "102");
        rs = pst.executeQuery();
        while(rs.next())
        {
            System.out.print(rs.getString(1));
        }

我收到了fllowing错误:com.microsoft.sqlserver.jdbc.SQLServerException:parameter index out of range 但如果我使用

stat = con.createStatement();
rs = stat.executeQuery("Select Grade from XS_KC where Snum='020101' and Cnum='101'");

我可以得到结果 怎么了?

1 个答案:

答案 0 :(得分:1)

查询中的第一个是Unicode 0x1FFF而不是0x3F00(问号)。尝试:

String sql = "Select Grade from XS_KC where Snum=? and Cnum=?";