Achan Zhong
Achan Zhong

Reputation: 3

Java PreparedStatement:com.microsoft.sqlserver.jdbc.SQLServerException:parameter index out of range

I'm try to execute a SQL query using java PreparedStatement in JAVA 8 and code blow

        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));
        }

I get the fllowing error:com.microsoft.sqlserver.jdbc.SQLServerException:parameter index out of range But if i use

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

I can get the result What's wrong??

Upvotes: 0

Views: 85

Answers (1)

Dan Guzman
Dan Guzman

Reputation: 46390

The first in your query is Unicode 0x1FFF rather than 0x3F00 (question mark). Try:

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

Upvotes: 1

Related Questions