在Java中格式化SQL查询的输出。

huangapple go评论82阅读模式
英文:

Formatting output of an SQL query in Java

问题

以下是你要翻译的内容:

我创建了一个带有一对多关系的简单数据库。在我的程序中,我使用了“owners”和“guitars”两个表。我需要运行一个单独的SQL查询来获取所有数据,然后打印所有者的姓名一次,以及他们拥有的吉他和每个吉他元组各自位于自己的缩进行上。

我遇到的问题是,在使用ResultSet时,似乎只能一次访问数据库值,如果不想让它抛出错误,必须遍历整个结果集。

我的查询如下:

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";

我当前用于打印信息的代码如下:

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");  
}

这将打印输出:

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

我希望输出看起来像这样:

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

我已经试图解决这个问题大约7个小时了,但除非有一些我漏掉的明显解决方案,否则我无法理解它是如何可能的。

我目前尝试使其正常工作的代码如下:

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;
    }
}

这个想法是我从“Guitars”表中存储了先前的ownerID,然后打印出所有者的名字。然后我进入另一个循环,检查它们是否是同一个人,程序将继续输出他们拥有的吉他,直到遇到新的所有者,然后中断循环并再次打印出名字(我知道中断是不好的做法,如果我能使其正常工作,我将把它改为布尔标志)。

问题是我不认为可以在ResultSet中两次访问相同的数据库值。这段代码会打印出名字,然后报错:

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)

如果有人能指导我方向,那将非常好。

英文:

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.

答案1

得分: 1

这是一个表示层的问题,你应该从Java中处理它:

String OwnerID = null;

while (myResultTuples.next()) {
    String OwnerIDNew = myResultTuples.getString(5);
    if (!OwnerIDNew.equals(OwnerID)) {
        System.out.print("名字: " + myResultTuples.getString(1) +
            ", 姓氏: " + myResultTuples.getString(2) + "\n");
        OwnerID = OwnerIDNew;
    }
    System.out.print("\t年份: " + myResultTuples.getString(3) + 
        ", 吉他: " + myResultTuples.getString(4) + ", 拥有者编号: " +
        myResultTuples.getString(5) + "\n");  
}

上述逻辑表示只有在遇到新的 OwnerID 记录时才打印名字和姓氏。这确保每位拥有者的名字和姓氏只会打印一次。你的查询中的 ORDER BY 子句确保结果集已经按拥有者排序。

英文:

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.

答案2

得分: 0

String lastOwnId = "";

while (myResultTuples.next()) {

    String currentOwnId = myResultTuples.getString(5);

    if (!lastOwnId.equals(currentOwnId)) {
        // 不同的用户需要打印一次,如果是相同的用户,则不打印
        System.out.print("名字: " + myResultTuples.getString(1) +
            ",姓氏: " + myResultTuples.getString(2) + "\n");
    }
    lastOwnId = currentOwnId;
    System.out.print("\t年份: " + myResultTuples.getString(3) +
        ",吉他: " + myResultTuples.getString(4) + ",所有者 ID: " + currentOwnId +
        "\n");
}
英文:
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");
}

huangapple
  • 本文由 发表于 2020年9月22日 10:59:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64002476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定