con = Connect.ConnectDB();
        String sql = "insert into table1 ("
        +"StudentID,"
        +"FirstName,"
        +"LastName,"
        +"Contact Number,"
        +"E-mail address,"
        +"CarNumber)"
        + "values("+txtid.getText()+ ",'"+txtname.getText()+"','"+txtlast.getText()+"','"+txtnumber.getText()+"','"+txtemail.getText()+"','"+txtcarno.getText()+"')" ;
        try{
            pst = con.prepareStatement(sql);
            pst.execute();
            JOptionPane.showMessageDialog(null, "Saved");


        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

can any one help me with this? im getting syntax error in insert into statement ..
Tq

Recommended Answers

All 6 Replies

Use an output statement to display the resulting sql string to the console, and then post that here so we can see what you are actually trying to store. In any case, remember that string values in SQL have to be delimited by single quotes, which you didn't do for the start of the student id value, though it was ended with one. If the id is numeric, remove the terminating single quote. IE, you have one of two errors that a cursory view shows me - one is that if the student id is a string, it isn't properly single-quote delimited, and if it is a numeric value, then it is improperly single-quote delimited at the end of the value... :-)

sorry newbie here.. ><
im using Jframes in netbeans. the output is 0. im trying to key in dummy record and see whether will appear in my access or not, and it just shows syntax error in insert into statement. fail to insert.

i really dont get what you mean by " student id value"..

don't you think this is an extra double quotation ?

insert into table1 (" <<<<

You have "-" in "E-mail address" . Remove it from the SQLtable column name as well as from the SQL command in java code. Also spaces like "Contact<space>Number" in a column name gives many troubles too.

Change them and tell us how it went.

You can always try any SQLcommand in java in MySQL by driectly executing it at mysql (of course after modifying the command from java format to sql format).

the " is actually looks like this >"+"StudentID,

still the same.. even i tried the update also the same.. syntax error UPDATE statement..

i dont have mysql,using MS access

See the JDBC tutorials for PreparedStatement. Cobbling together a statement like this is just BEGGING for problems. Not only for syntax problems like you have, which can even come if the code is right, because what happens if the text for a field value contains a single quote (')? But it also opens you up for SQL injection attacks. E.G. What happens if the text for the last field value is bogus'); delete from table1;?

If I were you I would try to take the command, change it and try to run it in the MS acess itself to see if the syntax is correct and the again reformat it to meet the java standard. Yes, try to make it only about 2 lines otherwise it is very hard to read it. Also don't forget the spaces of the column names. Try to work without spaes and "-" s.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.