insert into命令中的语法错误。输入日期值时可能出错

时间:2015-06-24 07:41:15

标签: java ms-access project

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
        conn = Connect.ConnectDB();
        Object selected1 = jComboBox1.getSelectedItem();
        Object selected2 = jComboBox1.getSelectedItem();
        selected1.toString();
        selected2.toString();
        String sql= "insert into Table1 ("
        +"ID,"
        +"Bill_No,"
        +"[Vendor_Name],"
        +"[Expense],"
        +"Amount,"
        +"TDS%,"

        +"Status,"
        +"Cheque_Stages,"
        +"Cno1,"+"Cno2,"+"Cno3,"+"Cno4,"
        +"[Cdate1],"+"[Cdate2],"+"[Cdate3],"+"[Cdate4],"
        +"[CAmount1],"+"[CAmount2],"+"[CAmount3],"+"[CAmount4],"+"[Purpose],"
        +"TDSamount,"+"Appamount)"
        +"values("+jTextField1.getText()+"','"+jTextField2.getText()+"','"+jTextField3.getText()
        +"','"+jTextField4.getText()
        +"','"+jTextField5.getText()+"','"+jTextField6.getText()+"','"//+"','"+selected1+"','"+selected2
        +"','"+jTextField9.getText()+"','"+jTextField10.getText()+"','"+jTextField11.getText()
        +"','"+jTextField12.getText()
        +"','"+jTextField15.getText()+"','"+jTextField16.getText()+"','"+jTextField17.getText()
        +"','"+jTextField18.getText()
        +"','"+jTextField7.getText()+"','"+jTextField19.getText()+"','"+jTextField20.getText()
        +"','"+jTextField21.getText()+"')"+jTextField8.getText()
        +"','"+jTextField13.getText()+"','"+jTextField14.getText()+"')";
        try{
            pst = conn.prepareStatement(sql);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "SAVED");
            conn.close();
            UpdateJTable();
        }
        catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e);
        }
    }      

我收到错误说插入语句中的语法错误以及 -

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
    at sun.jdbc.odbc.JdbcOdbc.SQLPrepare(JdbcOdbc.java:4837)
    at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(JdbcOdbcConnection.java:475)
    at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(JdbcOdbcConnection.java:443)
    at application.NewMDIApplication.jButton2MouseClicked(NewMDIApplication.java:1049)
    at application.NewMDIApplication.access$1900(NewMDIApplication.java:17)
    at application.NewMDIApplication$20.mouseClicked(NewMDIApplication.java:466)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6519)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:1)

只需将所有jTextFieldX.getText()替换为""并打印您将获得的字符串:

insert into Table1 (ID,Bill_No,[Vendor_Name],[Expense],Amount,TDS%,Status,Cheque_Stages,Cno1,Cno2,Cno3,Cno4,[Cdate1],[Cdate2],[Cdate3],[Cdate4],[CAmount1],[CAmount2],[CAmount3],[CAmount4],[Purpose],TDSamount,Appamount)values(','','','','','','','','','','','','','','','','','','')','','')

See here https://ideone.com/lik4SR

这里有很多错误:

  • 我不知道TDS%是否是有效的列名,或者是否只需要包含在[]
  • 23个参数与21个?提供的值(根据你对待第四个错误的方式而有所不同)
  • 在values-part
  • 的开头缺少'
  • 错误地将)放置在值
  • 的中间位置
  • 所有内容都包含在字符串中,没有任何内容代表字段名称建议的日期或数字
  • 你逃避的不是一个罪恶的输入 - > sql injection

因此你需要

  • 修复查询语法
  • 修正值数
  • 修正值类型,删除数字和日期的',为日期添加#

答案 1 :(得分:0)

对于CDateN字段,您必须提供日期值的格式化字符串表达式。我不熟悉Java,但今天生成的字符串应为#2015/06/24#,因此SQL的那些部分将如下所示:

... "sometext",#2015/06/24#,#2015/06/01#,"someothertext" ...