用Microsoft Access作为DBMS,建立数据库和数据表来实现对一个简易的英汉电子词典进行存储和管理,这个简易电子词典中的内容至少包括:英文单词名、词性、汉语释义、例句(其它信息可自行扩充)。请编写图形界面的Java Application,完成对电子词典中英文单词的查找、新词的添加、单词的修改、删除等功能。
时间: 2024-03-26 13:34:21 浏览: 7
好的,以下是一个简单的Java Application示例,实现了对Microsoft Access电子词典的基本操作:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DictionaryApp extends JFrame implements ActionListener {
private static final String DB_URL = "jdbc:ucanaccess://dictionary.accdb";
private static final String DB_USER = "";
private static final String DB_PASSWORD = "";
private JLabel wordLabel, typeLabel, meaningLabel, exampleLabel;
private JTextField wordField, typeField, meaningField, exampleField;
private JButton addButton, searchButton, updateButton, deleteButton;
private Connection conn;
private Statement stmt;
public DictionaryApp() {
super("Dictionary App");
// 创建界面
wordLabel = new JLabel("Word:");
typeLabel = new JLabel("Type:");
meaningLabel = new JLabel("Meaning:");
exampleLabel = new JLabel("Example:");
wordField = new JTextField(20);
typeField = new JTextField(20);
meaningField = new JTextField(20);
exampleField = new JTextField(20);
addButton = new JButton("Add");
searchButton = new JButton("Search");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
JPanel panel = new JPanel(new GridLayout(5, 2));
panel.add(wordLabel);
panel.add(wordField);
panel.add(typeLabel);
panel.add(typeField);
panel.add(meaningLabel);
panel.add(meaningField);
panel.add(exampleLabel);
panel.add(exampleField);
panel.add(addButton);
panel.add(searchButton);
panel.add(updateButton);
panel.add(deleteButton);
addButton.addActionListener(this);
searchButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
add(panel);
// 连接数据库
try {
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addWord();
} else if (e.getSource() == searchButton) {
searchWord();
} else if (e.getSource() == updateButton) {
updateWord();
} else if (e.getSource() == deleteButton) {
deleteWord();
}
}
private void addWord() {
String word = wordField.getText();
String type = typeField.getText();
String meaning = meaningField.getText();
String example = exampleField.getText();
if (word.isEmpty() || type.isEmpty() || meaning.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter word, type, and meaning");
return;
}
try {
String query = "INSERT INTO dictionary (word, type, meaning, example) VALUES ('" + word + "','" + type + "','" + meaning + "','" + example + "')";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Word added successfully");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error adding word");
}
}
private void searchWord() {
String word = wordField.getText();
if (word.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter word to search");
return;
}
try {
String query = "SELECT * FROM dictionary WHERE word='" + word + "'";
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
typeField.setText(rs.getString("type"));
meaningField.setText(rs.getString("meaning"));
exampleField.setText(rs.getString("example"));
} else {
JOptionPane.showMessageDialog(this, "Word not found");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error searching word");
}
}
private void updateWord() {
String word = wordField.getText();
String type = typeField.getText();
String meaning = meaningField.getText();
String example = exampleField.getText();
if (word.isEmpty() || type.isEmpty() || meaning.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter word, type, and meaning");
return;
}
try {
String query = "UPDATE dictionary SET type='" + type + "', meaning='" + meaning + "', example='" + example + "' WHERE word='" + word + "'";
int rows = stmt.executeUpdate(query);
if (rows > 0) {
JOptionPane.showMessageDialog(this, "Word updated successfully");
} else {
JOptionPane.showMessageDialog(this, "Word not found");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error updating word");
}
}
private void deleteWord() {
String word = wordField.getText();
if (word.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter word to delete");
return;
}
try {
String query = "DELETE FROM dictionary WHERE word='" + word + "'";
int rows = stmt.executeUpdate(query);
if (rows > 0) {
JOptionPane.showMessageDialog(this, "Word deleted successfully");
wordField.setText("");
typeField.setText("");
meaningField.setText("");
exampleField.setText("");
} else {
JOptionPane.showMessageDialog(this, "Word not found");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error deleting word");
}
}
public static void main(String[] args) {
new DictionaryApp();
}
}
```
请注意,该示例代码仅提供了一个基本的用户界面和单词管理功能,您可能需要根据需要进行修改和扩展,以满足您的项目要求。同时,您需要更改示例代码中的数据库URL、用户名和密码,以匹配您自己的Microsoft Access数据库。