簡體   English   中英

語法錯誤插入語句java

[英]Syntax Error Insert Into statement java

我想將數據集添加到MS Acces數據庫中。 但是我總是在executeUpdate語句中的SQL語句中得到語法錯誤。 如果有人可以幫助我,那就太好了。

在這里你可以看到錯誤:

java.sql.SQLException: [Microsoft][ODBC-Treiber für Microsoft Access] Syntaxfehler in der INSERT INTO-Anweisung.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
at verwaltung.ListenerRegistrieren.addBenutzer(ListenerRegistrieren.java:47)
at verwaltung.ListenerRegistrieren.actionPerformed(ListenerRegistrieren.java:28)
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.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$200(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.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)

這是Error所基於的代碼。 它位於executeUpdate() - Statement中:

try {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:MSAccess");
    PreparedStatement pstm;
    pstm = conn.prepareStatement("Insert Into Benutzer (Benutzername, Vorname, Nachname, E-Mail) values (?,?,?,?)");

    // SQL Statements
    pstm.setString(1, gui.benutzername.getText());
    pstm.setString(2, gui.vorname.getText());
    pstm.setString(3, gui.nachname.getText());
    pstm.setString(4, gui.email.getText());
    pstm.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    }

我找不到語法錯誤! 有人能幫我嗎?

謝謝

電子郵件列中有一個連字符,您應該將其轉義。 有關如何執行此操作,請參閱此答案: 在訪問數據庫表中的字段名稱中進行Dash

您需要添加表中所有列的值,否則Java JDBC將拋出並出錯。

如果您有一個包含2列用戶名和密碼的表用戶,則您的代碼將如下所示為prepareStatement。

pstm = conn.prepareStatement("Insert Into users values ('YourName', 'Password')");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM