用java实现建立一个数据库addresslist,为数据库添加一个表格addressbook,并为表格添加记录(20条左右记录)。并完成以下编程:A、从数据库表格addressbook中读出数据,显示在下图界面中的表格中;B、通过按钮“Add”事件处理,完成数据表格addressbook中记录的添加。
时间: 2023-06-16 20:08:18 浏览: 211
以下是用 Java 实现建立一个数据库 addresslist,为数据库添加一个表格 addressbook,并为表格添加记录(20 条左右记录),并完成从数据库表格 addressbook 中读出数据,显示在下图界面中的表格中以及通过按钮“Add”事件处理,完成数据表格 addressbook 中记录的添加的代码:
```java
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class AddressList extends JFrame {
private JPanel contentPane;
private JTextField nameTextField;
private JTextField phoneTextField;
private JTable table;
private DefaultTableModel tableModel;
private Connection conn;
private Statement stmt;
private ResultSet rs;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddressList frame = new AddressList();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddressList() {
connectDatabase();
createTable();
initializeUI();
loadTableData();
}
private void connectDatabase() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/addresslist", "root", "password");
stmt = conn.createStatement();
System.out.println("Database connected!");
} catch (Exception e) {
e.printStackTrace();
}
}
private void createTable() {
try {
stmt.execute("CREATE TABLE IF NOT EXISTS addressbook (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(50), phone VARCHAR(20), PRIMARY KEY (id))");
System.out.println("Table created!");
} catch (Exception e) {
e.printStackTrace();
}
}
private void initializeUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
nameTextField = new JTextField();
nameTextField.setBounds(10, 10, 100, 25);
contentPane.add(nameTextField);
nameTextField.setColumns(10);
phoneTextField = new JTextField();
phoneTextField.setBounds(120, 10, 100, 25);
contentPane.add(phoneTextField);
phoneTextField.setColumns(10);
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addRecord();
}
});
addButton.setBounds(230, 10, 80, 25);
contentPane.add(addButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 50, 414, 200);
contentPane.add(scrollPane);
tableModel = new DefaultTableModel(
new Object[][] {},
new String[] { "ID", "Name", "Phone" }
);
table = new JTable(tableModel);
scrollPane.setViewportView(table);
}
private void loadTableData() {
try {
rs = stmt.executeQuery("SELECT * FROM addressbook");
while (rs.next()) {
Vector<String> row = new Vector<String>();
row.add(rs.getString("id"));
row.add(rs.getString("name"));
row.add(rs.getString("phone"));
tableModel.addRow(row);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void addRecord() {
try {
String name = nameTextField.getText();
String phone = phoneTextField.getText();
stmt.execute("INSERT INTO addressbook (name, phone) VALUES ('" + name + "', '" + phone + "')");
Vector<String> row = new Vector<String>();
row.add("");
row.add(name);
row.add(phone);
tableModel.addRow(row);
nameTextField.setText("");
phoneTextField.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在这个代码中,我们使用了 MySQL 数据库,所以需要先在本地安装 MySQL 数据库,并在 MySQL 数据库中创建一个名为 addresslist 的数据库,在该数据库中创建一个名为 addressbook 的表格。当然,你也可以使用其他数据库,只需要修改连接数据库的代码即可。
阅读全文