从访问中检索特定行 table
Retrieve specific row from access table
我浏览了这个论坛并在网上搜索了我上述问题的解决方案,但找不到能为我指明正确方向的东西。如有重复请见谅
我正在开发一个 java 项目,我的应用程序在该项目中与 MS Access 2016 数据库进行交互。我的程序的功能之一是查询数据库中的特定记录并在 gui 中显示该记录的数据。这是我检索数据的代码:
int i = 0;
String q = "select * from QueryData where id=123456";
try {
pstmnt=conn.prepareStatement(q);
Object obj ;
rs = pstmnt.executeQuery();
while (rs.next()) {
obj=rs.getObject(i+1);
data.add(obj); //where data is a List object i++;
}
} catch ....
问题是我只获得了该记录中的第一个值(记录的第一列)并且 record/row 中有更多数据可用。
是否是 rs.next()
方法在执行此操作?如果是,我应该使用什么来获取此特定记录中的下一个值?
ResultSet#next()
iterates over the rows in the result set (which, in this case, is just a single row). If you don't know the result set's structure upfront, you can dynamically deduce it from a ResultSetMetaData
对象:
int i=0;
String q="select * from QueryData where id=123456";
try (PreparedStatement pstmnt = conn.prepareStatement(q);
ResultSet rs = pstmnt.executeQuery()) {
ResultSetMetaData rsmd = rs.getMetaData();
// Assume it's just one row.
// If there's more than one, you need a while loop
if (rs.next()) {
for (int i = 0; i < rsmd.getColumnCount(); ++i) {
data.add(rs.getObject(i + 1));
}
}
}
我浏览了这个论坛并在网上搜索了我上述问题的解决方案,但找不到能为我指明正确方向的东西。如有重复请见谅
我正在开发一个 java 项目,我的应用程序在该项目中与 MS Access 2016 数据库进行交互。我的程序的功能之一是查询数据库中的特定记录并在 gui 中显示该记录的数据。这是我检索数据的代码:
int i = 0;
String q = "select * from QueryData where id=123456";
try {
pstmnt=conn.prepareStatement(q);
Object obj ;
rs = pstmnt.executeQuery();
while (rs.next()) {
obj=rs.getObject(i+1);
data.add(obj); //where data is a List object i++;
}
} catch ....
问题是我只获得了该记录中的第一个值(记录的第一列)并且 record/row 中有更多数据可用。
是否是 rs.next()
方法在执行此操作?如果是,我应该使用什么来获取此特定记录中的下一个值?
ResultSet#next()
iterates over the rows in the result set (which, in this case, is just a single row). If you don't know the result set's structure upfront, you can dynamically deduce it from a ResultSetMetaData
对象:
int i=0;
String q="select * from QueryData where id=123456";
try (PreparedStatement pstmnt = conn.prepareStatement(q);
ResultSet rs = pstmnt.executeQuery()) {
ResultSetMetaData rsmd = rs.getMetaData();
// Assume it's just one row.
// If there's more than one, you need a while loop
if (rs.next()) {
for (int i = 0; i < rsmd.getColumnCount(); ++i) {
data.add(rs.getObject(i + 1));
}
}
}