简体   繁体   中英

Oracle JDBC Driver ResultSet PreparedStatement Throws ORA-009900 invalid SQL statement Exception

I am using Oracle JDBC Driver ojdbc6.jar. When executing a PreparedStatement the ResultSet is returned. However when trying to get details back of what has been saved, I am unable to get this and it throws an Exception.

Below is the code:

public Processor {

    private Connection connection;

    public Processor() throws Exception {
        connection = DriverManager.getConnection(url, username, password);
    }

    public int saveData(String name) throws Exception {
       PreparedStatement preparedStatement = connection.prepareStatement(name);
       preparedStatement.setString(1, name);
       ResultSet resultSet = preparedStatement.executeQuery();
       resultSet.next();
       String row = resultSet.getString("NAME");
       return resultSet.getRow();
    }

    public static void main(String[] args) throws Exception {
         Processor processor = new Processor();
         int row = processor.saveData(new String("INSERT INTO NAMES (name) VALUES (nameTable)"));
    }
}

The url, username and password are setup but not shown in the code. I can connect to the Oracle database and persist the data. However, the problem arises when calling resultSet.getString("NAME") . The following Exception is shown:

ORA-009900: invalid SQL statement
java.sql.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)

When executing the following code:

ResultSet resultSet = connection.createStatement().executeQuery("SELECT NAME FROM NAMES"));
resultSet.next();
String name = resultSet.getString("NAME");

This works. I have also executed in the SQL describe NAMES and the name attribute is displayed as NAME .

The issue is something to do with the PreparedStatement object returning the ResultSet data.

Any ideas?

EDIT:

ORA-00900: invalid SQL statement
java.sql.SQLSyntaxErrorException
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:675)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Odscrarr.doODNY(T48COdscrarr.java:98)
at oracle.jdbc.driver.T4CPreparedStatement.doDescribe(T4CPreparedStatement.java:818)
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleResultSetImpl.java:3711)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2799)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)
at com.example.ProcessorTest.testExecuteInsert(ProcessorTest.java:14)

Your saveData method is the cause because an INSERT does not produce a ResultSet , so executeQuery will produce an error, as there will be no ResultSet .

Also you should only use connection.prepareCall(String) for executing stored procedures, not for other types of statements. For a prepared statement you should use connection.prepareStatement(String) .

Change your code to:

public int saveData(String name) throws Exception {
   PreparedStatement preparedStatement = connection.prepareStatement(name);
   preparedStatement.setString(1, name);
   return preparedStatement.executeUpdate();
}

BTW: I hope you are aware that this method uses the parameter name both for the query and as the parameter for the query (which really does not make a lot of sense).

And finally, the new String(...) you do in your main is totally unnecessary.

First of all this line of code

int row = processor.saveData(new String("INSERT INTO NAMES (name) VALUES (nameTable)"));

As you have specified that the method saveData() takes argument as String so there is no need of any new String(...) here and moreover you are using PreparedStatement so you can use parameterized query something like this

int row = processor.saveData("INSERT INTO NAMES (name) VALUES (?)");

Then you can use this statement in saveData()

preparedStatement.setString(1, "name");

Finally you are updating the table not quering it so change this

preparedStatement.executeQuery();

to

return preparedStatement.executeUpdate();

One more thing you should be very specific about your method name. You should always name a method based on what it is going to do. Like saveData() here must be used only for saving data not querying the data as you were trying to do by using the ResultSet . If you want to retrieve the data then use some other method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM