gpt4 book ai didi

java - 如何执行更新

转载 作者:行者123 更新时间:2023-11-30 00:00:16 25 4
gpt4 key购买 nike

有人可以帮助我使用我的小型 Java/MySQL 数据库工具吗?部分代码如下,但是当单击 createCustomerButton 时,DBMS 告诉我,我的语法有些错误。

protected static JTextField nachnameField = new JTextField();
protected static JTextField vornameField = new JTextField();
protected static JTextField postleitzahlField = new JTextField();
protected static JTextField strasseField = new JTextField();
protected static JTextField emailField = new JTextField();
protected static JTextField telefonField = new JTextField();
protected static JTextField zusatzinfoField = new JTextField();
static JButton createCustomerButton = new JButton("Kunde anlegen");

...

public static void passStatement(String statementString)
throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement preparedStmt = conn.prepareStatement(statementString);
preparedStmt.executeUpdate();
}

...

public class Listener extends Main implements ActionListener, MouseListener {
public void actionPerformed(ActionEvent ae) {

@Override
public void mouseClicked(MouseEvent me) {
if (me.getSource() == createCustomerButton) {
nachnameField.getText();
vornameField.getText();
strasseField.getText();
postleitzahlField.getText();
telefonField.getText();
emailField.getText();
zusatzinfoField.getText();
String statement = "INSERT INTO kunde (nachname, vorname, strasse,
postleitzahl, telefon, email, zusatzinfo) VALUES("+nachnameField+","
+vornameField+","+strasseField+","+postleitzahlField+","+telefonField+","
+emailField+","+zusatzinfoField+")";

try {
passStatement(statement);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
createLowerPanel();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

完整错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version for the right
syntax to use near'
[,322,4,461x19,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,
alignment' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance
(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1062)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
at Main.passStatement(Main.java:66)
at Listener.mouseClicked(Listener.java:46)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
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:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
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:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
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)

非常感谢任何帮助!!

最佳答案

如果您要输入到 SQL 数据库的字符串值,则需要在每个元素前后添加单引号,例如...

tring statement = "INSERT INTO kunde (nachname, vorname, strasse, 
postleitzahl, telefon, email, zusatzinfo) VALUES('" + nachname + "','" // use variable here
+ vorname + "')"; // ...... etc...

希望这是清楚的。

关于java - 如何执行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com