• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple JDBC question

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I got this simple JDBC program and gives out the following error.

Program:
import java.sql.*;

public class OdbcProblem {
public static void main(String args[])
throws Exception {

String firstname="Raja bhaiya";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:personal_ds",
"", "");
Statement stmt = con.createStatement();
// ResultSet rs=stmt.executeQuery("select * from personal_info");
System.out.println("firstname is..."+firstname);
stmt.executeUpdate("INSERT INTO personal_info(firstname) VALUES("+firstname+")");

stmt.close();
con.close();

}
}

Error:

firstname is...Raja bhaiya
Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operat
or) in query expression 'Raja bhaiya'.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
at OdbcProblem.main(OdbcProblem.java:14)
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
stmt.executeUpdate("INSERT INTO personal_info(firstname) VALUES(" + firstname + ")")

I think you need some apostrophe's surrounding the value you are trying to insert.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to surround the value with single quotes.

So SQL saw this:

INSERT INTO personal_info(firstname) VALUES(Raja bhaiya)

when it would have preferred this:

INSERT INTO personal_info(firstname) VALUES('Raja bhaiya')
 
reply
    Bookmark Topic Watch Topic
  • New Topic