This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Simple JDBC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Simple JDBC question" Watch "Simple JDBC question" New topic
Author

Simple JDBC question

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
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("jdbcdbc: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)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
stmt.executeUpdate("INSERT INTO personal_info(firstname) VALUES(" + firstname + ")")

I think you need some apostrophe's surrounding the value you are trying to insert.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

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')


My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple JDBC question
 
Similar Threads
Newbie problem. JDBC Error
Simple mdb connection Error
Problems in inserting data to MS Access Database
Error
Data source name not found and no default driver specified