我正在尝试创建一个Java程序,将特定表从一台服务器上的MySQL数据库复制到另一台服务器上的MariaDB。我知道有mysqldump,但是程序的目的是要在不同的机器上运行,即使它们没有安装mysql。

我创建的内容似乎运行良好,因为生成的SQL看起来确实正确,并且例如在我使用HeidiSQL连接到MariaDB时执行。但是,当我使用.executeQuery执行sql时,收到一条错误消息,提示我的SQL存在问题。

代码基本上是:

        import java.sql.*

        public class JdbcTest {
        Connection conn = null;
        Connection conn_2 = null;
        Statement stmt = null;
        Statement stmt_2 = null;

        //MySQL
        Class.forName("com.mysql.cj.jdbc.Driver");
        //MariaDB
        Class.forName("org.mariadb.jdbc.Driver");

        System.out.println("Connecting to DB1");
        conn = DriverManager.getConnection(jdbc:mysql://server1/database,USER,PASS);

        System.out.println("Connecting to DB2");
        conn_2 = DriverManager.getConnection(jdbc:mariadb://server2/database,USER_2,PASS_2);

        List<String> ConnToTableNames = new ArrayList<String>();
        ConnToTableNames.add("table1");
        ConnToTableNames.add("table2");

        //Create queries for all tables in ConnToTableNames
        stmt = conn.createStatement();
        String exportSql = "";
        for(String table : ConnToTableNames) {
            //getting table structure
            exportSql += "\n" +
                    "-- ----------------------------\n" +
                    "-- Table structure for `" + table+
                    "`\n-- ----------------------------\n"+
                    "DROP TABLE IF EXISTS `"+table+"`;\n";
            ResultSet rs2 = stmt.executeQuery("SHOW CREATE TABLE `" + table + "`");
            rs2.next();
            exportSql += rs2.getString(2) + ";\n" +
                    "-- ----------------------------\n";
            rs2.close();

            //adding the content
            ResultSet rss = stmt.executeQuery("select * from "+table);
            while (rss.next()) {
                int colCount = rss.getMetaData().getColumnCount();
                if (colCount > 0) {
                    exportSql += "INSERT INTO `" + table +"` VALUES(";

                    for (int i = 0; i < colCount; i++) {
                        if (i > 0) {
                        exportSql += ",";
                        }
                            String s = "";
                        try {
                            s += "'";
                            s += rss.getObject(i + 1).toString();
                            s += "'";
                        } catch (Exception e) {
                            s = "NULL";
                        }
                        exportSql += s;
                    }
                    exportSql += ");\n";
                }
            }
            rss.close();
        }

        //putting everything into the VM
        stmt_2 = conn_2.createStatement();
        stmt_2.executeQuery(exportSql);

        stmt.close();
        conn.close();
        stmt_2.close();
        conn_2.close();
        }


我得到的错误是:

java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE `supplier` (
  `NUMMER` int(10) NOT NULL DEFAULT '0',
  `PRIO` int(' at line 5


真正令我困惑的是:如果我将exportSql转储到终端或文件中并通过HeidiSQL执行该命令,它将起作用...

一个想法,我做错了什么?

在此先感谢,祝您周末愉快

最佳答案

为了解决我的问题,我在连接字符串中添加了?allowMultiQueries=true并使用了execute()而不是executeQuery()。

10-06 05:48
查看更多