语法错误插入到语句java中。

时间:2021-10-06 22:44:41

I want to add a dataset into my MS Acces Database. But I always get a Syntax Error in the SQL Statements exactly at the executeUpdate Statement. If someone could help me, that would be great.

我想将一个数据集添加到我的MS Acces数据库中。但是在SQL语句中,我总是会在executeUpdate语句中得到语法错误。如果有人能帮助我,那就太好了。

Here you can see the Error:

在这里你可以看到错误:

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)

And this is the code which the Error is based on. It is in the executeUpdate() - Statement :

这是错误基于的代码。在executeUpdate() -语句中:

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();
    }

I can't find the Syntax Error! Can someone help me?

我找不到语法错误!有人能帮助我吗?

thank you

谢谢你!

2 个解决方案

#1


2  

The E-Mail column has a hyphen in it, you should escape it. See this answer for how to do that : Dash in a field name in access database table

电子邮件列中有一个连字符,您应该避免它。如何做到这一点,请参见这个答案:在access数据库表中使用字段名称中的“破折号”。

#2


0  

You need to add the values of all the columns in your table, else java JDBC will throw and error.

您需要在表中添加所有列的值,否则java JDBC将抛出和错误。

If you have a table users with 2 columns username and password your code will look as follow for your prepareStatement.

如果您有一个具有2列用户名和密码的表用户,那么您的代码将会按照您的准备程序进行跟踪。

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

preparestatement(“插入到用户值(‘YourName’,‘Password’)”);

#1


2  

The E-Mail column has a hyphen in it, you should escape it. See this answer for how to do that : Dash in a field name in access database table

电子邮件列中有一个连字符,您应该避免它。如何做到这一点,请参见这个答案:在access数据库表中使用字段名称中的“破折号”。

#2


0  

You need to add the values of all the columns in your table, else java JDBC will throw and error.

您需要在表中添加所有列的值,否则java JDBC将抛出和错误。

If you have a table users with 2 columns username and password your code will look as follow for your prepareStatement.

如果您有一个具有2列用户名和密码的表用户,那么您的代码将会按照您的准备程序进行跟踪。

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

preparestatement(“插入到用户值(‘YourName’,‘Password’)”);