2

My error occurs at the preparedStatement.executeUpdate(); statement and throws the following exception:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
  Position: 65
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:120)
    at Main.main(Main.java:28)

Here's my code:

import java.sql.*;

import Helper.DBHandler;

public class Main {

    private static Connection connection;
    private static PreparedStatement preparedStatement;

    public static void main(String[] args) throws ClassNotFoundException, SQLException {


        connection  = new DBHandler().getDbConnection();

        String sqlInsert = "INSERT INTO users(firstname,lastname,username,address,age)"
                         + "VALUES ?,?,?,?,?";


        preparedStatement = connection.prepareStatement(sqlInsert);

        System.out.println(connection.getNetworkTimeout());

        preparedStatement.setString(1,"John");
        preparedStatement.setString(2,"Doe");
        preparedStatement.setString(3,"jondo");
        preparedStatement.setString(4,"Tokyo Japan");
        preparedStatement.setInt(5,28);
        preparedStatement.executeUpdate();
    }
}

This is the DBHandler class, where config contains the variables dbhost, dbPort, etc. I verified that this part functions properly.

package Helper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBHandler extends Config{

    public Connection getDbConnection() throws ClassNotFoundException, SQLException {

        String connectionString = "jdbc:postgresql://" + dbHost + ":" + dbPort + "/" + dbName;

        Class.forName("org.postgresql.Driver");

        return  DriverManager.getConnection(connectionString, dbUser, dbPass);

    }

}

1 Answer 1

4

You need to place the tuple to be inserted inside parentheses. So, use this SQL:

INSERT INTO users (firstname, lastname, username, address, age)
VALUES (?,?,?,?,?);

Your updated Java code:

String sqlInsert = "INSERT INTO users (firstname, lastname, username, address, age) ";
sqlInsert += "VALUES (?,?,?,?,?)";

Note that if you don't like parentheses, then you could use your current placeholder syntax with an INSERT INTO ... SELECT:

INSERT INTO users (firstname, lastname, username, address, age)
SELECT ?, ?, ?, ?, ?;
Sign up to request clarification or add additional context in comments.

2 Comments

@GhostCat Your comment is truly the cat's meow!
The good thing about good people is that it is easy to identify upvote worthy content on their profile. And, encouraging: to discover an almost 300k user who also has no question / answer with 100 or more upvotes. Makes me feel less alone in that regard. May the badges and upvotes be with you.

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.