Hi,
I am trying to use PreparedStatement in my code and whenever I try to "set" values using the primitive "setString", "setInt" etc., it does not work. In other words, the values are never set in the query.
As an example, I am trying to query a table "source", and its schema is as follows:
SQL> desc source;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL VARCHAR2(4)
METHOD NUMBER(1)
RES NUMBER(9,2)
And the following is my code snippet:
public static void main(
String[] args)
{
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
String dbURL = "jdbc
racle:thin:@machineip:1521
pi";
Connection conn = DriverManager.getConnection (dbURL, "scott", "tiger");
String query = "SELECT id from source WHERE id = ?";
PreparedStatement prep = conn.prepareStatement(query);
prep.setInt(1, 0);
System.out.println("Query is : " + query);
ResultSet rset = prep.executeQuery();
while (rset.next())
{
System.out.println(rset.getString(1));
}
rset.close();
prep.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
As you can see, using PreparedStatement, the query should be of the form:
SELECT id from source WHERE id = 1
However, when I try to print the query, I get the following:
SELECT id from source WHERE id = ?
Thus, the "question mark" (?) is not replaced. Can anyone tell me what is going on ? FYI, I am using JDK 1.4.2, and Oracle 10g.
Thanks,
- Jyoti