1

Any idea why this doesn't work?

The name of the table is TESTTABLE with just one column called TEST_COLUMN which is the primary key. I'm sure it is something dumb, but thought i'd ask. I'm already connected to the database so i didn't worry about providing that code

Statement statement = connection.createStatement();
String test = "test";
statement.executeUpdate("INSERT INTO TESTTABLE (TEST_COLUMN) VALUES (" + test + ")");

gives me this error

ORA-00984: column not allowed here

1 Answer 1

5

I am assuming that is a varchar column, so try:

statement.executeUpdate("INSERT INTO TESTTABLE (TEST_COLUMN) VALUES ('" + test + "')");

Note the single quotes around the data.

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

5 Comments

The single quotes definitely nailed the problem on the head. @auwall You should look into using PreparedStatementsto help avoid SQL injection.
@auwall: Character data constants in Oracle must be enclosed in single quotes, which is what you're feeding Oracle with this statement. Generally speaking, building dynamic statements this way in Oracle is a Bad Idea, both for the reason @pickypg states as well as performance issues. You should use bind variables whenever possible.
ok i will look into that now. question though. I obtained a statement from connection.createStatement(); How do i obtain a PreparedStatement?
@auwall: From the same conneciton. Just call prepareStatement().
Thank, until 20 minutes ago i had never used anything of this sort and now it's working perfect.

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.