i’m new here and i have a question.
I am trying to insert a row into my MySQL database via Java.
for the java part i use Netbeans.
when i run it i get the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ‘VALUES01234567PietdeBakker0113605040java.util.GregorianCalendar[time=?,areFields’ at line 1
the code i came up with is the following from DBObject.java:
public void insertDeelnemer(Deelnemer d){
String sql = "INSERT INTO deelnemer(OVnummer,Voornaam,Tussenvoegsel,Achternaam,Telefoonnummer,Geboortedatum) VALUES" + d.getOvnummer() + d.getVoornaam() + d.getTussenvoegsel() + d.getAchternaam() + d.getTelefoonnummer() + d.getGeboortedatum();
try{
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(Exception e){
e.printStackTrace();
}
which goes with this from DeelnemerToevoegen.java:
String OVnummer = OVnummerVak1.getText();
String Voornaam = VoornaamVak1.getText();
String Achternaam = AchternaamVak1.getText();
String Tussenvoegsel = TussenvoegselVak1.getText();
int dag = DagComboBox1.getSelectedIndex()+1;
int maand = MaandComboBox1.getSelectedIndex();
int jaar = JaarComboBox1.getSelectedIndex()+1980;
GregorianCalendar Geboortedatum = new GregorianCalendar(jaar, maand, dag);
String Telefoonnummer = TelefoonnummerVak.getText();
Deelnemer d = new Deelnemer(OVnummer, Voornaam, Achternaam, Tussenvoegsel, Geboortedatum, Telefoonnummer);
DBObject dbo = new DBObject();
dbo.insertDeelnemer(d);
You’re missing a space between
VALUESand". This should beVALUES " +. Also, all values need to be comma-separated and strings need to be quoted using a single quote (‘). That’s what you get for not using parameterized queries…EDIT
Additional tip: Look at Adrian’s SQL statement. Unless the content of your
sqlvariable looks like that if you print it to the console, you’re not providing valid SQL.