java sql异常重复出现

时间:2017-07-13 07:55:13

标签: java mysql jdbc mysql-error-1064

public void Deposite() throws Exception

    {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");

                String url = "jdbc:mysql://localhost:3306/bank";

                Connection con = DriverManager.getConnection(url,"root","admin");

                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter your A/c no. : " );
                acNo = Integer.parseInt(br.readLine());

                String sql = "SELECT Name,Ac_No,Balance FROM CUSTOMER WHERE Ac_No=?";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setInt(2,acNo);

                ResultSet rs = ps.executeQuery();

                while(rs.next())
                {
                    String name = rs.getString("Name");
                    int acNo = rs.getInt("Ac_No");
                    float bal = rs.getFloat("Balance");

                    System.out.println("    "+name+"        "+acNo+"        "+bal);
                }

我得到了这个例外....请帮助我...

错误:

java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

1 个答案:

答案 0 :(得分:1)

您只有一个要绑定的参数。其中索引一:

所以:

ps.setInt(2,acNo);

必须是:

ps.setInt(1,acNo);