I need to select multiple columns from a table which have multiple result set value. I found some question already been answered, but couldn't find a specific one that would help me in my condition.
我需要从表中选择多个具有多个结果集值的列。我发现一些问题已经得到解答,但找不到能帮助我的具体问题。
I tried this but still no luck:
我试过这个但仍然没有运气:
String dboutp = "";
try {
DBConnect Database = new DBConnect();
Connection con = Database.getcon();
String query="SELECT ITEM, PHP FROM table WHERE name=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, name);
ResultSet rs=ps.executeQuery();
int columnCount = 0;
Object[] row = new Object[columnCount];
for (int i = 1; i <= columnCount; ++i) {
row[i - 1] = rs.getString(i); // Or even rs.getObject()
System.out.println(row[i-1]);
}
DefaultTableModel model = null;
model.addRow(row);
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
I also tried:
我也尝试过:
String dboutp = "";
try {
DBConnect Database = new DBConnect();
Connection con = Database.getcon();
String query="SELECT ITEM, PHP FROM table WHERE name=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, name);
ResultSet rs=ps.executeQuery();
int i=1;
while(rs.next()){
dboutp=dboutp+","+rs.getString(i);
i++;
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
At first method in line model.addRow(row)
I'm getting an exception saying java.lang.NullPointerException
, and in second method this exception occurs: java.sql.SQLException: Column Index out of range, 2 > 1
.
在第一个方法在行model.addRow(行)我得到一个异常说java.lang.NullPointerException,在第二个方法中发生这种异常:java.sql.SQLException:列索引超出范围,2> 1。
I would really appreciate if someone tell me what is the appropriate way of accessing multiple result set values including multiple column values.
如果有人告诉我访问多个结果集值(包括多个列值)的适当方法,我将非常感激。
1 个解决方案
#1
1
It looks you are conflating "rows" in the resultset, and "columns" in the row.
看起来你在结果集中混淆了“行”,在行中混淆了“列”。
Loop through the rows. And for each row, get the values of the columns.
循环遍历行。并为每一行,获取列的值。
To get something working, start with something simple. You can use a variable to count the number of rows fetched. Just don't confuse the number of rows you've fetched with the number of columns in the row.
为了让事情变得有效,从简单的事情开始。您可以使用变量来计算提取的行数。只是不要将您获取的行数与行中的列数混淆。
ResultSet rs=ps.executeQuery();
int rn = 0;
while(rs.next()) {
rn++;
String item = rs.getString(1);
String php = rs.getString(2);
System.out.println(" rn=" + rn + " item=" + item + " php=" + php);
}
System.out.println(" fetched total of " + rn + " rows");
#1
1
It looks you are conflating "rows" in the resultset, and "columns" in the row.
看起来你在结果集中混淆了“行”,在行中混淆了“列”。
Loop through the rows. And for each row, get the values of the columns.
循环遍历行。并为每一行,获取列的值。
To get something working, start with something simple. You can use a variable to count the number of rows fetched. Just don't confuse the number of rows you've fetched with the number of columns in the row.
为了让事情变得有效,从简单的事情开始。您可以使用变量来计算提取的行数。只是不要将您获取的行数与行中的列数混淆。
ResultSet rs=ps.executeQuery();
int rn = 0;
while(rs.next()) {
rn++;
String item = rs.getString(1);
String php = rs.getString(2);
System.out.println(" rn=" + rn + " item=" + item + " php=" + php);
}
System.out.println(" fetched total of " + rn + " rows");