wood burning stoves 2.0*
The moose likes JDBC and the fly likes Inserting data into MsAccess Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting data into MsAccess" Watch "Inserting data into MsAccess" New topic
Author

Inserting data into MsAccess

smitha rai
Greenhorn

Joined: Jun 29, 2001
Posts: 18
Hi,
I am having problem with inserting a row into the MsAccess Database. I am getting error like
"SQLException: Occurred while performing Database Operations ---> method Main() java.sql.SQLException
: [Microsoft][ODBC Microsoft Access 97 Driver] Syntax error in INSERT INTO statement.[Microsoft][ODB
C Microsoft Access 97 Driver] Syntax error in INSERT INTO statement".
My code is as under:
please help me out.
Smitha
==========================================
package dao;
import utils.GeneralFailureException;
import dbConnection.dbConnection;
import java.util.*;
import java.sql.*;
public class GeneralExpenses {
dbConnection dbCon = new dbConnection();
public GeneralExpenses() {
super();
}

public static java.sql.Timestamp getTstamp() {
java.util.Date date = (Calendar.getInstance()).getTime();
java.sql.Timestamp tStamp = new java.sql.Timestamp(date.getTime());
System.out.println("The Time Stamp : " + tStamp);
return tStamp;
}
public void insertExpenseTable(String sExphead, double dAmount, String sMode)
throws GeneralFailureException, SQLException, ClassNotFoundException {
String inssql = "INSERT INTO EXPENSES(tnum, date , " + sExphead + ", " + sMode + ") VALUES(?,?,?,?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dbCon.getConnection();
pstmt = conn.prepareStatement(inssql);

pstmt.setInt(1, 100);
pstmt.setTimestamp(2, getTstamp());
pstmt.setDouble(3, dAmount);
pstmt.setString(4, sMode);

pstmt.executeUpdate();
} finally {
dbCon.closeAll(pstmt, conn);
}
}
public static void main(String[] args) {
try {
GeneralExpenses ge = new GeneralExpenses();
ge.insertExpenseTable("xxxxxxx", 250.35, "yyyyyyyy");
System.out.println("worked");
} catch(ClassNotFoundException sqlex) {
System.err.print("SQLException: Occurred while performing Database Operations ---> method Main() " + sqlex);
System.err.println(sqlex.getMessage());
}
catch(SQLException sqlex) {
System.err.print("SQLException: Occurred while performing Database Operations ---> method Main() " + sqlex);
System.err.println(sqlex.getMessage());
} catch(GeneralFailureException e) {
System.err.print("GeneralFailureException: Occurred while Establishing Connection ---> method getConnection() " + e);
System.err.println(e.getMessage());
} catch(Exception e) {
System.err.print("GeneralFailureException: Occurred while Establishing Connection ---> method getConnection() " + e);
System.err.println(e.getMessage());
}
}
}
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73
The problem is with your timestamp field. In order to insert the current date/time into the database use 'now'. Let me show an example. For a table 'test' with a field 't' as date/time type, the query is
Insert into test ( t ) values ( now )

This will insert the current date/time into database.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inserting data into MsAccess