我是春天的新手。这是我的代码,用于从Access DB文件返回值,我需要在返回时捕获响应。
我以不同的方式尝试了它,没有什么好喜欢的。
无法捕获以下代码输出的返回结果。
我正在Jdeveloper中运行它。
任何想法都会受到赞赏。
package accessdatatoebs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AccessClass {
public AccessClass() {
super();
}
public static void main(String[] args) {
// variables
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
// Step 1: Loading or
// registering Oracle JDBC driver class
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}
catch(ClassNotFoundException cnfex) {
System.out.println("Problem in loading"
+ " MS Access JDBC driver");
cnfex.printStackTrace();
}
// Step 2: Opening database connection
try {
String msAccDB = "G:\\RMMDB\\RM.mdb";
String dbURL = "jdbc:ucanaccess://" +msAccDB;
// Step 2.A: Create and
// get connection using DriverManager class
connection = DriverManager.getConnection(dbURL);
// Step 2.B: Creating JDBC Statement
statement = connection.createStatement();
// Step 2.C: Executing SQL and
// retrieve data into ResultSet
resultSet = statement
.executeQuery("SELECT * FROM tbl_Materials");
// System.out.println("ID\tName\t\t\tAge\tMatches");
System.out.println("Material\tAccount");
System.out.println("====\t========");
// processing returned data and printing into console
while(resultSet.next()) {
System.out.println(resultSet.getInt(1) + "\t" +
resultSet.getString(2) + "\t" +
resultSet.getString(3) + "\t" +
resultSet.getString(4));
}
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
finally {
// Step 3: Closing database connection
try {
if(null != connection) {
// cleanup resources, once after processing
resultSet.close();
statement.close();
// and then finally close connection
connection.close();
}
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
return resultSet;
}
}
答案 0 :(得分:-1)
从这里您可以捕获结果并以list的形式返回
我解释为注释的每个新添加的代码
["19", "59", "16" "547"]