学生管理系统
源码地址:仅用参考http://download.csdn.net/detail/u012332962/6493501
Java老师给我们小组安排了课程最后的一个实验课,做一个链接数据库(mysql或者sql server)的学生选课管理系统个人从周三开始用零碎的时间,慢慢动手今天终于大致全部功能都完成了,有点小bug在选择的时候,CheckBox显示与实际选择时候稍有延迟;
作品介绍:
一.建立的基础数据库及其各种表格信息:
二.程序界面及其主要功能:
3.主要功能:
(1).完成与mysl数据库的链接;
(2).登录的时候,从数据库总抽取数据添加到应用界面程序中;
(3).能同步数据库内存贮的学生选课信息并使用数据库内的用户数据进行登录操作;
(4).在退出程序后,数据能后保存在数据库中进行贮存.
四.程序原代码;
1.登录界面LoginFrame.java:
(1)在登录界面,用户选择用户界面登录的时候,连接数据库,并向主界面程序端传输链接数据库后的链接参数:
package com.rainplus;
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.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class LoginFrame extends JFrame {
private JPanel content;
private JLabel userLabel;
private JLabel passwordLabel;
private JPasswordField passwordField;
private JTextField userNameField;
private JButton loginButton;
private JButton exitButton;
public LoginFrame() {
super("学生选课系统");
setSize(250, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content = new JPanel();
content.setLayout(null);
add(content);
userLabel = new JLabel("学号:");
userLabel.setBounds(30, 30, 40, 20);
content.add(userLabel);
userNameField = new JTextField("11046208");
userNameField.setBounds(80, 30, 120, 20);
content.add(userNameField);
passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(30, 60, 40, 20);
content.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setBounds(80, 60, 120, 20);
content.add(passwordField);
loginButton = new JButton("登录");
loginButton.setBounds(30, 100, 80, 30);
loginButton.addActionListener(new buttonListenner());
content.add(loginButton);
exitButton = new JButton("退出");
exitButton.setBounds(120, 100, 80, 30);
exitButton.addActionListener(new buttonListenner());
content.add(exitButton);
/* 设置窗体可见和居中 */
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new LoginFrame();
}
class buttonListenner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (exitButton == e.getSource()) {
System.exit(0);
}
if (loginButton == e.getSource()) {
/* 对数据库的提取验证操作; */
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/school?user=root&password=root";
Connection conection = DriverManager.getConnection(url);
Statement statement = conection.createStatement();
String sql = "SELECT * FROM user";// 选择用户表
/* 获取结果集 */
ResultSet resultSet = statement.executeQuery(sql);
resultSet.next();
String userName = resultSet.getString(1);
while (!userName.equals(userNameField.getText())) {
resultSet.next();
userName = resultSet.getString(1);
}
if (userName.equals(userNameField.getText())) {
String password = resultSet.getNString(2);
if (password.equals(new String(passwordField
.getPassword()))) {
new MainFrame(userName,statement);
dispose();
} else {
JOptionPane.showMessageDialog(null, "密码错误!");
}
}
resultSet.close();
} catch (SQLException | ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null, "数据库链接出错");
}
}
}
}
}
2.主程序源代码MainFrame.java
利用登录界面传送过来的参数;对界面事件进行事件响应的时候进行对数据的处理操作
package com.rainplus;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
public class MainFrame extends JFrame {
private JPanel content;
private JPanel userPanel;
private JTable informationTable;
private JLabel course;
private Object[][] columsStrings;
String user;
Statement state;
int courses = 0;
public MainFrame(String userName, Statement statement) {
super();
user = userName;
state = statement;
getInformation(userName, statement);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
content = new JPanel();
add(content);
content.setLayout(new BorderLayout(5, 5));
/* 将窗体分割成两个部分1.已学科信息,2,课程部分 */
/* 1.选课信息 */
userPanel = new JPanel();
userPanel.setLayout(new FlowLayout());
add(userPanel, BorderLayout.NORTH);
userPanel.add(new JLabel("已选课程:"));
course = new JLabel();
userPanel.add(course);
/* 2,课程部分 */
JPanel coursePanel = new JPanel();
add(coursePanel, BorderLayout.CENTER);
informationTable = setTable(userName, statement);
informationTable.addMouseListener(new MouseAdapter());
JScrollPane courseScroll = new JScrollPane(informationTable);
coursePanel.add(courseScroll);
setVisible(true);
setLocationRelativeTo(null);
}
class TestTableCellRenderer extends JCheckBox implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
return this;
}
}
class MouseAdapter implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
setData(user, state);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
private void setData(String userName, Statement statement) {
int nRow = informationTable.getSelectedRow();
if ((boolean) informationTable.getValueAt(nRow, 4)) {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,
"是否确认选择这一课程!")) {
try {
String sql = "INSERT INTO studentcourse(username,cnumber) VALUE('"
+ userName + "','" + columsStrings[nRow][0] + "')";
statement.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "选课成功!");
getStudentCourse(userName, statement);
informationTable.setValueAt(true, nRow, 4);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "选课错误!");
}
} else {
// TODO
informationTable.setValueAt(false, nRow, 4);
}
} else {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,
"是否删除这一课程!")) {
try {
String sql = "DELETE FROM studentcourse WHERE username="
+ userName + " AND cnumber="
+ columsStrings[nRow][0];
statement.executeUpdate(sql);
getStudentCourse(userName, statement);
JOptionPane.showMessageDialog(null, "退选成功!");
informationTable.setValueAt(false, nRow, 4);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "退选错误!");
}
} else {
informationTable.setValueAt(true, nRow, 4);
}
}
}
private void getInformation(String userName, Statement statement) {
try {
String sql = "SELECT * FROM student where username=" + userName;
ResultSet resultSet = statement.executeQuery(sql);
resultSet.next();
String numString = resultSet.getString(1);
String nameString = resultSet.getString(2);
setTitle("欢迎登录:" + nameString + "同学,您的学号为:" + numString);
resultSet.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "获取学生信息错误!");
}
}
private JTable setTable(String userName, Statement statement) {
String[] columnames = { "书号", "名称", "学分", "学时", "选择 " };
try {
String sql = "SELECT * FROM course";
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
while (resultSet.next()) {
courses++;
}
int nColus = resultSetMetaData.getColumnCount();
resultSet.first();
columsStrings = new Object[courses][nColus + 1];
for (int i = 0; resultSet.next(); i++) {
for (int j = 1; j <= nColus; j++) {
columsStrings[i][j - 1] = new String(resultSet.getString(j));
}
}
resultSet.close();
// 默认选课状态为false
for (int i = 0; i < courses; i++) {
columsStrings[i][4] = new Boolean(false);
}
getStudentCourse(userName, statement);
resultSet.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "获取课程的数据错误2!");
e.printStackTrace();
}
// 设置表格的样式
DefaultTableModel tableModel;
tableModel = new DefaultTableModel(columsStrings, columnames) {
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex != 4)
return false;// 这个是可以编辑的列
return true;
}
};
JTable table = new JTable(tableModel);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(4).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(4).setCellRenderer(new TestTableCellRenderer());
table.setModel(tableModel);
return table;
}
private void getStudentCourse(String userName, Statement statement)
throws SQLException {
String sql;
ResultSet resultSet;
// 从数据库中获取该学生已经选了课程的消息
sql = "SELECT cname FROM course WHERE cnumber IN"
+ "(SELECT cnumber FROM studentcourse WHERE username ="
+ userName + ")";
resultSet = statement.executeQuery(sql);
// 设置单选框的选择状态
boolean flag = false;
while (resultSet.next()) {
for (int j = 0; j < courses; j++) {
if (resultSet.getString(1).equals(columsStrings[j][1])) {
columsStrings[j][4] = true;
flag = true;
}
}
}
// 显示已选课程状态
if (flag) {
StringBuffer buffer = new StringBuffer();
resultSet.first();
buffer.append(resultSet.getString(1));
while (resultSet.next()) {
buffer.append(", ");
buffer.append(resultSet.getString(1));
}
course.setText(buffer.toString());
} else {
course.setText("");
}
resultSet.close();
}
}
3运行程序:
五.已知Bug
在进行对数据的操作选择的时候,CheckBox显示与实际选择时候稍有延迟;