在Java中,如何将具有设置值的不同MySQL查询合并到单个查询中?
我想将四个不同的SQL查询合并到一个查询中,以减少SQL连接的使用 这是我的代码:在Java中,如何将具有设置值的不同MySQL查询合并到单个查询中?,java,mysql,sql,join,Java,Mysql,Sql,Join,我想将四个不同的SQL查询合并到一个查询中,以减少SQL连接的使用 这是我的代码: long WTPty = 0L; // holds some value from other part of program long NTPty = 0L; // holds some value from other part of program long ETPty = 0L; // holds some value from other part of
long WTPty = 0L; // holds some value from other part of program
long NTPty = 0L; // holds some value from other part of program
long ETPty = 0L; // holds some value from other part of program
long STPty = 0L; // holds some value from other part of program
Statement stmt = (Statement) conn.createStatement();
//query 1
String w_tblpty="update tbl_priority SET total='"+WTPty+"' where priority= 'west'";
stmt.executeUpdate(w_tblpty);
//query 2
String n_tblpty="update tbl_priority SET total='"+NTPty+"' where priority= 'north'";
stmt.executeUpdate(n_tblpty);
//query 3
String s_tblpty="update tbl_priority SET total='"+STPty+"' where priority= 'south'";
stmt.executeUpdate(s_tblpty);
//query 4
String e_tblpty="update tbl_priority SET total='"+ETPty+"' where priority= 'east'";
stmt.executeUpdate(e_tblpty);
我的目标是减少SQL连接的使用并优化代码。是否可以将上述四个查询合并为一个查询 您可以对语句对象使用
addBatch
方法:
long WTPty = 0L; // holds some value from other part of program
long NTPty = 0L; // holds some value from other part of program
long ETPty = 0L; // holds some value from other part of program
long STPty = 0L; // holds some value from other part of program
Statement stmt = (Statement) conn.createStatement();
//query 1
String w_tblpty="update tbl_priority SET total='"+WTPty+"' where priority= 'west'";
//query 2
String n_tblpty="update tbl_priority SET total='"+NTPty+"' where priority= 'north'";
//query 3
String s_tblpty="update tbl_priority SET total='"+STPty+"' where priority= 'south'";
//query 4
String e_tblpty="update tbl_priority SET total='"+ETPty+"' where priority= 'east'";
stmt.addBatch(w_tblpty);
stmt.addBatch(n_tblpty);
stmt.addBatch(s_tblpty);
stmt.addBatch(e_tblpty);
stmt.executeBatch();
您可以对语句对象使用
addBatch
方法:
long WTPty = 0L; // holds some value from other part of program
long NTPty = 0L; // holds some value from other part of program
long ETPty = 0L; // holds some value from other part of program
long STPty = 0L; // holds some value from other part of program
Statement stmt = (Statement) conn.createStatement();
//query 1
String w_tblpty="update tbl_priority SET total='"+WTPty+"' where priority= 'west'";
//query 2
String n_tblpty="update tbl_priority SET total='"+NTPty+"' where priority= 'north'";
//query 3
String s_tblpty="update tbl_priority SET total='"+STPty+"' where priority= 'south'";
//query 4
String e_tblpty="update tbl_priority SET total='"+ETPty+"' where priority= 'east'";
stmt.addBatch(w_tblpty);
stmt.addBatch(n_tblpty);
stmt.addBatch(s_tblpty);
stmt.addBatch(e_tblpty);
stmt.executeBatch();
如,使用,和。此外,使用:
如,使用,和。此外,使用:
大家好,任何一段代码都会受到高度赞赏并提前感谢。首先,开始使用参数化SQL和准备好的语句,而不是直接将值放入SQL中。然后使用
addBatch
…UPDATE
语句不是查询。“查询”在英语中被定义为“问题或信息请求”。在SQL上下文中,这意味着一个SELECT
语句。您好,任何一段代码都会受到高度赞赏&提前感谢。首先,开始在准备好的语句中使用参数化SQL,而不是直接将值放入SQL中。然后使用addBatch
…UPDATE
语句不是查询。“查询”在英语中被定义为“问题或信息请求”。在SQL上下文中,这意味着一个SELECT
语句。我试试看。非常感谢:)不客气!如果有效,不要忘记验证答案;)看起来不错。我试试看。非常感谢:)不客气!如果有效,不要忘记验证答案;)更安全的版本:p@Andreas非常感谢:)更安全的版本:p@Andreas非常感谢:)