使用java和SQL查询INSERT INTO将新记录插入MS访问

时间:2013-01-10 10:08:11

标签: java sql ms-access

我想添加一条新记录来访问数据库,但是SQL查询没有应用它,这里是我的代码,我在其中添加数据!!

    public void actionPerformed(ActionEvent e) {
    JButton b=(JButton)e.getSource();
    if(b==c.t.addR){
        String aa=Integer.toString(55);
        String t=c.t.titlefield.getText();
        String d=c.t.datefield.getText();
        String p=c.t.progressfield.getText();
        String pr=c.t.priorityfield.getText();
        String s="Open";
        System.out.println(t+"\t"+d+"\t"+p+"\t"+pr+"\t"+s); //error is here
        try {
            c.b.st.executeQuery("INSERT into Records (TaskID,Title,Deadline,Progress,Priority,Status) VALUES("+aa+"','"+t+"','"+d+"','"+p+"','"+pr+"','"+s+")");
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

这是我得到的错误

  

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]   查询表达式中的语法错误(缺少运算符)   '55', 'A', '1', '1', '高', '打开'。在   sun.jdbc.odbc.JdbcOdbc.createSQLException(未知来源)at at   sun.jdbc.odbc.JdbcOdbc.standardError(未知来源)at   sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(未知来源)at at   sun.jdbc.odbc.JdbcOdbcStatement.execute(未知来源)at   sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(未知来源)at   Listener.actionPerformed(Listener.java:27)at   javax.swing.AbstractButton.fireActionPerformed(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自java.awt.AWTEventMulticaster.mouseReleased(未知来源)     在java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 400(未知来源)at   java.awt.EventQueue $ 2.run(未知来源)at   java.awt.EventQueue $ 2.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.AccessControlContext $ 1.doIntersectionPrivilege(未知   来源)at   java.security.AccessControlContext $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.AccessControlContext $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

错误必须发生在这一行:

c.b.st.executeQuery("INSERT into Records (TaskID,Title,Deadline,Progress,Priority,Status) VALUES("+aa+"','"+t+"','"+d+"','"+p+"','"+pr+"','"+s+")");

看着它,那是因为你没有围绕参数的开放和结束报价?尝试:

c.b.st.executeQuery("INSERT into Records (TaskID,Title,Deadline,Progress,Priority,Status) VALUES('"+aa+"','"+t+"','"+d+"','"+p+"','"+pr+"','"+s+"')");