gpt4 book ai didi

java - 通过 Java GUI 将数据插入表

转载 作者:行者123 更新时间:2023-11-29 07:39:01 25 4
gpt4 key购买 nike

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

感谢您的帮助!!!

最佳答案

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

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

或者

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

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

关于java - 通过 Java GUI 将数据插入表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29629626/

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