0

字符串 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?

4

2 回答 2

1

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

查询:

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

和执行:

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

干杯

于 2014-02-14T15:21:54.093 回答
1

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

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

在需要时使用PreparedSatement而不是Statement

  • 使用不同的参数集多次执行相同的语句
  • 不要关心参数格式,例如。如果listendatetime是 Timestamp 类型,您可以使用 justps.setTimestamp(4, listendatetime)并且驱动程序在底层数据库上独立地正确格式化它。
于 2014-02-14T15:34:31.637 回答