根据以下要求,为我编写Java代码:使用MySQL数据库。在数据库中使用表存储单词和该单词的翻译解释,例如“sun,太阳”“moon,月亮”等。 通过 GUI 界面管理单词簿。可以向单词簿添加单词,可以修改单词簿中的单词,可以删除单词簿中的单词。 通过GUI 界面查询单词。可以查询一个,随机查询若干个或全部单词。
时间: 2023-12-06 18:45:04 浏览: 132
java+mysql开发的单词簿源码.zip
以下是一个Java代码示例,基于MySQL数据库,实现了单词簿的GUI管理和查询功能。你需要先创建一个名为"wordbook"的数据库,并在其中创建一个名为"words"的表,该表包含两个字段:word和translation,分别表示单词和翻译。
```
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class WordbookGUI extends JFrame {
private static final String DB_URL = "jdbc:mysql://localhost/wordbook";
private static final String DB_USER = "username";
private static final String DB_PASSWORD = "password";
private JTextField wordField, translationField;
private JButton addButton, updateButton, deleteButton, queryButton;
private JTextArea resultArea;
public WordbookGUI() {
super("Wordbook");
wordField = new JTextField(20);
translationField = new JTextField(20);
addButton = new JButton("Add");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
queryButton = new JButton("Query");
resultArea = new JTextArea(15, 40);
JPanel inputPanel = new JPanel(new GridLayout(2, 2));
inputPanel.add(new JLabel("Word:"));
inputPanel.add(wordField);
inputPanel.add(new JLabel("Translation:"));
inputPanel.add(translationField);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(queryButton);
JScrollPane resultScrollPane = new JScrollPane(resultArea);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(resultScrollPane, BorderLayout.SOUTH);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addWord();
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateWord();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteWord();
}
});
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
queryWords();
}
});
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void addWord() {
String word = wordField.getText().trim();
String translation = translationField.getText().trim();
if (word.isEmpty() || translation.isEmpty()) {
JOptionPane.showMessageDialog(this, "Word and translation cannot be empty!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO words (word, translation) VALUES (?, ?)")) {
stmt.setString(1, word);
stmt.setString(2, translation);
stmt.executeUpdate();
resultArea.setText("Word added: " + word + " -> " + translation);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private void updateWord() {
String word = wordField.getText().trim();
String translation = translationField.getText().trim();
if (word.isEmpty() || translation.isEmpty()) {
JOptionPane.showMessageDialog(this, "Word and translation cannot be empty!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement stmt = conn.prepareStatement("UPDATE words SET translation = ? WHERE word = ?")) {
stmt.setString(1, translation);
stmt.setString(2, word);
int count = stmt.executeUpdate();
if (count == 0) {
resultArea.setText("Word not found: " + word);
} else {
resultArea.setText("Word updated: " + word + " -> " + translation);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private void deleteWord() {
String word = wordField.getText().trim();
if (word.isEmpty()) {
JOptionPane.showMessageDialog(this, "Word cannot be empty!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement stmt = conn.prepareStatement("DELETE FROM words WHERE word = ?")) {
stmt.setString(1, word);
int count = stmt.executeUpdate();
if (count == 0) {
resultArea.setText("Word not found: " + word);
} else {
resultArea.setText("Word deleted: " + word);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private void queryWords() {
String word = wordField.getText().trim();
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
if (word.isEmpty()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM words");
StringBuilder sb = new StringBuilder();
while (rs.next()) {
sb.append(rs.getString("word")).append(" -> ").append(rs.getString("translation")).append("\n");
}
resultArea.setText(sb.toString());
} else {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM words WHERE word = ?");
stmt.setString(1, word);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
resultArea.setText(rs.getString("word") + " -> " + rs.getString("translation"));
} else {
resultArea.setText("Word not found: " + word);
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WordbookGUI();
}
});
}
}
```
阅读全文