用java开发一个在线学习学生系统中的题库管理部分,并要有GUI部分。其中管理员(manager)能增删查改改题库。
时间: 2023-06-19 22:04:48 浏览: 74
好的,您可以按照以下步骤来实现在线学习学生系统中的题库管理部分:
1. 创建数据库表
首先,您需要创建一个名为“题库(question)”的数据库表,其中包含以下字段:
- id: 题目ID,自增长的整数类型。
- content: 题目内容,VARCHAR 类型。
- answer: 答案,VARCHAR 类型。
- type: 题目类型,VARCHAR 类型。
2. 创建 Java 类
接下来,您需要创建一个名为“Question”的 Java 类,该类将映射到数据库表中的每个字段。您可以使用以下代码开始:
```
public class Question {
private int id;
private String content;
private String answer;
private String type;
// getter and setter methods
}
```
3. 创建 DAO 类
接下来,您需要创建一个名为“QuestionDAO”的 Java 类,该类用于执行与数据库相关的操作。您可以使用以下代码开始:
```
public class QuestionDAO {
private Connection connection;
public QuestionDAO() {
// 获取数据库连接
connection = getConnection();
}
// 获取数据库连接
private Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name", "your_username", "your_password");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
// 增加题目
public void addQuestion(Question question) {
String sql = "INSERT INTO question (content, answer, type) VALUES (?, ?, ?)";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, question.getContent());
ps.setString(2, question.getAnswer());
ps.setString(3, question.getType());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除题目
public void deleteQuestion(int id) {
String sql = "DELETE FROM question WHERE id=?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改题目
public void updateQuestion(Question question) {
String sql = "UPDATE question SET content=?, answer=?, type=? WHERE id=?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, question.getContent());
ps.setString(2, question.getAnswer());
ps.setString(3, question.getType());
ps.setInt(4, question.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询题目
public Question getQuestion(int id) {
Question question = null;
String sql = "SELECT * FROM question WHERE id=?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
question = new Question();
question.setId(rs.getInt("id"));
question.setContent(rs.getString("content"));
question.setAnswer(rs.getString("answer"));
question.setType(rs.getString("type"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return question;
}
// 查询所有题目
public List<Question> getAllQuestions() {
List<Question> questions = new ArrayList<>();
String sql = "SELECT * FROM question";
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Question question = new Question();
question.setId(rs.getInt("id"));
question.setContent(rs.getString("content"));
question.setAnswer(rs.getString("answer"));
question.setType(rs.getString("type"));
questions.add(question);
}
} catch (SQLException e) {
e.printStackTrace();
}
return questions;
}
}
```
4. 创建 GUI 界面
最后,您需要创建一个名为“QuestionManager”的 GUI 界面,该界面包含一个表格和一组按钮,用于执行增加、删除、修改和查询操作。您可以使用以下代码开始:
```
public class QuestionManager extends JFrame {
private JTable table;
private QuestionTableModel model;
private QuestionDAO dao;
public QuestionManager() {
super("题库管理");
dao = new QuestionDAO();
model = new QuestionTableModel(dao.getAllQuestions());
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
JButton addButton = new JButton("增加");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
QuestionDialog dialog = new QuestionDialog(QuestionManager.this, "增加题目");
dialog.setVisible(true);
if (dialog.isOK()) {
Question question = dialog.getQuestion();
dao.addQuestion(question);
model.addQuestion(question);
}
}
});
buttonPanel.add(addButton);
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
int id = (int) table.getValueAt(selectedRow, 0);
dao.deleteQuestion(id);
model.deleteQuestion(selectedRow);
}
}
});
buttonPanel.add(deleteButton);
JButton updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
int id = (int) table.getValueAt(selectedRow, 0);
Question question = dao.getQuestion(id);
QuestionDialog dialog = new QuestionDialog(QuestionManager.this, "修改题目", question);
dialog.setVisible(true);
if (dialog.isOK()) {
question = dialog.getQuestion();
dao.updateQuestion(question);
model.updateQuestion(selectedRow, question);
}
}
}
});
buttonPanel.add(updateButton);
JButton queryButton = new JButton("查询");
queryButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String type = JOptionPane.showInputDialog(QuestionManager.this, "请输入题目类型");
if (type != null && !type.isEmpty()) {
List<Question> questions = dao.getAllQuestions();
List<Question> filteredQuestions = new ArrayList<>();
for (Question question : questions) {
if (question.getType().equals(type)) {
filteredQuestions.add(question);
}
}
model.setQuestions(filteredQuestions);
}
}
});
buttonPanel.add(queryButton);
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new QuestionManager().setVisible(true);
}
});
}
}
```
5. 创建表格模型类
最后,您需要创建一个名为“QuestionTableModel”的表格模型类,该类用于显示所有题目。您可以使用以下代码开始:
```
public class QuestionTableModel extends AbstractTableModel {
private static final String[] COLUMN_NAMES = {"ID", "内容", "答案", "类型"};
private List<Question> questions;
public QuestionTableModel(List<Question> questions) {
this.questions = questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
fireTableDataChanged();
}
public void addQuestion(Question question) {
questions.add(question);
fireTableRowsInserted(questions.size() - 1, questions.size() - 1);
}
public void deleteQuestion(int rowIndex) {
questions.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
}
public void updateQuestion(int rowIndex, Question question) {
questions.set(rowIndex, question);
fireTableRowsUpdated(rowIndex, rowIndex);
}
@Override
public int getRowCount() {
return questions.size();
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public String getColumnName(int columnIndex) {
return COLUMN_NAMES[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Integer.class;
case 1:
case 2:
case 3:
return String.class;
default:
return Object.class;
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Question question = questions.get(rowIndex);
switch (columnIndex) {
case 0:
return question.getId();
case 1:
return question.getContent();
case 2:
return question.getAnswer();
case 3:
return question.getType();
default:
return null;
}
}
}
```
至此,您已经成功实现了在线学习学生系统中的题库管理部分,并且具有GUI界面,管理员(manager)能增删查改题库。
阅读全文