0

I am trying to update a access database via odbc driver through java.

Table name is form1

I am getting a syntax error when I execute this command:

updates="INSERT INTO form1 
(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,
bloodgroup,slno,college,department,liveprojects,trainings); 

values("+abc+",'"+t3.getText()+"','"+t4.getText()+"',"+def+","+def1+","+zzz+",'"
      +t8.getText()+"','"+t9.getText()+"','"+t10.getText()+"',"+aaaa+",'"
      +t12.getText()+"',"+xyz+",'"+t13.getText()+"','"+dd+"','sa','da')";

Thank you.

2
  • 3
    What's that ; doing after the column list? (And please do read about sql injection and bind parameters.) Commented Aug 4, 2012 at 10:42
  • You shouldn't be using ODBC, use a real JDBC driver instead. Commented Aug 4, 2012 at 12:10

6 Answers 6

1

There are two problems with your statement - a real and a potential one:

  • Real: the semicolon before the values keyword needs to be removed
  • Potential: the statement needs to be converted for use with parameters, otherwise a single quote in the body of any of the string parameters will cause a syntax error.

Here is how you can switch to parameterized prepared statements:

String updates="INSERT INTO form1 (entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,bloodgroup,slno,college,department,liveprojects,trainings) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement psUpd = con.prepareStatement(updates);
psUpd.setInt(1, abc);
psUpd.setString(2, t3.getText());
psUpd.setString(3, t4.getText());
psUpd.setInt(4, def);
psUpd.setInt(5, def1); // The types of parameters need to match the type of setXYZ
... // Continue for the remaining parameters, then call
psUpd.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

1

First problem:

updates="INSERT INTO form1 
(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,
bloodgroup,slno,college,department,liveprojects,trainings);

*you are separating field and values with ; but it will be blank *

Secondly It is best to use prepareStatement

String sql ="INSERT INTO TableName  
(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,
bloodgroup,slno,college,department,liveprojects,trainings) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?); 

    PreparedStatement ps = con.prepareStatement(sql );    
     int index = 1;
    ps.setInt(index++, abc);
    ps.setString(index++, t3.getText());
    ps.setString(index++, t4.getText());
    ps.setInt(index++, def);
    ps.setInt(index++, def1); // The types of parameters need to match the type of setXYZ
    ... // Continue for the remaining parameters, then call
    ps.executeUpdate();

2 Comments

when PreparedStatement used i get message Driver does not support this function....why do i get this message????i use access 2003 and Odbc Driver
I used the following code and it worked..Thanks guys for the help....cheers Statement stmt=con.createStatement(); String insert="insert into Students (slno,entrydate,names,gender,dob,phno,emailid,facebookid,address,semester,bloodgroup,college,department,liveprojects,trainings) " + "values("+serialno+","+entdate+",'"+nameofs+"','"+Gender+"',"+bod+","+phone+",'"+emaid+"','"+femaid+"','"+addr+"',"+sem+",'"+bg+"','"+coll+"','"+depto+"','java','java')";
0

Try to remove the ; before values.

Comments

0

1.

Use INSERT INTO form1 values(value1,value2......); directly.

2. If you want to stick to your own format... then do the following changes.

- Remove the ; before the values.

- To insert the comma, its better to use "," instead of ','

Comments

0

The semicolon (;) after the column names is the culprit. You are terminating the statement by doing that. just remove the ; . your code will work.

1 Comment

Also make use of prepare statements. Its not good to inject directly the values in the string.
0

try this.....

updates="INSERT INTO form1(entrydate,name,gender,month,date,phno,emailid,facebookid,address,semester,bloodgroup,slno,college,department,liveprojects,trainings) 
values( '"+abc+"','"+t3.getText()+"','"+t4.getText()+"','"+def+"','"+def1+"','"+zzz+"','" +t8.getText()+"','"+t9.getText()+"','"+t10.getText()+"','"+aaaa+"','"
  +t12.getText()+"','"+xyz+"','"+t13.getText()+"','"+dd+"','sa','da')";

Comments

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.