我正在尝试将具有多个同名的表的MySQL数据插入到Access数据库中
问题:我不确定应该如何建立连接,因为我只连接了一个数据库(MySQL),现在我需要连接到一个mdb (Access)。我已经到了可以使用"UCanAccess“的地步了,但它只是一个很小的框架,我不确定它是否能工作。
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Class clazz2 = Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection MySQL_con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
Connection Acc_con = DriverManager.getConnection(URL2);
//How can I connect to Access and MySQL?
PreparedStatement pst = MySQL_con.prepareStatement(
"INSERT INTO DB2.dbo.table1
SELECT * FROM DB1.dbo.table1
WHERE DB1.table1.x='5';");发布于 2017-10-04 22:42:23
MySQL连接器/J无法写入Access数据库,UCanAccess也无法从MySQL数据库读取数据,因此您无法使用问题中描述的单个语句复制数据。相反,您应该使用MySQL连接进行阅读,使用UCanAccess连接进行写入,如下所示:
try (
Connection mysqlConn = DriverManager.getConnection(mysqlConnUrl);
Statement mysqlStmt = mysqlConn.createStatement();
ResultSet mysqlRs = mysqlStmt.executeQuery(
"SELECT client_id, last_name FROM client WHERE x = '5'");
Connection ucanaccessConn = DriverManager.getConnection(ucanaccessConnUrl);
PreparedStatement ucanaccessStmt = ucanaccessConn.prepareStatement(
"INSERT INTO client (client_id, last_name) VALUES (?, ?)")) {
while (mysqlRs.next()) {
ucanaccessStmt.setInt(1, mysqlRs.getInt(1));
ucanaccessStmt.setString(2, mysqlRs.getString(2));
ucanaccessStmt.executeUpdate();
}
}https://stackoverflow.com/questions/46548923
复制相似问题