gpt4 book ai didi

java - 准备好的语句的setString不起作用

转载 作者:行者123 更新时间:2023-11-29 01:16:36 25 4
gpt4 key购买 nike

我正在尝试对准备好的语句使用 setString(index, parameter) 方法来创建 ResultSet 但它似乎没有正确插入。我知道查询是正确的,因为我在后面的 else 中使用了相同的查询(减去对 setString 的需要)。这是我目前拥有的代码:

**据我了解,ps.setString(1, "'%"+ committeeCode + "%'"); 应该替换 ?在查询中,但我的输出另有说明。感谢您的帮助。

public String getUpcomingEvents(String committeeCode) throws SQLException{
Context ctx = null;
DataSource ds = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;

StringBuilder htmlBuilder = new StringBuilder();
String html = "";

try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:ConnectDaily");
conn = ds.getConnection();

if(committeeCode != null){
//get all events
String queryStatement = "SELECT " +
.......
"WHERE c.calendar_id = ci.calendar_id AND c.short_name LIKE ? " +
"AND ci.style_id = 0 " +
"AND ci.starting_date > to_char(sysdate-1, 'J') " +
"AND ci.item_type_id = cit.item_type_id " +
"ORDER BY to_date(to_char(ci.starting_date), 'J')";




ps = conn.prepareStatement(queryStatement);

ps.setString(1, "'%" + committeeCode + "%'");
System.out.println(queryStatement);

rs = ps.executeQuery();

if (rs != null){
while(rs.next()){
String com = rs.getString("name");
String comID = rs.getString("short_name");

String startTime = rs.getString("starting_time");
String endTime = rs.getString("ending_time");
String name = rs.getString("contact_name");
String desc = rs.getString("description");
String info = rs.getString("contact_info");
String date = rs.getString("directory");

htmlBuilder.append("<li><a href='?com="+committeeCode+"&directory=2014-09-10'>"+com+" - "+ date +" - "+startTime+" - "+endTime+"</a> <!-- Link/title/date/start-end time --><br>");
htmlBuilder.append("<strong>Location: </strong>"+comID+"<br>");
htmlBuilder.append("<strong>Dial-In:</strong>"+com+"<br>");
htmlBuilder.append("<strong>Part. Code:</strong>"+info+"<br>");
htmlBuilder.append("<a href='http://nyiso.webex.com'>Take me to WebEx</a>");
htmlBuilder.append("</li>");
}
}
html = htmlBuilder.toString();

...

    }catch (NamingException e) {
e.printStackTrace();
//log error and send error email
} catch (SQLException e) {
e.printStackTrace();
//log error and send error email
}finally{
//close all resources here
ps.close();
rs.close();
conn.close();
}
return html;

}

输出

14:18:22,979 INFO  [STDOUT] SELECT  to_char(to_date(to_char(ci.starting_date), 'J'),'mm/dd/yyyy') as start_date,  to_char(to_date(to_char(ci.ending_date), 'J'),'mm/dd/yyyy') as end_date,  to_char(to_date(to_char(ci.starting_date), 'J'),'yyyy-mm-dd') as directory,  ci.starting_time,  ci.ending_time,  ci.description,  cit.description as location,  c.name,  c.short_name,  ci.add_info_url,  ci.contact_name,  ci.contact_info FROM calitem ci, calendar c, calitemtypes cit WHERE c.calendar_id = ci.calendar_id AND c.short_name LIKE ? AND ci.style_id = 0 AND ci.starting_date > to_char(sysdate-1, 'J') AND ci.item_type_id = cit.item_type_id ORDER BY to_date(to_char(ci.starting_date), 'J')

最佳答案

setString 中不需要引号:

ps.setString(1, "%" + committeeCode + "%");

此方法会将指定的 String 绑定(bind)到第一个参数。它不会改变保存在queryStatement中的原始查询String

关于java - 准备好的语句的setString不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331920/

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