简体   繁体   中英

Java SQL Insert rows with auto-increment id

I'm trying to insert into a database with the following columns, however I'm getting the following 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 'Id) Values ('Gonzalo','Higuain','09 7 2018','Argentina',12.0,'Attacker','Right',' at line 1

  1. id, int auto-increment

  2. FirstName, Text

  3. LastName, Text

  4. DateOfBirth, Text

  5. Nationality, Text

  6. Height, Float

  7. Position, Text

  8. StrongFoot, Text

  9. TeamId, Int

      DatabaseConnection DatabaseConnect= new DatabaseConnection(); DatabaseConnect.getConnection(); Statement mystatement = DatabaseConnect.myConn.createStatement(); String club = ("Insert into players ( FirstName, LastName, DateOfBirth, Nationality, Height, Position, StrongFoot, Team Id) Values (?,?,?,?,?,?,?,?)"); PreparedStatement input = DatabaseConnect.myConn.prepareStatement(club); input.setString(1, fName); input.setString(2, lName); input.setString(3, formattedDate); input.setString(4, nationality); input.setDouble(5, height); input.setString(6, position); input.setString(7, foot); input.setInt(8, teamId); input.executeUpdate(); 
String club = ("Insert into players ( FirstName, LastName, DateOfBirth, Nationality, Height, Position, StrongFoot, Team Id) Values (?,?,?,?,?,?,?,?)");

I think Team Id above should be TeamId . You have a space in there. Here's what it should be:

String club = ("Insert into players ( FirstName, LastName, DateOfBirth, Nationality, Height, Position, StrongFoot, TeamId) Values (?,?,?,?,?,?,?,?)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM