【问题标题】:Java - Mysql - Multiple queryJava - Mysql - 多重查询
【发布时间】:2014-03-14 00:29:13
【问题描述】:

字符串 qLink = "";

                        qLink = "INSERT INTO trackgps.queclinklogs(Supplier,NtwProtocol,IMEI,log,DBTIME)" +
                                "VALUES"                    +
                                "("                         +
                                "'" + supplier              + "',"+
                                "'" + protocol              + "',"+
                                "'" + failedQIMEI           + "',"+
                                "'" + failedQLog            + "',"+
                                "'" + currentQuecTimestamp  + "'" +
                                "),"                        +
                                "("                         +
                                "'" + supplier              + "'" + "," +
                                "'" + protocol              + "'" + "," +
                                "'" + QuecLinkIMEI          + "'" + "," +
                                "'" + data2                 + "'" + "," +
                                "'" + currentQuecTimestamp  + "'" +
                                ")";    


                        Statement stmtLink = connQ.createStatement();
                        stmtLink.execute(qLink);
                        stmtLink.close();

字符串字节消耗 = "";

                    bytesconsumption = "INSERT INTO test_backoffice.carrierDataConsumption(IMEI,beginMonth,endMonth,dataConsumed,month,year) VALUES"    +
                                        "("                                                                                                             +
                                        "'"+ QuecLinkIMEI                                                                                   + "'" + "," +
                                        "NOW()"                                                                                                   + "," +
                                        "NOW()"                                                                                                   + "," +
                                        "'"+ totalBytesConsumed                                                                                  + "'," +
                                        "MONTH(NOW())"                                                                                            + "," +
                                        "YEAR(NOW())"                                                                                                   +
                                        ") ON DUPLICATE KEY UPDATE endMonth = NOW(), dataConsumed = dataConsumed + " + totalBytesConsumed;

                    Statement stmtbytesconsumption;

                    stmtbytesconsumption = connQ.createStatement();
                    stmtbytesconsumption.execute(bytesconsumption);
                    stmtbytesconsumption.close();

字符串 qdebug = "";

    qdebug = "INSERT INTO trackgps.rawdata(Module,SubModule,IMEI,listenerTime,msg)" +
            "VALUES"                    +
            "("                         +
            "'"+ "LISTENER TCP"         + "'" + "," +
            "'"+ SubMod                 + "'" + "," +
            "'"+ identifier             + "'" + "," +
            "'"+ listendatetime         + "'" + "," +
            "'"+ msg                    + "'" +
            ")";    

    Statement stmtqdebug = conn.createStatement();
    stmtqdebug.execute(qdebug);
    stmtqdebug.close();

有没有办法在一个 java 语句中执行这三个插入? 而不是创建 3 个执行 3 次关闭的语句?

我还有其他问题,我应该使用 Statements 还是 PrepareStatements?

【问题讨论】:

    标签: java mysql multi-query


    【解决方案1】:

    您可以在一个语句中调用所有 3 个查询:

    Statement stmt = conn.createStatement();
    stmt.executeUpdate(qLink);
    stmt.executeUpdate(bytesconsumption);
    stmt.executeUpdate(qdebug);
    stmt.close();
    

    根据需要使用PreparedSatement 而不是Statement

    • 使用不同的参数集多次执行同一语句
    • 不要关心参数格式,例如。如果 listendatetime 是 Timestamp 类型,您可以只使用 ps.setTimestamp(4, listendatetime) 并且驱动程序在底层数据库上独立地对其进行正确格式化。

    【讨论】:

      【解决方案2】:

      您可以使用“;”连接查询字符将其拆分并在唯一的语句中执行。

      查询:

      "INSERT INTO table1 ... ;INSERT INTO table2 ...;"
      

      和执行:

      Statement st = conn.createStatement();
      st.execute("INSERT INTO table1 ... ;INSERT INTO table2 ...;");
      st.close();
      

      干杯

      【讨论】:

      • tks :)。我要试试看。
      • 它不起作用:(。它只在我单独准备语句时起作用。
      • 您可以使用@agad 解决方案! :)
      • 谢谢@fastebro。我会用它:)
      猜你喜欢
      • 2014-10-28
      • 2023-03-26
      • 2014-10-22
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2011-04-28
      • 2011-12-29
      • 1970-01-01
      相关资源
      最近更新 更多