Re: JDBC PreparedStatements ?

From: C. Ferguson <c_ferguson_at_rationalconcepts.com>
Date: Sun, 10 Sep 2000 18:43:32 GMT
Message-ID: <39BBD654.4258BA45_at_rationalconcepts.com>


Hi Bob,

   You process the prepared statement the same way you process the statement.

    So,

         PreparedStatement dbPrepStmt=
        dbConn.prepareStatement(qs+"?");

        dbPrepStmt.setString(1,"Robert Morris");

         ResultSet R = dbPrepStmt.executeQuery();
         while (R.next()) {
              System.out.println(R.getString(1)); // If theNumber is a
string
              System.out.println(R.getString(2));
         }
        R.close();
        dbPrepStmt.close();

    Also, as a note. It is considered bad form to cross post to all the groups. The server group is a good place to start...

hth,
cindy

Bob Morris wrote:

> I can't get a PeparedStatement query to return a result set with any
> rows in it. The snippet below shows me that the database is ok and has
> data, that a ResultSet with column names is returned, but that there are
> no rows in the ResultSet.
>
> I'm using Oracle8i 1.1.5, jdk 1.2, and the Oracle8i jdbc thin driver
> supplied with 1.1.5. What am I doing wrong?
>
> Thanks, especially for email replies to ram_at_cs.umb.edu
>
> --Bob Morris
>
> //initialize driver and make connection omitted
> //...
>
> String qs=
> "SELECT theNumber,theAddr FROM PHONEBOOK WHERE theName=";
>
> //try an ordinary Statement; it works
> Statement stmt=dbConn.createStatement();
> R= stmt.executeQuery(qs+"'Robert Morris'");
> R.next();
> System.out.println("1: "+R.getString(1));
>
> //try a PreparedStatement;
> // it has metadata but says"There are no rows"
> PreparedStatement dbPrepStmt=
> dbConn.prepareStatement(qs+"?");
>
> dbPrepStmt.setString(1,"Robert Morris");
> dbPrepStmt.execute();
>
> R = dbPrepStmt.getResultSet();
>
> //show some metadata. it works
> ResultSetMetaData rsmd = R.getMetaData();
> for(int i=0;i<rsmd.getColumnCount();i++)
> System.out.println (rsmd.getColumnLabel(i+1));
>
> // print stuff if there is any
> if (!R.next())
> System.out.println("There are no rows");
> else {
> try {
> System.out.println("NumRows:"+ R.getRow());
> System.out.println("Result:"+ R.getString(1));
> }
> catch(Exception E){E.printStackTrace();}
> }
Received on Sun Sep 10 2000 - 20:43:32 CEST

Original text of this message