1

I can't seem to work out the bug. I need the ResultSet though to get the actual Object. The error is: java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). at rs = pst.executeQuery();. Code:

Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;

String url = null; //Set, but not visible in code.
String user = null; //Set, but not visible in code.
String password = null; //Set, but not visible in code.

public Object get(String table, String key){
    try {
        con = DriverManager.getConnection(url, user, password);

        String query = "SELECT * FROM `" + table + "` WHERE key = ?;";
        pst = con.prepareStatement(query);

        pst.setString(1, key);
        pst.executeUpdate();

        rs = pst.executeQuery();

        rs.next();

        return rs.getObject(1);

    } catch (SQLException ex) {

        return null;

    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {

            return null;

        }
    }
}
OHQCraft
  • 11
  • 1
  • 2
  • 1
    That error message appears when you try to execute a DDL statement like `CREATE TABLE FOO(id int);` using `executeQuery` method. Your given code doesn't seem to be the place where this exception arises. – Luiggi Mendoza May 05 '14 at 17:10
  • 3
    You are using pst.executeUpdate(); on a select query. Why? – Syam S May 05 '14 at 17:12
  • @VMai I think you may have fixed it for me, testing now. – OHQCraft May 05 '14 at 17:15

1 Answers1

0

donot use executeQuery use executeUpdate that your sql statement is between (update,insert,delete) see https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)