Question

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
Was it helpful?

Solution

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top