java sql语句 date,两个java.sql.Date之间的Select语句

my code is:

java.sql.Date fromDate= new java.sql.Date(date1);

java.sql.Date toDate= new java.sql.Date(date2);

String select = "SELECT * FROM Table WHERE Date between " + fromDate+ " and" + toDate;

I´m using Derby database, and I have to run this query but return error. How can I do? Thanks.

解决方案

First, stop building SQL like that. It's vulnerable to SQL injection attacks, conversion issues (which is probably the problem here) and it's hard to read.

Use parameterized SQL instead:

// TODO: Close the statement, e.g. using a try-with-resources statement

// or a finally block.

PreparedStatement statement =

conn.prepareStatement("SELECT * FROM Table WHERE Date between ? and ?");

statement.setDate(1, fromDate);

statement.setDate(2, toDate);

ResultSet results = statement.executeQuery();

// Use the results

This may well be enough to fix the problems immediately. If it's not, please give more details.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值