简体   繁体   English

准备声明

[英]PreparedStatement

I am using PreparedStatement's setString method to set values for the start and end dates in a sql query. 我正在使用PreparedStatement的setString方法来设置sql查询中开始日期和结束日期的值。

String sql = "..... " AND tableA.time BETWEEN ? " +
                    " AND ?";

PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, startDate);
st.setString(2, endDate);

The values however are not being set. 但是,未设置值。 I understand that normally there is an equals sign: "tableA.member_id = ?" 我了解通常会有一个等号:“ tableA.member_id =?” +" +”

How do I than call setString method when I am using a 'Between' operator in the sql statement? 当我在sql语句中使用“ Between”运算符时,如何调用setString方法? Hope someone can advise. 希望有人可以指教。 Thank you. 谢谢。

See http://docs.oracle.com/cd/B28359_01/server.111/b28286/conditions011.htm 参见http://docs.oracle.com/cd/B28359_01/server.111/b28286/conditions011.htm

"BETWEEN" can be used with datetime data types. “ BETWEEN”可以与日期时间数据类型一起使用。 However, if tableA.time is a datetime field of some kind (Timestamp, etc.), using st.setString(...) won't work. 但是,如果tableA.time是某种日期时间字段(Timestamp等),则无法使用st.setString(...)。 You need to use setDate(dt) or setTimestamp(ts) instead. 您需要改用setDate(dt)或setTimestamp(ts)。 See Using setDate in PreparedStatement for more info. 有关更多信息,请参见在PreparedStatement中使用setDate

Use setDate . 使用setDate

Is your startDate and endDate a java.util.Date object? 您的startDateendDatejava.util.Date对象吗?

If it is, you can use this code. 如果是这样,则可以使用此代码。

st.setDate(1, new java.sql.Date(startDate.getTime()));
st.setDate(2, new java.sql.Date(endDate.getTime()));

If it is not. 如果不是这样。 Convert that first ro java.util.Date object. 转换第一个ro java.util.Date对象。

BETWEEN expects its arguments to have a type that is compatible with the column being tested. BETWEEN期望其参数具有与要测试的列兼容的类型。 Giving it string arguments for a date column won't work. 为日期列提供字符串参数将不起作用。

You could add a function to the SQL in order to convert the string to a date (I don't know what database you're using, this example uses Oracle's to_date function): 您可以向SQL添加函数以将字符串转换为日期(我不知道您使用的是哪个数据库,此示例使用Oracle的to_date函数):

from tableA.time BETWEEN to_date(?, 'yyyy/mm/dd') AND to_date(?, 'yyyy/mm/dd')

Alternatively you could leave the SQL alone and use setDate on the PreparedStatement, like: 或者,您可以不使用SQL,而在PreparedStatement上使用setDate,例如:

setDate(1, new SimpleDateFormat("yyyy/MM/dd").parse(startDate));

This second way is more usual, it has the advantage of avoiding having to add a date-conversion function (that may be non-standard) to your SQL. 第二种方法更常见,它的优点是避免向SQL添加日期转换功能(可能是非标准的)。

But you would have to do the conversion on one side or the other. 但是您将必须在一侧或另一侧进行转换。 The database's SQL parser won't convert it for you. 数据库的SQL解析器不会为您转换它。 If the database took responsibility for the conversion and there was a mistake it could introduce data errors silently, the less error-prone alternative is for the application developer to tell the database how the date string should be converted. 如果数据库负责转换,并且有一个错误,它可能会无提示地引入数据错误,那么错误率较低的替代方法是让应用程序开发人员告诉数据库应如何转换日期字符串。

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

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