Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:01:57+00:00 2026-06-15T14:01:57+00:00

Most of my code seems to work, but I keep on getting Exception in

  • 0

Most of my code seems to work, but I keep on getting Exception in thread “main” java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). It happens after the finally block in readDatabase(). It doesn’t get to the print statement System.out.println(“DOESN’T GET HERE”);
I don’t know why. Here is the class where everything is processed. In the main class, it just makes an object of this one and calls readDatabase();

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//static because when creating an object of it in main, you won't have to make an object of the outer class (SQLProject) first
public class MySQLAccess{
    private Connection connect = null;
    private Statement statement = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    public void readDatabase() throws Exception
    {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");

            statement = connect.createStatement();
            System.out.println("here1");
            resultSet = statement.executeQuery("select * from test.comments");
            writeResultSet(resultSet);

            preparedStatement = connect.prepareStatement("INSERT INTO test.comments values(default, ?, ?, ?, ?, ?, ?)");
            //columsn in test.comments
            // myuser, email, webpage, datum, summary, COMMENTS
            preparedStatement.setString(1, "Test");
            preparedStatement.setString(2, "TestEmail");
            preparedStatement.setString(3, "TestWebpage");
            preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
            preparedStatement.setString(5, "Test Summary");
            preparedStatement.setString(6, "Test Comment");
            System.out.println("here2");
            preparedStatement.executeUpdate();

            preparedStatement = connect.prepareStatement("SELECT myuser, webpage, datum, summary, comments FROM test.comments");
            System.out.println("here3");
            resultSet = preparedStatement.executeQuery();
            writeResultSet(resultSet);

            preparedStatement = connect.prepareStatement("DELETE FROM test.comments WHERE myuser='?';");
            preparedStatement.setString(1, "Test");
            preparedStatement.executeUpdate();

            resultSet = statement.executeQuery("SELECT * FROM test.comments;");
            System.out.println("Writing meta data");
            writeMetaData(resultSet);
        }
        catch (Exception e){
            throw e;
        }
        finally{ 
            close();
            System.out.println("ALMOST");
            }
        System.out.println("DOESN'T GET HERE");
    }

    private void writeMetaData(ResultSet resultSet) throws SQLException
    {
        System.out.println("The columns in the table are: ");
        System.out.println("Table: " + resultSet.getMetaData().getTableName(1));

        for(int i=1;i<=resultSet.getMetaData().getColumnCount(); i++)
        {
            System.out.println("Column " + i + " " + resultSet.getMetaData().getColumnName(i));
        }
    }

    private void writeResultSet(ResultSet resultSet) throws SQLException
    {
        while(resultSet.next())
        {
            String user = resultSet.getString("myuser");
            String website = resultSet.getString("webpage");
            String summary = resultSet.getString("summary");
            Date date = resultSet.getDate("datum");
            String comment = resultSet.getString("comments");

            System.out.println("User: " + user);
            System.out.println("website: " + website);
            System.out.println("summary: " + summary);
            System.out.println("date: " + date);
            System.out.println("comment: " + comment);
        }
    }

    private void close()
    {
        try{
            if(resultSet != null)
                resultSet.close();
            if(statement != null)
                statement.close();
            if(connect != null)
                connect.close();

        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("hello");
            System.out.println(e);
        }
    }
}//private inner class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:01:59+00:00Added an answer on June 15, 2026 at 2:01 pm
    preparedStatement = 
        connect.prepareStatement("DELETE FROM test.comments WHERE myuser='?';");
    preparedStatement.setString(1, "Test");
    

    This is the problematic statement. The question mark is enclosed in quotes and so the statement parser is not able to find it out and so the next statement is throwing the error.

    Though the parameter type is String, the corresponding placeholder shouldn’t be included in quotes. The prepared statement processor will take care of generating the appropriate SQL based on the data type of parameters. So, it is always a plain ? that should be used as placeholder for parameters of any data type.

    So, those two statements should simply be as follows:

    preparedStatement = 
        connect.prepareStatement("DELETE FROM test.comments WHERE myuser=?");
    preparedStatement.setString(1, "Test");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an OpenCV Android app. Most of its code is in Java but
I am trying to use SpinLock, but even this most basic code in a
I'm writing a small noticeboard app and most of it seems to work so
We have a setup where most code, before being promoted to full production, is
Most JavaScript code is also syntactically valid ActionScript 3.0 code. However, there are some
I use astyle to format my code most of the time, and I love
Most of the code I've written in .NET to make REST calls have been
I've worked out most of the code and have several game classes. The one
I've done most of my code in as3, working from either document class or
I have an application with most of the code written in javascript. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.