1

i don`t know what should i do about "not supported by PreparedStatment" exception.

my code is

connection = connectToDB();
        print("Connection Success");

        String sql = "Create Table test(id Integer primary key not null," +
                 "name Varchar(32)," +
                 "age Integer)";
        Statement statement = connection.createStatement();
        statement.execute(sql);
        print("Created Table");

        sql = "Insert into test Values(1, 'Mr.Maeda', 23)";
        statement.execute(sql);
        print("Inserted Data");

        ResultSet result;
        sql = "select * from test";
        PreparedStatement preStatement = connection.prepareStatement("select * from test where age = ?");
        preStatement.setString(1, "23");
        result = preStatement.executeQuery(sql);
        while(result.next()){
            print(result.getString(result.getRow()));               
        }

and I am using eclipse & java.

1
  • Where exactly are you getting the exception? Commented Jun 18, 2013 at 5:09

2 Answers 2

1

Change

result = preStatement.executeQuery(sql);

to

result = preStatement.executeQuery();

PreparedStatement.executeQuery() doesn't take the query string as an argument because it's already prepared it.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to call executeUpdate() instead of execute while inseritng the records:

 sql = "Insert into test Values(1, 'Mr.Maeda', 23)";
 statement.executeUpdate(sql);

instead of

 sql = "Insert into test Values(1, 'Mr.Maeda', 23)";
 statement.execute(sql);

Difference between execute and executeUpdate:

boolean execute() Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

executeUpdate() Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

For more information read javadocs

2 Comments

I don't think this would solve the issue. I believe execute() will work as well for UPDATE.
Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.