|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
java, weblogic, oracle, PreparedStatement
I am having difficulty getting prepared statements to work in Weblogic 8.1. I have code that looks like this:
//this works String keyString = "keyID"; String sql = "select * from myTable where myKey = '"+ keyString + "'"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); i = 0; while(rs.next()){ System.out.println("row["+i+"] = ["+rs.getString(1) + "]"); i++; } ps.clearParameters(); rs.close(); //but this returns no data: sql = "select * from myTable where myKey = ?"; ps = con.prepareStatement(sql); ps.setString(1, keyString); rs = ps.executeQuery(); while(rs.next()){ System.out.println("row["+i+"] = ["+rs.getString(1) + "]"); i++; } rs.close(); The 2nd code fragment returns an empty result set, while the first returns me my data. The only difference is whether I use a '?' and parameters or just put the data into single quotes and not use a parameter to the PreparedStatement. But that kind of defeats the purpose of PreparedStatements. I'm using weblogic 8.1. Java 1.4.2 and Oracle 9i. What am I missing or not configuring correctly? How do I make the PreparedStatement work correctly w/ weblogic 8.1? Thanks, Walter Moore |
|
#2
|
||||
|
||||
|
I've read and reread your code and it seems all right to me. But I'd suggest you to put the working code in comment and let the second code (with the parameters and the ps.setString()) do its work.
I have the feeling it could be possible that the cursor of the resultset does not give any records since it already has found all the records of the database. It's just a feeling but I'm very interested to hear if you get any records if you put the working part of your code in comment. |
|
#3
|
|||
|
|||
|
found the solution!
It turns out that the DB field (myKey) is CHAR(3). Oracle pads automatically when you do not use paramters. So in the first case it would trim the keyID. So if the actualy key I was looking for was 'US ', and I passed in 'US', it would pad so that it was 'US '.
BUT, when you use parameters, no such padding occurs. This is an inconsistency with the oracle driver. Once I padded the key with spaces to match what was in the DB, or RTRIM(myKey), data was returned. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > java, weblogic, oracle, PreparedStatement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|