-1

Exception in thread "main" java.sql.SQLException: Method 'executeQuery(String)' not allowed on prepared statement.

public Object getObject() throws SQLException, IOException, ClassNotFoundException 
    {
        PreparedStatement ps;
        String sql="select * from student";
        ps=con.prepareStatement(sql);
        ResultSet rs=ps.executeQuery(sql);
        Student newStudent=null;
        while(rs.next())
        {
            try (ByteArrayInputStream bais = new ByteArrayInputStream(rs.getBytes("description"))) {
                ObjectInputStream obis;
                obis = new ObjectInputStream(bais);
                newStudent=(Student)obis.readObject();
            }
        }
        return newStudent;

    }

Can anyone tell me how to resolve this issue ?

kero
  • 10,647
  • 5
  • 41
  • 51

1 Answers1

2

You should execute query in Prepared Statement as

ps.executeQuery(); //Without parameter

If you read the documentation for executeQuery(String), it is clearly mentioned,

Executes the given SQL statement, which returns a single ResultSet object.
*Note:*This method cannot be called on a PreparedStatement or CallableStatement

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58