gpt4 book ai didi

Java - Mysql - 多重查询

转载 作者:行者123 更新时间:2023-11-29 08:10:06 25 4
gpt4 key购买 nike

字符串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 次执行和 3 次关闭?

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

最佳答案

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

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

当您需要时,使用 PreparedSatement 而不是 Statement:

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

关于Java - Mysql - 多重查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21782852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com