1

我得到一个 Java MySQL 异常

@Override

public ResultSet executeQuery (String query, ArrayList<Argument> arguments) throws SQLException
{
    final PreparedStatement pstmt = getPstmt(query, arguments);
    final ResultSet retrievedData = pstmt.executeQuery();
    return retrievedData;
}




//getpstmt()

private PreparedStatement getPstmt (String query, ArrayList<Argument> arguments) throws SQLException
    {
        PreparedStatement pstmt = null;
        try {
            pstmt = connection.prepareStatement(query);

            if (arguments != null) {
                int argPosition = 1;
                for (final Argument arg : arguments) {
                    if (arg.getType() == ARGUMENT_TYPE.INTEGER) {
                        pstmt.setInt(argPosition++, arg.getInt());
                    }
                    else {
                        pstmt.setString(argPosition++, arg.getString());
                    }
                }
            }
        }
        catch (final Exception ex) {
            NmsLogger.writeDebugLog(ex);
            return null;
        }

        return pstmt;

    }



// One instance of calling executeQuery

public String getProperty(String propertyName) 
    {

        try 
        {
            ArrayList<Argument> arguments = new ArrayList<Argument>();
            arguments.add(new Argument(propertyName));

            final java.sql.ResultSet resultset = Application.getDBHandler().executeQuery(SQLQueries.GET_PROPERTY, null);
            Application.getDBHandler().executeQuery(SQLQueries.GET_PROPERTY, arguments);
            if(resultset.next())
            {
                resultset.getString(1);

            }
            return resultset.toString();
        }
        catch (SQLException e)
        {
            NmsLogger.writeDebugLog(e);
            e.printStackTrace();
        }

        return null;
    }

调试以下代码时出现以下错误:

java.sql.SQLException:没有为参数 1 指定值

有人确实建议了一种解决方法..为什么我会遇到这样的异常?

4

1 回答 1

3

您传递null给 executeQuery 而不是这里的参数列表:

Application.getDBHandler().executeQuery(SQLQueries.GET_PROPERTY, null);

?因此,在 query的地方没有值"Select * from app_config where Property = ?",因此是例外。

希望这可以帮助。

于 2012-08-10T05:10:59.810 回答