@
目录一、系统介绍
本系统实现了学生登录和管理员登录,学生实现选课,查看已选课程,修改密码,查看学生信息功能。管理员实现选课信息的增删改查,学生信息查询,学生密码修改功能。
二、系统展示
1.课程查询
2.课程添加
3.退课
三、系统实现
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;
}
}
四、获取源码
请联系QQ:2080866767
标签:java,Java,String,选课,Swing,ps,import,new,dataTable From: https://www.cnblogs.com/Timi2019/p/16982291.html