Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
|
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2003, 12:16 AM
syu syu is offline
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default PreparedStatement Problem

Hi,

I can't get what I want when I run the following code. It should have 2 records, but only the 1st recordNumber and Callnumber returned. Any one can help me out? Thank you.
================================================== =======
try{
    dbc = new DatabaseConnection();//userdefined class Connection con = dbc.getConnection();
    stmt = dbc.getStatement();
    String sql = "SELECT ITEMRECORDNUMBER, CALLNUMBER FROM ITEM WHERE ITEMRECORDNUMBER = ?";
    PreparedStatement pstmt = con.prepareStatement(sql);
        String strItemRecordNumber = "";
        String strItemCallNumber = "";

        ResultSet rs = null;
    query = "SELECT ITEMRECORDNUMBER FROM ITEM WHERE BIBLIOGRAPHICRECORDNUMBER = '" + strBibliographicRecordNumber + "'";
    rst = stmt.executeQuery(query);
    while (rst.next()){
            strItemRecordNumber = rst.getString(1);
            pstmt.setString(1, strItemRecordNumber);
            rs = pstmt.executeQuery();
            while(rs.next()){
             strItemRecordNumber = rs.getString(1);
                         strItemCallNumber = rs.getString(2);
                         System.out.println (strItemRecordNumber);
                 System.out.println (strItemCallNumber);
                         } rs.close();
        }
        rst.close();
        stmt.close();
        dbc.disconnectDB();
          }catch(SQLException sqle){}


 
Old December 23rd, 2003, 08:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try your SQL without using a prepared statement. This way you can prove that it isn't the data that is causing the problem.

Cheers

Martyn
 
Old December 23rd, 2003, 08:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Something else you could try is to detetermine the number of records in the result set:

rs.last();
System.out.println("Number of Records: " + rs.getRow());

Cheers

Martyn
 
Old December 27th, 2003, 10:03 PM
syu syu is offline
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried your first method, but still only got the 1st record. Is that because I am using the same connection?

 
Old December 28th, 2003, 01:01 AM
syu syu is offline
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great News!!

I solved the problem by using ArrayList. Methods that I used include add(Object o) and size().

Thanks anyway






Similar Threads
Thread Thread Starter Forum Replies Last Post
PreparedStatement benifits more gokul_blr Java Databases 2 August 17th, 2004 12:16 AM
PreparedStatement not working properly wslyhbb Java Databases 3 September 9th, 2003 01:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.