java.sql.SQLException:参数索引超出范围(1个参数数目,即0).在使用PreparedStatement时 [英] java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). While using PreparedStatement

查看:123
本文介绍了java.sql.SQLException:参数索引超出范围(1个参数数目,即0).在使用PreparedStatement时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java/MariaDb中使用prepareStatement,如以下函数所示

Using preparedStatement in Java/MariaDb as in the following function

public ArrayList<String> findPath(String roomName) throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
    ArrayList<String> path = new ArrayList<String>(); 
    connection = getConnection();
    String queryPattern = "SELECT `Livello_1`, `Livello_2`, `Livello_3`, `Livello_4` FROM Camera WHERE Camera.Nome = '?'";
    PreparedStatement queryStatement = connection.prepareStatement(queryPattern);
    queryStatement.setString(1, roomName);
    ResultSet rs = queryStatement.executeQuery();
    if(rs.next())
    {
        for(int i = 0; i < 3; i++)
        {
            path.add(rs.getString(i));
        }
    }
    return path;
}

我收到错误消息:

java.sql.SQLException:参数索引超出范围(1>个参数,即0).

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

,错误行号指向行

queryStatement.setString(1, roomName);

推荐答案

正如评论中提到的,您应该删除?中的引号.

As was mentioned in the comment, you should remove the quotes from the ?.

此外,在 rs.getString(i)中, i 应该为正.从1开始循环计数.

In addition, in rs.getString(i), i should be positive. Start the count of the loop from 1.

总结:

public ArrayList<String> findPath(String roomName) throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
    ArrayList<String> path = new ArrayList<String>(); 
    connection = getConnection();
    String queryPattern = "SELECT Livello_1, Livello_2, Livello_3, Livello_4 FROM Camera WHERE Camera.Nome = ?";
    PreparedStatement queryStatement = connection.prepareStatement(queryPattern);
    queryStatement.setString(1, roomName);
    ResultSet rs = queryStatement.executeQuery();
    if(rs.next())
    {
        for(int i = 1; i < 4; i++)
        {
            path.add(rs.getString(i));
        }
    }
    return path;
}

这篇关于java.sql.SQLException:参数索引超出范围(1个参数数目,即0).在使用PreparedStatement时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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