Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ORA-00911: invalid character using PreparedStatements

597647Feb 5 2009 — edited Oct 15 2014
I am getting an ORA-00911 error thrown back in the form of a java.sql.SQLException. I was able to figure out which character in my query was causing the exception, but I'm not sure what to do about it.

I'm calling a method called recordIsPresent which will return true or false, depening on if it finds a matching record. Here is the code:

private static boolean recordIsPresent(PackageBean pb, Connection conn){
boolean isPresent = false;
PreparedStatement stmt = null;
ResultSet rs = null;
String SQL = "SELECT COUNT(*) FROM FEEDER_QUEUE WHERE FQ_ITEMTYPE = 'A10' " +
"AND FQ_ITEMNAME ='" + pb.getPackageName() + "'";

log.debug("Checking for record using SQL: " + SQL);

try {
stmt = conn.prepareStatement(SQL);
rs = stmt.executeQuery();
while(rs.next()){
int i = rs.getInt(1);
if(i > 0){
isPresent = true;
log.debug("A10 Record for package " + pb.getPackageName() + "already present, not resubmitting");
} else {
isPresent = false;
log.debug("No A10 Record found for package " + pb.getPackageName() +", submitting A10");
}
}
} catch (SQLException e) {
e.printStackTrace();
}

return isPresent;
}


The pb.getPackageName() returns a string that is sent with the query. Here's what the SQL looks like in my logs:

recordIsPresent Checking for record using SQL: SELECT COUNT(*) FROM FEEDER_QUEUE WHERE FQ_ITEMTYPE = 'A10' AND FQ_ITEMNAME ='*AllEscCharsChars"?<:>;'/\!()&$*'

If I run the SQL query in sqlplus the extra apostrophe in the string is causing the problen.

I'm using a PeparedStatement which I thought would escape the character for me, but it's not.

Does anyone have any advice on how to proceed? Is there some method I can call to escape the chars before sending the statement?

Thanks in advnace for any help!

-Andrew


This post has been answered by Joe Weinstein-Oracle on Feb 5 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 5 2009
Added on Feb 5 2009
2 comments
2,822 views