File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Simple JDBC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Simple JDBC question" Watch "Simple JDBC question" New topic

Simple JDBC question

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273

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

import java.sql.*;

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

String firstname="Raja bhaiya";
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+")");




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(
at sun.jdbc.odbc.JdbcOdbc.standardError(
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(
at sun.jdbc.odbc.JdbcOdbcStatement.execute(
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(
at OdbcProblem.main(
Marilyn de Queiroz

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

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

"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: 3749

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

[OCA 8 Book] [Blog]
Consider Paul's rocket mass heater.
subject: Simple JDBC question
It's not a secret anymore!