有人可以通过我的小型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)
非常感谢任何帮助!!
答案 0 :(得分:0)
您在JTextField上调用getText()
并忽略返回的字符串。而是将实际的JTextField的toString结果而不是其文本传递到SQL语句中。别这么做。
相反,从字段中获取文本字符串,并将它们放入字符串变量中。然后使用这些变量来构建SQL语句。即,
String foo = fooTextField.getText();
// now use foo, not fooTextField to create your SQL statements.
即,
if (me.getSource() == createCustomerButton) {
String nachname = nachnameField.getText(); // get text put into variable
String vorname = vornameField.getText();
// .... etc
String statement = "INSERT INTO kunde (nachname, vorname, strasse,
postleitzahl, telefon, email, zusatzinfo) VALUES(" + nachname + "," // use variable here
+ vorname + "," ...... etc...
另外:
答案 1 :(得分:0)
如果它是您输入SQL数据库的字符串值,那么您需要在每个元素之前和之后放置单引号,例如..
tring statement = "INSERT INTO kunde (nachname, vorname, strasse,
postleitzahl, telefon, email, zusatzinfo) VALUES('" + nachname + "','" // use variable here
+ vorname + "')"; // ...... etc...
希望这很清楚。