通过Java GUI将数据插入表中

时间:2015-04-14 14:10:57

标签: java mysql user-interface

当我尝试向表中插入新数据时,我收到了一堆错误(为每个字段键入数据,然后单击“提交”)。它应该在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);

谢谢你的帮助!!!

1 个答案:

答案 0 :(得分:2)

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

INSERT INTO <table_name> VALUES (<value1>, <value2>, ....);

OR

INSERT INTO <table_name> (<column_1>, <column_2>, ...) VALUES (<value1>, <value2>, ....);

此外,您应该尝试使用PreparedStatement而不是Statement来动态设置语句的值。并详细介绍JDBC。