user3702643
user3702643

Reputation: 1505

MySQL insert with auto increment

Im trying to insert some data from my application into my database, but I keep getting the error stated below.

I have 4 columns in my table and one is set to auto increment. I suspect that is causing the error.

Can anyone help me out with this?

Table Columns:

Id (auto increment), userID(generated from application), username, name

Code:

    String sql = "insert into details values (?, ?, ?)";

    PreparedStatement preparedStatement = connect.prepareStatement(sql);
    preparedStatement.setInt(1, userID);
    preparedStatement.setString(2, username);
    preparedStatement.setString(3, name);
    preparedStatement.executeUpdate(); 

    preparedStatement.executeUpdate(sql);

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1

Upvotes: 0

Views: 314

Answers (3)

user3702643
user3702643

Reputation: 1505

Apparently it was just a simple human error. Wrote the execute statement twice.

    String sql = "insert into details values (?, ?, ?)";

    PreparedStatement preparedStatement = connect.prepareStatement(sql);
    preparedStatement.setInt(1, userID);
    preparedStatement.setString(2, username);
    preparedStatement.setString(3, name);

    preparedStatement.executeUpdate(); <-- Changed to preparedStatement.execute();

    preparedStatement.executeUpdate(sql); <-- Removed this line

Upvotes: 0

Jens
Jens

Reputation: 69480

You have to change your sql-statement to this:

String sql = "insert into details (userID, username, name) values (?, ?, ?)";

Because you do not add all attributes of the table you have to say which arguments you add.

Upvotes: 0

cherouvim
cherouvim

Reputation: 31928

try this:

insert into details (userID, username, name) values (?, ?, ?)

Upvotes: 2

Related Questions