0

我正在使用 jpa 和 sql server 2008 开发一些东西,但出现以下错误

Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting date and/or time from character string.
Error Code: 241

Call: SELECT TOP 100 * FROM [database].[dbo].[registro] WHERE (indate between 'Wed Apr 30 01:01:01 COT 2014' and 'Mon May 12 01:01:01 COT 2014') or name like '%and%'
Query: ReadAllQuery(referenceClass=Register sql="SELECT TOP 100 * FROM [database].[dbo].[register] WHERE (indate between 'Wed Apr 30 01:01:01 COT 2014' and 'Mon May 12 01:01:01 COT 2014') or name like '%and%'")

jpa 查询:

Query query = em.createNativeQuery("SELECT TOP 100 * FROM [database].[dbo].[register] WHERE (indate between '"+idate+"' and '"+fdate+"') or name like '%"+name+"%'",Register.class);

有人可以告诉我要更改什么或更好的查询方式。

4

1 回答 1

0

您不应该在本机查询中使用日期字符串。

此外,您必须避免sql injection使用setParemeter.

以下查询应该有效:

String query = "SELECT TOP 100 * FROM [database].[dbo].[register] WHERE (indate between ? and  ?);"

Query query = em.createNativeQuery(query).setParameter(1,[your-date- value],TemporalType.DATE).setParameter(2,[your second-date-value],TemporalType.DATE);

List<Register> registerList = query.getResultList();

我希望这对你有帮助。

于 2014-05-28T12:48:34.200 回答