Having a syntax error in INSERT statement on the date and time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carecare86
    New Member
    • Nov 2009
    • 1

    Having a syntax error in INSERT statement on the date and time

    These are the codes. I cant see any error there. Please kindly help. Thanks.

    void saveMedicalReco rds() {
    try {
    Class.forName(d river);
    Connection connection = DriverManager.g etConnection(ur l2, username, password);
    Statement statement = connection.crea teStatement();
    sql = "INSERT INTO medicalrecords (NRIC, Time, Diagnosis, MC, NoOfDays, Prescription1, Qty1, Prescription2, Qty2, Prescription3, Qty3, Prescription4, Qty4, Prescription5, Qty5, Additional) VALUES ('"+txtPID3.get Text()+"',"+tim eNow+",'"+txtDi agnosis.getText ()+"',"+MC_Stri ng+",'"+txtDays .getText()+"',' "+txtDrug1.getT ext()+"',"+txtQ 1.getText()+",' "+txtDrug2.getT ext()+"',"+txtQ 2.getText()+",' "+txtDrug3.getT ext()+"',"+txtQ 3.getText()+",' "+txtDrug4.getT ext()+"',"+txtQ 4.getText()+",' "+txtDrug5.getT ext()+"',"+txtQ 5.getText()+",' "+txtAdd.getTex t()+"')";
    System.out.prin tln("sql is : "+sql);
    statement.execu teUpdate(sql);
    connection.clos e();
    }
    catch (Exception ex){
    ex.printStackTr ace();
    }
    saySavedMedical Records();
    }
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Can you tell us a little more? Where are the errors... Please post a bit of it and a brief explaination. I suspect you are not formatting the date variable upon inserting. What is the date format in the db.

    I would also use preparedStateme nt to fend off having to format dates going in. Also preparedStateme nt is of great use against SQL injection.

    http://www.java2s.com/Code/Java/Database-SQL-JDBC/DemoPreparedSta tementSetBytes. htm


    In a bit!

    Comment

    Working...