建立数据库和数据表来实现对一个简易的英汉电子词典进行存储和管理,这个简易电子词典中的内容至少包括:英文单词名、词性、汉语释义、例句(其它信息可自行扩充)。请编写图形界面的Java Application,完成对电子词典中英文单词的查找、新词的添加、单词的修改、删除等功能。
时间: 2023-07-10 16:38:09 浏览: 186
好的,下面是一个简单的Java Application示例代码,可以实现对电子词典的增删改查功能。需要注意的是,这里使用了JavaFX框架来创建图形用户界面,并使用JDBC连接MySQL数据库。如果您使用的是Microsoft Access数据库,可以根据需要进行相应的修改。
```java
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.*;
public class DictionaryApp extends Application {
private final String HOST = "localhost";
private final String PORT = "3306";
private final String DATABASE = "dictionary";
private final String USERNAME = "root";
private final String PASSWORD = "password";
private final String TABLE_NAME = "words";
private final String FIELD_ID = "id";
private final String FIELD_WORD = "word";
private final String FIELD_POS = "pos";
private final String FIELD_MEANING = "meaning";
private final String FIELD_EXAMPLE = "example";
private Connection connection;
private TextField wordField;
private ComboBox<String> posComboBox;
private TextArea meaningArea;
private TextArea exampleArea;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// 创建数据库连接
connectToDatabase();
// 创建界面
Label title = new Label("英汉电子词典");
Label wordLabel = new Label("单词:");
wordField = new TextField();
Label posLabel = new Label("词性:");
posComboBox = new ComboBox<>();
posComboBox.getItems().addAll("n.", "v.", "adj.", "adv.", "prep.", "conj.", "pron.");
posComboBox.getSelectionModel().selectFirst();
Label meaningLabel = new Label("释义:");
meaningArea = new TextArea();
Label exampleLabel = new Label("例句:");
exampleArea = new TextArea();
GridPane inputPane = new GridPane();
inputPane.setVgap(10);
inputPane.setHgap(10);
inputPane.add(wordLabel, 0, 0);
inputPane.add(wordField, 1, 0);
inputPane.add(posLabel, 0, 1);
inputPane.add(posComboBox, 1, 1);
inputPane.add(meaningLabel, 0, 2);
inputPane.add(meaningArea, 1, 2);
inputPane.add(exampleLabel, 0, 3);
inputPane.add(exampleArea, 1, 3);
Button addButton = new Button("添加");
addButton.setOnAction(event -> addWord());
Button searchButton = new Button("查找");
searchButton.setOnAction(event -> searchWord());
Button modifyButton = new Button("修改");
modifyButton.setOnAction(event -> modifyWord());
Button deleteButton = new Button("删除");
deleteButton.setOnAction(event -> deleteWord());
HBox buttonBox = new HBox(10);
buttonBox.getChildren().addAll(addButton, searchButton, modifyButton, deleteButton);
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.getChildren().addAll(title, inputPane, buttonBox);
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.show();
}
private void connectToDatabase() throws SQLException {
String url = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE + "?serverTimezone=UTC";
connection = DriverManager.getConnection(url, USERNAME, PASSWORD);
}
private void addWord() {
try {
String word = wordField.getText().trim();
String pos = posComboBox.getSelectionModel().getSelectedItem();
String meaning = meaningArea.getText().trim();
String example = exampleArea.getText().trim();
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO " + TABLE_NAME + " (" + FIELD_WORD + ", " + FIELD_POS + ", " + FIELD_MEANING + ", " + FIELD_EXAMPLE + ") VALUES (?, ?, ?, ?)"
);
statement.setString(1, word);
statement.setString(2, pos);
statement.setString(3, meaning);
statement.setString(4, example);
statement.executeUpdate();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("添加单词");
alert.setHeaderText(null);
alert.setContentText("添加成功!");
alert.showAndWait();
} catch (SQLException e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("添加单词");
alert.setHeaderText(null);
alert.setContentText("添加失败:" + e.getMessage());
alert.showAndWait();
}
}
private void searchWord() {
try {
String word = wordField.getText().trim();
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM " + TABLE_NAME + " WHERE " + FIELD_WORD + " = ?"
);
statement.setString(1, word);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String pos = resultSet.getString(FIELD_POS);
String meaning = resultSet.getString(FIELD_MEANING);
String example = resultSet.getString(FIELD_EXAMPLE);
posComboBox.getSelectionModel().select(pos);
meaningArea.setText(meaning);
exampleArea.setText(example);
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("查找单词");
alert.setHeaderText(null);
alert.setContentText("未找到该单词!");
alert.showAndWait();
}
} catch (SQLException e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("查找单词");
alert.setHeaderText(null);
alert.setContentText("查找失败:" + e.getMessage());
alert.showAndWait();
}
}
private void modifyWord() {
try {
String word = wordField.getText().trim();
String pos = posComboBox.getSelectionModel().getSelectedItem();
String meaning = meaningArea.getText().trim();
String example = exampleArea.getText().trim();
PreparedStatement statement = connection.prepareStatement(
"UPDATE " + TABLE_NAME + " SET " + FIELD_POS + " = ?, " + FIELD_MEANING + " = ?, " + FIELD_EXAMPLE + " = ? WHERE " + FIELD_WORD + " = ?"
);
statement.setString(1, pos);
statement.setString(2, meaning);
statement.setString(3, example);
statement.setString(4, word);
int rowCount = statement.executeUpdate();
if (rowCount > 0) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("修改单词");
alert.setHeaderText(null);
alert.setContentText("修改成功!");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("修改单词");
alert.setHeaderText(null);
alert.setContentText("未找到要修改的单词!");
alert.showAndWait();
}
} catch (SQLException e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("修改单词");
alert.setHeaderText(null);
alert.setContentText("修改失败:" + e.getMessage());
alert.showAndWait();
}
}
private void deleteWord() {
try {
String word = wordField.getText().trim();
PreparedStatement statement = connection.prepareStatement(
"DELETE FROM " + TABLE_NAME + " WHERE " + FIELD_WORD + " = ?"
);
statement.setString(1, word);
int rowCount = statement.executeUpdate();
if (rowCount > 0) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("删除单词");
alert.setHeaderText(null);
alert.setContentText("删除成功!");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("删除单词");
alert.setHeaderText(null);
alert.setContentText("未找到要删除的单词!");
alert.showAndWait();
}
} catch (SQLException e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("删除单词");
alert.setHeaderText(null);
alert.setContentText("删除失败:" + e.getMessage());
alert.showAndWait();
}
}
@Override
public void stop() throws Exception {
connection.close();
super.stop();
}
}
```
这个程序实现了一个简单的英汉电子词典,可以通过图形用户界面实现对词典中单词的增删改查等功能。需要注意的是,这个程序只是一个示例,您可以根据自己的需要进行相应的修改和扩展。
阅读全文