java—将mysql数据库的特定表复制到jdbc中的mariadb数据库

wz3gfoph  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(445)

我正在尝试创建一个java程序,将特定表从一台服务器上的mysql数据库复制到另一台服务器上的mariadb。我知道有mysqldump,但是这个程序的目的是在不同的机器上运行,即使它们没有安装mysql。
我所创建的似乎工作得很好,因为生成的sql看起来确实正确,并且在使用heidisql连接mariadb时执行。但是当我使用 .executeQuery ,我得到一个错误作为我的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执行它的工作。。。
我做错了什么?
提前谢谢,周末愉快

x3naxklr

x3naxklr1#

为了解决我的问题,我补充道 ?allowMultiQueries=true 并使用execute()而不是executequery()。

相关问题