Implementer
Implementer

Reputation: 37

Exception in prepared statement

Following is the code.

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     conn = DriverManager.getConnection("jdbc:odbc:cse");
     //Statement stmt;
     ResultSet rset;
     //stmt = conn.createStatement();

     String sql = " Select * from registration where id=?";
     PreparedStatement pst = conn.prepareStatement(sql);
     pst.setString(1, "101");
     rset = pst.executeQuery(sql);

     while (rset.next()) {
         arr.add(rset.getInt("id"));
         arr.add(rset.getString("first"));
         arr.add(rset.getString("last"));
         arr.add(rset.getInt("age"));
    }

  System.out.println(arr);
  pst.close();
  conn.close();

For the above am getting "Error: java.sql.SQLException: Driver does not support this function". What might be the problem?

Upvotes: 1

Views: 1116

Answers (1)

Mureinik
Mureinik

Reputation: 312146

You are misusing the PreparedStatement interface. When using PreparedStatements, you should prepare the statement with your query, bind all necessary parameters and then execute it without any SQL - this will cause the statement to execute the previously prepared SQL statement:

 String sql = "Select * from registration where id=?";
 PreparedStatement pst = conn.prepareStatement(sql);
 pst.setString(1, "101");
 rset = pst.executeQuery(); // Note - No args in the executeQuery call

Upvotes: 7

Related Questions