简体   繁体   中英

Formatting output of an SQL query in Java

I've created a simple database with a one to many relationship. For my program I used tables owners and guitars. I need to run a single SQL query to grab all the data, and then print the owner's name once, and all of their guitars and the tuples each on their own indented line.

The problem I'm running into is that with ResultSet, it seems that you can only access a database value a single time, and you must iterate through the entire result set if you don't want it throwing errors.

My query is as follows:

    static String queryToBeExecuted = 
    "SELECT Owners.ownerFirst, Owners.ownerLast, Guitars.guitarYear, 
    Guitars.guitarMakeModel, Guitars.ownerID 
    FROM Owners, Guitars 
    WHERE Owners.ownerID = Guitars.ownerID 
    ORDER BY Owners.ownerID";

My current code for printing the information is this:

while(myResultTuples.next()){
        System.out.print("First name: " + myResultTuples.getString(1) + 
       ", Last name: " + myResultTuples.getString(2) + "\n");
        System.out.print("\tYear: " + myResultTuples.getString(3) + 
       ", Guitar: " + myResultTuples.getString(4) + ", Owner ID: " + myResultTuples.getString(5) + "\n");  
}

This prints the output:

First name: John, Last name: Smith
    Year: 2015, Guitar: Gibson Les Paul, Owner ID: 1
First name: John, Last name: Smith
    Year: 1969, Guitar: Fender Stratocaster, Owner ID: 1
First name: James, Last name: White
    Year: 1972, Guitar: Gibson Firebird, Owner ID: 3
First name: James, Last name: White
    Year: 2006, Guitar: Ibanez RG2550E, Owner ID: 3
First name: Danny, Last name: Quinn
    Year: 2006, Guitar: PRS CE-22, Owner ID: 6
First name: Danny, Last name: Quinn
    Year: 1993, Guitar: Fender Stratocaster, Owner ID: 6
First name: Danny, Last name: Quinn
    Year: 2010, Guitar: Martin OM-28V, Owner ID: 6
First name: Heather, Last name: Jones
    Year: 2005, Guitar: Gibson SG, Owner ID: 7
First name: Heather, Last name: Jones
    Year: 2018, Guitar: Fender Telecaster, Owner ID: 7

I want the output to look like this:

First name: John, Last name: Smith
    Year: 2015, Guitar: Gibson Les Paul, Owner ID: 1
    Year: 1969, Guitar: Fender Stratocaster, Owner ID: 1
First name: James, Last name: White
    Year: 1972, Guitar: Gibson Firebird, Owner ID: 3
    Year: 2006, Guitar: Ibanez RG2550E, Owner ID: 3
First name: Danny, Last name: Quinn
    Year: 2006, Guitar: PRS CE-22, Owner ID: 6
    Year: 1993, Guitar: Fender Stratocaster, Owner ID: 6
    Year: 2010, Guitar: Martin OM-28V, Owner ID: 6
First name: Heather, Last name: Jones
    Year: 2005, Guitar: Gibson SG, Owner ID: 7
    Year: 2018, Guitar: Fender Telecaster, Owner ID: 7

I've been trying to figure this out for around 7 hours now and I just can't understand how it's possible unless there's some obvious solution that I'm missing.

The current code I've been trying to get working is this:

int prevID = 0;
while(myResultTuples.next()){
    prevID = Integer.parseInt(myResultTuples.getString(5));
    System.out.print("First name: " + myResultTuples.getString(1) + ", Last name: " + myResultTuples.getString(2) + "\n");
    while(myResultTuples.next() && prevID == Integer.parseInt(myResultTuples.getString(5))){
        if (prevID == Integer.parseInt(myResultTuples.getString(5)))
            System.out.print("\tYear: " + myResultTuples.getString(3) + ", Guitar: " + myResultTuples.getString(4) + ", Owner ID: " + myResultTuples.getString(5) + "\n");  
        else
            break;
    }
}  

The idea is that I store the previous ownerID from the Guitars table and then print out the owner first and last name. Then I enter another loop that checks that they are the same person and the program will just continue to spit out their owned guitars until it hits a new owner, break the loop and prints out the name again (I know break is bad practice, I'll change it to a bool flag if I can get it working).

The problem is that I don't think you can access the same database value twice with ResultSet. This code prints the first and last name and then gives the error:

First name: John, Last name: Smith
java.sql.SQLException: No data found
Unexpected exception : java.sql.SQLException: No data found, sqlstate = null
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7137)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3906)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
    at HW2Buckridge.main(HW2.java:90)

If anyone can give me a direction to go in, that would be pretty nice.

This is a presentation layer issue, and you should handle it from Java:

String OwnerID = null;

while (myResultTuples.next()) {
    String OwnerIDNew = myResultTuples.getString(5);
    if (!OwnerIDNew.equals(OwnerID)) {
        System.out.print("First name: " + myResultTuples.getString(1) +
            ", Last name: " + myResultTuples.getString(2) + "\n");
        OwnerID = OwnerIDNew;
    }
    System.out.print("\tYear: " + myResultTuples.getString(3) + 
        ", Guitar: " + myResultTuples.getString(4) + ", Owner ID: " +
        myResultTuples.getString(5) + "\n");  
}

The above logic says to print the first and last name line only when encountering a new OwnerID record. This ensure that first/last name gets printed only once per owner. The ORDER BY clause of your query ensures that the result set would already be sorted by owner.

String lastOwnId = "";

while(myResultTuples.next())

{
    
    String currentOwnId = myResultTuples.getString(5);

    if (!lastOwnId.equals(currentOwnId)) {
        //not same user need to print once ,if same user ,so don't print
       System.out.print("First name: " + myResultTuples.getString(1) +
            ", Last name: " + myResultTuples.getString(2) + "\n");
    }lastOwnId = currentOwnId;
     System.out.print("\tYear: " + myResultTuples.getString(3) +
                ", Guitar: " + myResultTuples.getString(4) + ", Owner ID: " + currentOwnId +
                "\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM