java 设置查询参数;参数值与预期类型不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14384650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:05:15  来源:igfitidea点击:

setQueryParams; Parameter value did not match expected type

javasqljakarta-eeprimefaces

提问by Hymanson Bray

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Ok, so I have the user inputing a date, which is to be queried in java using em.createQuery. As you can see from the screenshots, I have the input data going in as pattern="dd-MMM-yy(thats how its stored in the server), but I have also tried this without the pattern tag and it still does not work. The actual error is at the setQueryParams(q)though and I haven't been able to figure out what it is. Anyone have an idea to where I might look? THanks.

好的,所以我让用户输入一个日期,该日期将在 java 中使用em.createQuery进行查询。正如您从屏幕截图中看到的那样,我将输入数据输入为pattern="dd-MMM-yy(这就是它存储在服务器中的方式),但是我也尝试过没有模式标记的情况,但它仍然无法正常工作. 实际的错误是在setQueryParams(q) 上,但我一直无法弄清楚它是什么。有人知道我可能在哪里看吗?谢谢。

回答by matt b

You haven't shown us what qis, but it sounds like you are giving a java.lang.Stringwhere a java.util.Dateis expected.

您还没有向我们展示什么q是,但听起来您给出了java.lang.String一个java.util.Date预期的地方。

Also, what is up with writing strings like

另外,写字符串是怎么回事

queryStr += " and c." + "dateCreated" + " >=:" + ...

? that looks like a pain to maintain/read.

? 这看起来很难维护/阅读。

回答by eric

I agree with Matt b, looks like you're supplying a string and expecting a date (though it's hard to read the text on your screenshot). Take a look at SimpleDateFormat, and use the "parse" method to derive a Date object from a String.

我同意 Matt b 的观点,看起来您正在提供一个字符串并期待一个日期(尽管很难阅读屏幕截图上的文字)。看看SimpleDateFormat,并使用“解析”方法从字符串派生一个 Date 对象。

Edit:

编辑:

It looks like you're date is in a string format that looks something like this:

看起来您的日期是字符串格式,如下所示:

Tue Jan 01 00:00:00 EST 2013

美国东部时间 2013 年 1 月 1 日星期二 00:00:00

Here is code that should parse a string in that format:

这是应该解析该格式字符串的代码:

public static void main(String[] args) {

    String dateString = "Tue Jan 01 00:00:00 EST 2013";
    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
    try {
        Date date = format.parse(dateString);
        System.out.println(date.toString());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I can't tell by looking at your log statements if the square brackets are part of your string or not, if they are you'd have to include those in your format:

我无法通过查看您的日志语句来判断方括号是否是字符串的一部分,如果是,您必须在格式中包含这些内容:

SimpleDateFormat format = new SimpleDateFormat("[EEE MMM dd hh:mm:ss z yyyy]");

Note I don't have your exactdate string here, so I may have made a mistake transcribing it from your image. Use a breakpoint or a print statement to get the exact format of the String q.

注意我这里没有您的确切日期字符串,所以我可能在从您的图像转录它时犯了一个错误。使用断点或打印语句来获取字符串 q 的确切格式。

Of course, this is all under the assumption that q is a string, based on your logs it seems like that's the case.

当然,这一切都假设 q 是一个字符串,根据您的日志,情况似乎就是这样。