gpt4 book ai didi

java - 如何使用 PreparedStatement 和 Case INsensitive 搜索

转载 作者:行者123 更新时间:2023-11-29 13:36:21 26 4
gpt4 key购买 nike

1.如何使用 PrepareStatement 获取 familyname 和 givenname?
2.此外,如何按 familyname 或 givenname 进行不区分大小写的搜索?

String query ="SELECT agent.familyname, agent.givenname" +
" FROM agent" +
" WHERE agent.agentid = piececreation.agentid" +
" AND (LOWER(familyname) = '"+agent_lastname+"' OR LOWER(givenname) = '"+agent_name+"') ORDER by familyname";


PreparedStatement pst = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, agent_lastname);
pst.setString(2, agent_name);
// Executing the insert
pst.executeUpdate();

最佳答案

将 familyName 或 givenName 也设为小写,因为您已经在使用 LOWER DB API

String query ="SELECT agent.familyname, agent.givenname" +
" FROM agent" +
" WHERE agent.agentid = piececreation.agentid" +
" AND (LOWER(familyname) = '"+agent_lastname.toLowerCase()+"' OR LOWER(givenname) = '"+agent_name.toLowerCase()+"') ORDER by familyname";

当您使用PreparedStatement 时,不要直接在您的SQL 中附加值,如果您这样做,您很容易出现SQL Attack。而是参数化您的值(value)观。

String query = 
"SELECT agent.familyname, agent.givenname"
+ " FROM agent"
+ " WHERE agent.agentid = ?"
+ " AND ("
+ " LOWER(familyname) = ? OR LOWER(givenname) = ?"
+ ") "
+ " ORDER by familyname";


pst.setInt(1, piececreation.agentid);
pst.setString(2, agent_lastname.toLowerCase());
pst.setString(3, agent_name.toLowerCase());

然后根据定义调用适当的 setXXX 方法设置值 here .

您可以阅读教程here

关于java - 如何使用 PreparedStatement 和 Case INsensitive 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10326952/

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