简体   繁体   English

如何在Java中串联SQL查询?

[英]How to concatenate sql query in java?

I'm beginning to learn sql and java and I have a problem. 我开始学习sql和java,但遇到了问题。

The logic for the code is: 该代码的逻辑是:

The first part of the sql statement should be "a.stdn_code_ts" and as more elements (in this case student code) are included, I need to concatenate with and OR-Statement. sql语句的第一部分应该是“ a.stdn_code_ts”,随着包括更多元素(在这种情况下为学生代码),我需要与and语句连接。

student codes could be a single value or a range, say for example: '567777' is valid as well as '567777-876677'. 学生代码可以是单个值或范围,例如:“ 567777”和“ 567777-876677”一样有效。

If it is single value, just add "=" then the student code. 如果是单个值,则只需添加“ =”,然后添加学生代码。 In the example if the user entered '567777' then the query should be something like "a.stdnt_code_ts = '567777'" 在示例中,如果用户输入了“ 567777”,则查询应该类似于“ a.stdnt_code_ts ='567777'”

If it is a range, add the first student code then "BETWEEN" second code. 如果是范围,则添加第一个学生代码,然后添加“ BETWEEN”第二个代码。 Ie: if the user entered '567777-876677', the query should be "a.stdnt_code_ts BETWEEN '567777' AND '876677'". 即:如果用户输入“ 567777-876677”,则查询应为“ a.stdnt_code_ts BETWEEN'567777'和'876677'”。

and as I mentioned above, if there are 2 or more student codes the query should be concatenated with an "OR a.stdnt_code_ts" then checks again if it is a single value or a range. 并且如上所述,如果有2个或更多学生代码,则查询应与​​“ OR a.stdnt_code_ts”连接起来,然后再次检查它是否为单个值或范围。

I already have this code and got stuck: 我已经有了这段代码并被卡住:

private void formatStudentCode(Connection connection) throws Exception {
    studentCode = "a.stdnt_code_ts ";

    for(int i=0; i < stdntCode.size(); i++) {
        if (stdntCode.get(i).indexOf("-")==-1) {
            studentCode += "= '" + stdntCode.get(i) + "'";
        }
        else {
            String [] range=stdntCode.get(i).split("-");
            studentCode += "BETWEEN '" + range[0] + "' AND '" + range[1] + 
            "'";
        }
    }
}

First, this code is incomplete, so I'll need to make some guesses. 首先,此代码不完整,因此我需要做出一些猜测。 But let's try. 但是,让我们尝试。

Let's fix the loop first: 让我们先修复循环:

String sql = "SELECT * FROM students";

List<String> terms = new ArrayList<>();
List<String> arguments = new ArrayList<>();
// Don't need index here, for each loop is better
for (String code : codes) {
    // No need for IndexOf
    if (code.contains("-")) {
        terms.add("stdnt_code_ts between ? and ?");
        String[] split = code.split("-");
        arguments.add(split[0]);
        arguments.add(split[1]);
    }
    else {
        // Don't concatenate SQL query parameters
        terms.add("stdnt_code_ts = ?");
        arguments.add(code);
    }
}

Now to put our OR : 现在把我们的OR

if (terms.size() > 0) {
   sql += " WHERE " + Strings.join(terms, " OR ");
}

Now to add actual parameters for each question mark: 现在为每个问号添加实际参数:

PreparedStatement preStmt = conn.prepareStatement(sql);
int count = 0;

for (String code : arguments) {
    preStmt.setString(++count, code);
}

And finally to execute the query: 最后执行查询:

ResultSet rs = preStmt.executeQuery();

Note that I'm not running this code, so I may miss a line or two, but that's the general idea of how it should be done correctly. 请注意,我没有运行此代码,因此我可能会错过一两行,但这是正确完成操作的基本思路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM