1

I get a Java MySQL exception

@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;
    }

I get the following error when I debug the following code :

java.sql.SQLException: No value specified for parameter 1

Someone do suggest a way to solve..Why I get such an exception?

7
  • 3
    can you post your query and also the code in getPstmt? Commented Aug 10, 2012 at 4:44
  • @JohnTotetWoo I have edited the question.. Hope u get it now. Commented Aug 10, 2012 at 4:51
  • Can you add a breakpoint at for (final Argument arg : arguments) { and see if arguments is empty? Commented Aug 10, 2012 at 4:54
  • can you show us an example query that you're running? Commented Aug 10, 2012 at 4:57
  • GET_PROPERTY is an example query. public static final String GET_PROPERTY = "Select * from app_config where Property = ? "; Commented Aug 10, 2012 at 5:02

1 Answer 1

3

You're passing null to executeQuery instead of list of arguments here:

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

So, the no value in the place of ? in the query "Select * from app_config where Property = ?", hence the exception.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

@Senthil Thanks.this helped and there were few other changes.:) Its clear now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.