Java+Swing+dat文件存储实现学生选课管理系统
- 一、系统介绍
- 二、系统展示
- 1.用户登陆、注册
- 2.课程信息查询
- 3.添加课程
- 4.选课
- 5.退课
- 三、系统实现
- 四、其他
- 1.其它系统
一、系统介绍
功能展示:用户注册、用户登陆
课程管理:课程信息的查询、添加、删除
我的选课:查询、退课
二、系统展示
1.用户登陆、注册
2.课程信息查询
3.添加课程
4.选课
5.退课
三、系统实现
StudentClient.java
package course;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import course.MainPanel;
public class StudentClient
{
// 定义布局
BorderLayout mainLayout = new BorderLayout();
CardLayout layout = new CardLayout();
JFrame bamFrame;
MainPanel mainPanel = new MainPanel();// 主面板
JPanel upPanel = new JPanel();// 主面板中的---上面板
JPanel downPanel = new JPanel();// 主面板中的---下面板
CourseDao coursedao = new CourseDao();
AddCoursePanel addCoursePanel = new AddCoursePanel();//添加课程页面
final Object[] columnNames = {"课程编号", "课程名称", "备注"}; //列名最好用final修饰
Object[][] rowData = queryData("");
DefaultTableModel model = new DefaultTableModel(rowData,columnNames);
JTable dataTable = new JTable (model);
JScrollPane pane1;
JScrollPane pane3;
public StudentClient() {
downPanel.setLayout(layout);
dataTable.setPreferredScrollableViewportSize(new Dimension(650, 100));//设置表格的大小
dataTable.setRowHeight (30);//设置每行的高度为20
dataTable.setRowHeight (0, 20);//设置第1行的高度为15
dataTable.setRowMargin (5);//设置相邻两行单元格的距离
dataTable.setSelectionBackground (Color.white);//设置所选择行的背景色
dataTable.setSelectionForeground (Color.blue);//设置所选择行的前景色
//friends.setGridColor (Color.black);//设置网格线的颜色
dataTable.selectAll ();//选择所有行
//dataTable.setRowSelectionInterval (0,2);//设置初始的选择行,这里是1到3行都处于选择状态
dataTable.clearSelection ();//取消选择
dataTable.setDragEnabled (false);//不懂这个
dataTable.setShowGrid (false);//是否显示网格线
dataTable.setShowHorizontalLines (false);//是否显示水平的网格线
dataTable.setShowVerticalLines (true);//是否显示垂直的网格线
//friends.setValueAt ("tt", 0, 0);//设置某个单元格的值,这个值是一个对象
dataTable.doLayout ();
dataTable.setBackground (Color.lightGray);
// 设置表格中的数据居中显示
DefaultTableCellRenderer r=new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
dataTable.setDefaultRenderer(Object.class,r);
pane1 = new JScrollPane (addCoursePanel);//JTable最好加在JScrollPane上
pane1.setPreferredSize(new Dimension (650,300));
//JScrollPane pane2 = new JScrollPane (example2);
pane3 = new JScrollPane (dataTable);
pane3.setPreferredSize(new Dimension (650,300));
downPanel.add("cardSelect", pane3);// 卡片一 (添加)
downPanel.add("cardAdd", pane1);// 卡片一 (查询)
upPanel.add(mainPanel);
//downPanel.add(addCoursePanel);
//downPanel.add(pane1);
// 定义主窗体
bamFrame = new JFrame("学生选课管理");
bamFrame.setLayout(mainLayout);
bamFrame.setSize(650, 400);// 设定窗体大小
// 给窗体加面板
bamFrame.add(upPanel, BorderLayout.NORTH);
bamFrame.add(downPanel, BorderLayout.SOUTH);
addListeners();// 调用监听方法
bamFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//bamFrame.pack();
bamFrame.setVisible(true);
}
// 窗体中部分对象监听方法
private void addListeners() {
// 查询按钮
mainPanel.getSelectButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cname = mainPanel.getCourseField().getText();
System.out.print(cname);
Object[][] rowData2 = queryData(cname);
model.setDataVector(rowData2, columnNames);
layout.show(downPanel, "cardSelect");
}
});
// 添加按钮
mainPanel.getAddButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(downPanel, "cardAdd");
}
});
// 点击《提交》按钮
addCoursePanel.getOkButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String courseid = addCoursePanel.getCourseIdText().getText();
String coursename = addCoursePanel.getCourseNameText().getText();
String note = addCoursePanel.getNoteText().getText();
int i = addCC(courseid, coursename, coursename);
if(i>0) {
Object[][] rowData2 = queryData("");
model.setDataVector(rowData2, columnNames);
layout.show(downPanel, "cardSelect");
}
}
});
// 点击《返回》按钮
addCoursePanel.getBackButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(downPanel, "cardSelect");
}
});
// 点击《退课》按钮
mainPanel.getDelButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = dataTable.getSelectedRow();
if(i>0) {
int index1 = dataTable.getSelectedRow();//获取选中的行
String courseid = (String) dataTable.getValueAt(index1, 0);
int j=coursedao.DelCourse(courseid);
if(j>0) {
JOptionPane.showMessageDialog(downPanel, "退课成功!");
Object[][] rowData2 = queryData("");
model.setDataVector(rowData2, columnNames);
}else {
JOptionPane.showMessageDialog(downPanel, "退课失败!");
Object[][] rowData2 = queryData("");
model.setDataVector(rowData2, columnNames);
}
}else {
JOptionPane.showMessageDialog(downPanel, "请选择要退的课程!");
}
}
});
}
public static void main (String[] args)
{
new StudentClient();
}
//生成表格数据
/**
* @return
*/
public Object[][] queryData(String coursename){
List<Course> list=coursedao.queryAllCourse(coursename);
rowData=new Object[list.size()][columnNames.length];
for(int i=0;i<list.size();i++){
for(int j=0;j<rowData.length;j++){
rowData[i][0]=list.get(i).getCourseid();
rowData[i][1]=list.get(i).getCoursename();
rowData[i][2]=list.get(i).getNote();
}
}
return rowData;
}
//添加课程
/**
* @return
*/
public int addCC(String courseid,String coursename,String note){
int result = 0;
if ( addCoursePanel.getCourseIdText().getText().equals(""))
JOptionPane.showMessageDialog(addCoursePanel, "课程编号不能为空");
else if (addCoursePanel.getCourseNameText().getText().equals(""))
JOptionPane.showMessageDialog(addCoursePanel, "课程名称不能为空");
else {
result=coursedao.AddCourse(courseid, coursename, coursename);
}
return result;
}
}
CourseDao.java
package course;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CourseDao {
private Connection conn=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
//查询课程
public List<Course> queryAllCourse(String coursename){
String sql="select * from course where coursename like '%" + coursename + "%' or courseid like '%" + coursename +"%'";
List<Course> list=new ArrayList<Course>();
try {
conn=DbUtils.getConnection();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
System.out.println(ps.toString());
while(rs.next()){
Course course=new Course();
course.setCourseid(rs.getString(1));
course.setCoursename(rs.getString(2));
course.setNote(rs.getString(3));
list.add(course);
}
DbUtils.close(rs, ps, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//添加课程
public int AddCourse(String courseid,String coursename,String note){
int result=0;
String sql="insert into course(courseid,coursename,note) values(?,?,?)";
List<Course> list=new ArrayList<Course>();
try {
conn=DbUtils.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, courseid);
ps.setString(2, coursename);
ps.setString(3, note);
result =ps.executeUpdate();// 返回值代表收到影响的行数
DbUtils.close(rs, ps, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//退课
public int DelCourse(String courseid){
int result=0;
String sql="DELETE from course where courseid = '" + courseid + "'";
List<Course> list=new ArrayList<Course>();
try {
conn=DbUtils.getConnection();
ps = conn.prepareStatement(sql);
result =ps.executeUpdate();// 返回值代表收到影响的行数
DbUtils.close(rs, ps, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
四、其他
1.其它系统
Java+Swing学生信息管理系统
Java+Swing实现学生选课管理系统
Java+Swing图书管理系统