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

i am getting this exception....plz help me...

error:

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

1 Answer 1

1

You only have one Parameter to bind. Which has the index one:

So:

ps.setInt(2,acNo);

must be:

ps.setInt(1,acNo);
Sign up to request clarification or add additional context in comments.

3 Comments

Bt i have provided acNo at 2nd index on the given table... @jens
Is this problem related to the index numbering 9 or parameter ????
setInt first parameter relates to the index of "?" expressions in your query. You only have one, so the setInt should bind to "1" only.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.