JDBC Interface with Oracle9

I am having issues with the following query. One of my co-workers say that this is a result of PreparedStatement being buggy in java, but I thought that was odd since I know may developers user this class.

In the example code below, the "rs.next()" produces a "false" result. When using the same query in the database and substituting the values, I get one row. This contradicts. Also, it is worth noting that if I remove "field_a3" and "field_a4", I get one row back. I have disected my code down to this primitive level with a lot of confusion and fustration. Any information or advise on making this code below work properly would be appreciated.

thanks

query = "SELECT * "+

"FROM table3 WHERE field_a1 = ? AND " +

"field_a2 = ? AND field_a3 = ? AND field_a4 = ?";

Context context = new InitialContext();

DataSource ds = (DataSource) context.lookup("example_ds");

Connection conn = ds.getConnection();

PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1,"L1");

pstmt.setString(2,"hcgs");

pstmt.setString(3,"467759");

pstmt.setString(4,"sc");

ResultSet rs = pstmt.executeQuery();

System.out.println("next is <" + rs.next() + ">");

[1257 byte] By [jring1972a] at [2007-11-27 3:53:20]
# 1

> I am having issues with the following query. One of

> my co-workers say that this is a result of

> PreparedStatement being buggy in java,

Without qualification that statement is completely false.

>

> In the example code below, the "rs.next()" produces a

> "false" result. When using the same query in the

> database and substituting the values, I get one row.

> This contradicts. Also, it is worth noting that if

> I remove "field_a3" and "field_a4",

And what are the field types of those?

Char fields, versus varchar, require padded matches or trims.

jschella at 2007-7-12 8:57:24 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> > I am having issues with the following query. One

> of

> > my co-workers say that this is a result of

> > PreparedStatement being buggy in java,

>

> Without qualification that statement is completely

> false.

>

Well, I am only quoting what he said. I thought it was odd personally. The issue mainly stemmed from using PreparedStatements in conjunction with Informix databases.

> >

> > In the example code below, the "rs.next()" produces

> a

> > "false" result. When using the same query in the

> > database and substituting the values, I get one

> row.

> > This contradicts. Also, it is worth noting that

> if

> > I remove "field_a3" and "field_a4",

>

> And what are the field types of those?

>

> Char fields, versus varchar, require padded matches

> or trims.

The fields are char(2), char(4), char(10) and char(4). Weblogic 8.1 is used for doing the datasource lookup to the Oracle 9 database.

How are padded or trimmed matches done? I assume I am trimming on the selected field to match the value that is being set in the PreparedStatement. I will rewrite the query and see what happens.

jring1972a at 2007-7-12 8:57:24 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
It worked. Trimming the fields fixed the problem. Personally, I forgot about CHAR doing that. This table a co-worker of mine had CHAR and I forgot it pads as opposed to VARCHAR.thanks for the help.
jring1972a at 2007-7-12 8:57:24 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...