gpt4 book ai didi

java - 创建一个方法来选择并显示单个表数据库中的所有数据

转载 作者:行者123 更新时间:2023-12-01 05:46:15 27 4
gpt4 key购买 nike

我需要创建一个从名为 Accounts 的单个表数据库中选择所有数据的方法 - 如您所见,我下面的工作仅部分完成,因为我不确定如何继续该方法(我是 Java 新手) ,任何建议。我需要做什么才能使它成为一个完整的、独立的方法?

String select = "Select * from Accounts";


results = statement.executeQuery(select);
}
catch(SQLException e){
System.out.println("Cannot execute query");
e.printStackTrace();
System.exit(1);
}

try{
while (results.next()){
System.out.println("Name: " + results.getInt(1));
System.out.println("Address: " + results.getString(2) + " " +
results.getString(3));
System.out.println("Salary: " + results.getString(4));
System.out.println("Grade: " + results.getString(5));
System.out.println("Year: " + results.getInt(6));
System.out.println("Tax Rate: " + results.getDouble(7));
System.out.println();
}
}
catch(SQLException e){
System.out.println("Error retrieving data");
e.printStackTrace();
System.exit(1);
}

最佳答案

这是你正在看的吗?

 public static Object[][] results() {
int count = 0;
String select = "Select * from Accounts";
results = statement.executeQuery(select);
try {
while (results.next()) {
count++;
}
results = statement.executeQuery(select);


} catch (SQLException e) {
System.out.println("Cannot execute query");
e.printStackTrace();
System.exit(1);
}
Object data[][] = new Object[count][7];
try {
int i = 0;
while (results.next()) {
data[i][0] = results.getInt(1);
data[i][1] = results.getString(2);
data[i][2] = results.getString(3);
data[i][3] = results.getString(4);
data[i][4] = results.getString(5);
data[i][5] = results.getString(6);
data[i][6] = results.getString(7);
i++;
/*
System.out.println("Name: " + results.getInt(1));
System.out.println("Address: " + results.getString(2) + " " +
results.getString(3));
System.out.println("Salary: " + results.getString(4));
System.out.println("Grade: " + results.getString(5));
System.out.println("Year: " + results.getInt(6));
System.out.println("Tax Rate: " + results.getDouble(7));
System.out.println();
*/
}
} catch (SQLException e) {
System.out.println("Error retrieving data");
e.printStackTrace();
System.exit(1);
}
}

我没有对此进行测试,但如果这是您正在寻找的内容,您可以利用它..

--编辑--
您可以调用此函数并使用返回的结果来构造您的 JTable。我猜你如果不看看 link 就知道如何构建 JTable

关于java - 创建一个方法来选择并显示单个表数据库中的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5860362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com