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

时间:2022-03-02 15:00:57

I want to add a new record to access database,, but the SQL query is not applying it,, here is my Code in which i am adding data!!

我想添加一个新的记录来访问数据库,但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();
        }
    }

Here is the error i am getting

这是我得到的错误

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '55','a','1','1','high','Open'. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source) at Listener.actionPerformed(Listener.java:27) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]查询表达式“55”,“a”,“1”,“1”,“高”,“打开”中的语法错误(缺少运算符)。在sun.jdbc.odbc.JdbcOdbc.createSQLException(未知来源)在sun.jdbc.odbc.JdbcOdbc.standardError(未知来源)在sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(未知来源)在sun.jdbc.odbc.JdbcOdbcStatement位于javax.swing.AbstractButton $的javax.swing.AbstractButton.fireActionPerformed(未知来源)的Listener.actionPerformed(Listener.java:27)sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(未知来源)的.execute(未知来源) Handler.actionPerformed(来源不明)在javax.swing.DefaultButtonModel.fireActionPerformed(来源不明)在javax.swing.DefaultButtonModel.setPressed(来源不明)在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(来源不明)在爪哇。 awt.AWTEventMulticaster.mouse在java.awt上的java.awt.Component.processEvent(未知来源)的javax.swing.JComponent.processMouseEvent(未知来源)的java.awt.Component.processMouseEvent(未知来源)中发布了(未知来源)。 java.awt.Component.dispatchEvent中的Container.processEvent(未知来源) IMPL(来源不明)在java.awt.Container.dispatchEventImpl(来源不明)在java.awt.Component.dispatchEvent(来源不明)在java.awt.LightweightDispatcher.retargetMouseEvent(来源不明)在java.awt.LightweightDispatcher.processMouseEvent( java.awt.Component上的java.awt.LightweightDispatcher.dispatchEvent(未知来源)java.awt.Window.dispatchEventImpl(未知来源)java.awt.Component.dispatchEvent(未知来源)java.awt.Component.dispatchEvent(未知来源)在java.awt.EventQueue.access的java.awt.EventQueue.dispatchEventImpl(未知来源)java.awt.EventQueue $ 2.run(未知来源)java.awt.EventQueue $ 2.run(未知)源)在java.security.AccessController.doPrivileged(本机方法)在java.security.AccessControlContext $ 1.doIntersectionPrivilege(来源不明)在java.security.AccessControlContext $ 1.doIntersectionPrivilege(来源不明)在java.awt.EventQueue中$ 3.run(来自java.awt.EventQueue的未知来源$ 3.run(未知来源)在java.security.AccessController.doPrivileged(本机方法)在java.security.AccessControlContext $ 1.doIntersectionPrivilege(来源不明)在java.awt.EventQueue.dispatchEvent(来源不明)在java.awt.EventDispatchThread.pumpOneEventForFilters(来源不明)在java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)在java.awt.EventDispatchThread.pumpEvents(未知来源)在java.awt.EventDispatchThread.pumpEvents(未知来源)在java的。 awt.EventDispatchThread.run(未知来源)

1 个解决方案

#1


1  

The error has to be happening on this line:

错误必须在这一行上发生:

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

And looking at it, it will be because you don't have an openning and closing quote around your parameters? Try:

看一下,这是因为你的参数周围没有开放和结束的引用?尝试:

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

#1


1  

The error has to be happening on this line:

错误必须在这一行上发生:

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

And looking at it, it will be because you don't have an openning and closing quote around your parameters? Try:

看一下,这是因为你的参数周围没有开放和结束的引用?尝试:

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