通过JavaGUI将数据插入表

通过JavaGUI将数据插入表,java,mysql,user-interface,Java,Mysql,User Interface,当我尝试向表中插入新数据时(为每个字段键入数据,然后单击submit),会出现一系列错误。它应该在TextArea中输出SQL插入命令(与Java使用的命令完全相同)。我如何解决这个问题?我的代码中遗漏了什么吗?这是我的密码: import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; //This GUI interface is a place holder to be fini

当我尝试向表中插入新数据时(为每个字段键入数据,然后单击submit),会出现一系列错误。它应该在TextArea中输出SQL插入命令(与Java使用的命令完全相同)。我如何解决这个问题?我的代码中遗漏了什么吗?这是我的密码:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

//This GUI interface is a place holder to be finished by students. 

public class InsertPanel extends JPanel{
    private Connection connection = null;
    private Statement statement = null;
    public JTextField isbnTextFld;
    public JTextField authorTextFld;
    public JTextField titleTextFld;
    public JTextField priceTextFld;
    public JTextArea textArea;
    public JButton submitBtn;

    public InsertPanel() {
        setBackground(Color.yellow);
        setPreferredSize(new Dimension(540, 500));

        JPanel p = new JPanel();
        p.setLayout(new GridLayout(5, 3));

        JLabel isbnLabel = new JLabel("ISBN: ");
        isbnTextFld = new JTextField(10);

        JLabel authorLabel = new JLabel("Author: ");
        authorTextFld = new JTextField(15);  

        JLabel titleLabel = new JLabel("Title: ");
        titleTextFld = new JTextField(20);

        JLabel priceLabel = new JLabel("Price: ");
        priceTextFld = new JTextField(10);

        submitBtn = new JButton("Submit");
        ButtonListener buttonListener = new ButtonListener();
        submitBtn.addActionListener(buttonListener);

        textArea = new JTextArea(10, 30);

        p.add(isbnLabel);
        p.add(isbnTextFld);
        p.add(authorLabel);
        p.add(authorTextFld);
        p.add(titleLabel);
        p.add(titleTextFld);
        p.add(priceLabel);
        p.add(priceTextFld);
        p.add(submitBtn);
        p.add(textArea);
        add(p, BorderLayout.NORTH);
        add(textArea, BorderLayout.CENTER);
    }

    public class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            try {
                connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost/books", "root", "admin");
                statement = connection.createStatement();
                String isbn = isbnTextFld.getText();
                String title = titleTextFld.getText();
                String author = authorTextFld.getText();
                String price = priceTextFld.getText();
                String sql = "Insert('" + (isbn) + "' + '" + (title) + "' +  '" + (author) + "' + '" + (price) 
                    + "')";
                statement.executeUpdate(sql);
            } catch (SQLException sqlException){
                    sqlException.printStackTrace();
            }
        }
    }
}

create table books
(  isbn char(13) not null primary key,
   author char(30),
   title char(60),
   price float(4,2)
);

insert into books values
  ("0-672-31697-8", "Michael Morgan", "Java 2 for Professional Developers", 34.99),
  ("0-672-31745-1", "Thomas Down", "Installing Debian GNU/Linux", 24.99),
  ("0-672-31509-2", "Pruitt, et al.", "Teach Yourself GIMP in 24 Hours", 24.99),
  ("0-672-31769-9", "Thomas Schenk", "Caldera OpenLinux System Administration Unleashed", 49.99);

谢谢你的hepl

我的第一个建议是,在进一步尝试之前,您应该了解更多SQL语法。我这样说是因为有效的SQL insert语句应该如下所示:

INSERT INTO <table_name> VALUES (<value1>, <value2>, ....);
插入值(,…);

插入到(,…)值(,…);

您还应该尝试使用
PreparedStatement
而不是
Statement
来动态设置语句的值。并详细检查JDBC。

这里是错误:com.mysql.JDBC.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在第1行使用“(“1-11 1-33344-4”+”软件工程解释了“+”Dannia Thomas“+”55.56”,您可以提供插入数据的原始sql吗?@Blip我刚刚在上面的问题
Insert(“+(isbn)+“+”+”+“+”+”(标题)+”中对其进行了更新“+”+(作者)+“+”+”+(价格)+“)
不是有效的SQL insert语句。
INSERT INTO <table_name> (<column_1>, <column_2>, ...) VALUES (<value1>, <value2>, ....);