Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Inserting data into MsAccess

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
}
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dinner will be steamed monkey heads with a side of tiny ads.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic