zoukankan      html  css  js  c++  java
  • java.sql.SQLException Parameter index out of range (0 1 ).

    不能 preparedStatement.setString(0, "" + account_no); 导致: java.sql.SQLException: Parameter index out of range (0 < 1 ).
     
     我的代码如下:
    PreparedStatement preparedStatement = connection
        .prepareStatement("select * from  account where account_no = '#{account_no}';");
    preparedStatement.setString(1, "" + account_no);
    ResultSet resultSet = preparedStatement.executeQuery();
    String x = null;
    while (resultSet.next()) {
       boolean next = resultSet.next();
       double aDouble = resultSet.getDouble(1);
       double freezed_amount = resultSet.getDouble(2);
       x = aDouble + " -- " + freezed_amount;
       System.out.println(x);
    }
     
    不能这样 preparedStatement.setString(0, "" + account_no);
     
    而是 preparedStatement.setString(1, "" + account_no);
     
    因为
    parameterIndex 从1 开始..
     
    java.sql.PreparedStatement void setString(int parameterIndex,
                   String x)
    throws SQLException
    Sets the designated parameter to the given Java String value. The driver converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's size relative to the driver's limits on VARCHAR values) when it sends it to the database.
    
    Params:
    parameterIndex – the first parameter is 1, the second is 2, ...
    x – the parameter value
    Throws:
    SQLException – if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closed PreparedStatement
    ResultSet 的 getXxx方法也是 从1 开始;官方的注释是:
    java.sql.ResultSet public abstract double getDouble(int columnIndex) throws java.sql.SQLException Retrieves the value of the designated column in the current row of this ResultSet object as a double in the Java programming language. Params: columnIndex – the first column is 1, the second is 2, ... Returns: the column value; if the value is SQL NULL, the value returned is 0 Throws: java.sql.SQLException – if the columnIndex is not valid; if a database access error occurs or this method is called on a closed result set
     
    +++++++++++
     
    where account_no = #{account_no}
    where account_no = '#{account_no}'
    where account_no = '?'
    上面三种sql 报错:java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
     
    所以只能 where account_no = ?
     


    版权声明
    本文原创发表于 博客园,作者为 阿K .     本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
    欢迎关注本人微信公众号:觉醒的码农,或者扫码进群:

  • 相关阅读:
    深度学习之Python 脚本训练keras mnist 数字识别模型
    Hive udtf 报错 java.lang.String cannot be cast to java.lang.Integer
    vue 中 created 和 mounted 钩子生命周期 问题
    vue和 jsplumb 集成 出现下面的错误
    Cognos 中 javascript jQuery 的使用
    Hadoop 下常用的命令
    剑指offer30:连续子数组的最大和
    剑指offer29:最小的k个数
    剑指offer28:找出数组中超过一半的数字。
    剑指offer27:按字典序打印出该字符串中字符的所有排列
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/14305312.html
Copyright © 2011-2022 走看看