如何解决:“参数索引超出范围(2>参数数量,即1)". [英] How to fix: "Parameter index out of range (2 > number of parameters, which is 1)."

查看:101
本文介绍了如何解决:“参数索引超出范围(2>参数数量,即1)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试通过Java(jdbc)从mySql数据库中获取timeStamps(日期对象),则会出现错误:参数索引超出范围(2>参数数量,即1)."

If i try to get the timeStamps (Date object) from my mySql database via Java (jdbc) i get the error: "Parameter index out of range (2 > number of parameters, which is 1)."

我在互联网上找不到适合我的解决方案,因为(我认为)sql查询正确无误,并且我输入了确切的数字?"需要.

I can´t find a solution on the internet that works for me, because (i think) the sql-query is correct and i placed the exact number of '?' needed.

PreparedStatement st = conn.prepareStatement("SELECT * FROM timestamp WHERE stampTime BETWEEN '?/0/? 00:00:00.00' AND '?/31/? 23:59:59.999' AND userid = ? ");
            st.setInt(1, month);
            st.setInt(2, year); //It crashes here
            st.setInt(3, month);
            st.setInt(4, year);
            st.setInt(5, uId);
            ResultSet rs = st.executeQuery();

我希望该语句可以准备并执行,但我收到的错误是参数索引超出范围(2>参数数量,即1)."

I expect the statement to be prepared and executed but i am reciving the error "Parameter index out of range (2 > number of parameters, which is 1)."

推荐答案

问号?是字符串文字的一部分时,不解释为参数指示符.这就是为什么在'?/0/?内出现问号的原因.00:00:00.00''?/31/?23:59:59.999'不算作已准备好的语句的参数;仅计入 userid =?中的?,因为它是公开"中唯一的一个.

Question marks ? are not interpreted as parameter designators when they are part of a string literal. That is why question marks inside '?/0/? 00:00:00.00' and '?/31/? 23:59:59.999' are not counted as parameters of the prepared statement; only the ? inside userid = ? is counted, because it is the only one "in the open".

您可以通过在Java程序中构造范围的结束日期,并将它们绑定到 stampTime BETWEEN中的参数来解决此问题.AND?条件.

You can fix this problem by constructing the end dates of the range in your Java program, and binding them to parameters inside stampTime BETWEEN ? AND ? condition.

这篇关于如何解决:“参数索引超出范围(2>参数数量,即1)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆