我使用net.ucanaccess.jdbc.UcanaccessDriver
从MS Access中读取数据:
public static void main(String[] args) {
try {
Statement statement = getConnection().createStatement();
ResultSet resultSet = statement.executeQuery(" select * from students ");
while (resultSet.next()) {
String log = resultSet.getLong("id") + " - " + resultSet.getString("name") + " - " + resultSet.getString("family");
System.out.println(log);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
File f = new File("files/access.accdb");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
connection = DriverManager.getConnection("jdbc:ucanaccess://" + f.getAbsolutePath());
}
catch (Exception e) {
e.printStackTrace();
}
return connection;
}
但是现在,我想从MS Access读取数据InputStream
,可能是这样的:
public static Connection getConnection() {
Connection connection = null;
try {
File f = new File("files/access.accdb");
InputStream inputStream = new FileInputStream(f);
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
connection = DriverManager.getConnection(inputStream); /*Changed*/
}
catch (Exception e) {
e.printStackTrace();
}
return connection;
}
答案 0 :(得分:1)
不,您不能使用UCanAccess直接从InputStream打开Access数据库。但是,您可以使用java.nio.Files.copy将InputStream复制到临时文件,然后让UCanAccess打开它。