Results 1 to 3 of 3
Thread: Help me. PreparedStatement
- 06-28-2007, 04:48 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
Help me. PreparedStatement
Basically, I'm trying to figure out how to use a PreparedStatement with an oracle sequence.
I tried setString(1, "myOraclesequence.nextval") but i get an sql exceptionJava Code:Connection con = getConnectionFromSomewhere(); PreparedStatement ps = con.perpareStatment( " Insert into tableA (id, name) values (?,?); ps.set???( 1, myOraclesequence.nextval); <--dunno how to do this line ps.setString(1, "TFECW");
because id is a number type in oracle and i'm sending it as a string. Java obviously won't compile something like
My search yeilded this linkJava Code:setInt(1, myOraclesequence.nextval);
While the solution there makes sense, I looking for something that doesn't require a db change and all the forms, essays, and tps reports that go along with a db change.
Is there something I can wrap my sequence in?
Basically, we're trusting data from an outside system. Well, they are sending us bad data this causes an sql exception because the ' aren't escaped.
I know it'd take less than 2 seconds to add a method to escape the SQL characters, but i'm trying to avoid it. We have enough gum and duck tape holding this application together.
Thanks.
Felissa:p
- 06-28-2007, 04:58 PM #2
Member
- Join Date
- Jun 2007
- Posts
- 92
- Rep Power
- 0
RE: Help me. PreparedStatement
What is your DDL like? The reason I ask is that I usually don't actually insert into sequence columns - I let the database get the next sequence.
However, there are times when you have a foreign key constraint that requires you to get the sequence for a child table. In that case you'll need to first select the next value for the sequence:
Once you have that you can use it in the parent insert and in any child inserts. The downside is that you have to do a select before the insert but you could put that off into another method to make it a bit easier.Java Code:select sequence_name.NEXTVAL from dual
Marcus:cool:
- 06-28-2007, 05:03 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
RE: Help me. PreparedStatement
The application is pretty basic. All it does is insert, update, and select on the just a few tables. So i'm not sure what you mean by DDL
What we were doing before hand was something like
That didn't cause problems, but i'd have to manually escape the chars.Java Code://this is manualy typed in so i'm sure i'm missing a ton of 's and ,s //another reason i'm not a fan of donig it this way StringBuffer sql = "insert into tableA (id, name) values ( '"; sql.append("mySequence.nextval"); sql.append(' " , " '); sql.append(someMethod.getDataFromXml("name")); sql.append("' )"); //set up connection stuff newStatement.executeUpdate(sql.toString());
Unless i'm not understanding a parent child table relationship, couldn't a possibilty arise that when i pulled back the nextval to insert, i could get an insert from a different user which would cause an exception when i tried to insert?
Thanks.
Felissa:p
Similar Threads
-
PreparedStatement with java.sql.Date
By Java Tip in forum Java TipReplies: 0Last Post: 02-10-2008, 11:39 AM -
Using PreparedStatement for insertion
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:30 AM -
Using PreparedStatement
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:24 AM -
PreparedStatement
By Java Tip in forum Java TipReplies: 0Last Post: 12-05-2007, 03:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks