完成学生选课管理系统的开发
本系统是一个单机版的小型的学生选课管理系统,在本系统中,开发了一个面向管理员、学生和教师这三个对象的教务平台,对学生提供的服务有登录、选课、、修改登录密码、和查询成绩这几个服务,所选课程总数不能超过3门;对教师提供的服务有登录、修改登录密码和登录成绩;对管理员提供的服务有登录开设学生和教师帐号、删除学生和教师帐号的服务。
(1)架构:单机
(2)运行软件环境: windows XP SP3 、jdk1.6
(3)开发环境:
硬件平台:
CPU:P41.8GHz
内存:256MB以上
软件平台:windows XP SP3 、jdk1.6
操作系统:WindowsXP
数据库:SQL Server 2000、SQLServer SP4补丁
开发工具:Eclipse 3.3
分辨率:1024*768
各模块功能:
(1)E-R图
(2)关系模式
学生(学生学号,学生姓名,教师性别,教师生日,所在班级)
教师(教师编号,教师姓名,教师性别,教师生日,教师职称,所在院系)
课程(课程号,课程名,学分,选课人数)
选课(课程号,学生学号,成绩)
授课(课程号,教师编号,上课地点)
(3)数据库管理系统:Microsoft SQL Server 2000
(4)数据库命名
StudentManager
(5)数据库表
Logon(登录帐号密码)
主要用来保存用户登录信息
字段名 | 数据类型 | 长度 | 是否为空 | 是否主键 |
---|---|---|---|---|
UserID | char | 10 | 不为空 | 主键 |
Passwordr | charr | 10 |
StudentTable1(学生信息表)
主要用来保存学生信息。
字段名 | 数据类型 | 长度 | 主键否 | 是否为空 | 描述 |
---|---|---|---|---|---|
StudentID | Char | 10 | 主键 | 不为空 | 学生学号 |
StudentName | Char | 10 | 不为空 | 学生姓名 | |
StudentSex | Char | 2 | 不为空 | 学生性别 | |
StudentBirthday | Datetime | 8 | 学生生日 | ||
Class | char | 16 | 所在班级 |
TeacherTable1(教师信息表)
用来储存教师的基本信息
字段名 | 数据类型 | 长度 | 主键否 | 是否为空 | 描述 |
---|---|---|---|---|---|
TeacherID | Char | 10 | 主键 | 不为空 | 教师编号 |
TeacherName | Char | 10 | 不为空 | 教师姓名 | |
TeacherSex | Char | 2 | 不为空 | 教师性别 | |
TeacherBirthday | Datetime | 8 | 教师生日 | ||
Post | char | 10 | 教师职称 | ||
Department | char | 20 | 所在院系 |
CourseTable(课程信息表)
字段名 | 数据类型 | 长度 | 主键否 | 是否为空 | 描述 |
---|---|---|---|---|---|
CourseID | Char | 16 | 主键 | 不为空 | 课程编号 |
CourseName | Char | 16 | 不为空 | 课程名 | |
Point | Char | 8 | 不为空 | 学分 | |
StuNumber | Datetime | 4 | 不为空 | 选课人数 |
ScoreTable(学生成绩表)
用于存储学生成绩
字段名 | 数据类型 | 长度 | 是否为空 | 是否主键 |
---|---|---|---|---|
CourseID | char | 16 | 不为空 | 主键 |
StudentID | char | 10 | 不为空 | 主键 |
Score | int | 4 |
TeachTable(j教师授课表)
字段名 | 数据类型 | 长度 | 是否为空 | 是否主键 |
---|---|---|---|---|
CourseID | char | 16 | 不为空 | 主键 |
TeacherID | char | 10 | 不为空 | 主键 |
Location | int | 10 | ||
(8)数据库账户及权限 (截图) |
学生账户表
教师账户表
用户登录表:
(9)数据库存储过程:
AllCourse:
create proc AllCourse
as
begin
select x.CourseID,x.CourseName,x.Point ,y.TeacherName,y.Post,z.Location,x.StuNumber
from Course x,TeacherTable1 y,TeachTable z
where x.CourseID=z.CourseID and y.TeacherID=z.TeacherID
end
DeleteLogon :
create proc DeleteLogon(@id char(10))
as
begin
delete from Logon
where UserID=@id
end
DeleteStudent:
create proc DeleteStudent(@id char(10))
as
begin
delete from StudentTable1
where StudentID=@id
end
DeleteTeacher :
create proc DeleteTeacher(@id char(10))
as
begin
delete from TeacherTable1
where TeacherID=@id
end
InsertLogon :
create proc InsertLogon(@id char(10))
as
begin
insert into Logon
values(@id,@id)
end
InsertStudent :
create proc InsertStudent(@userid char(10),@username char(10),@sex char(2),@birth datetime,@class char(10))
as
begin
insert into StudentTable1 values(@userid ,@username,@sex,@birth,@class)
end
return
InsertTeacher:
create proc InsertTeacher(@userid char(10),@username char(10),@sex char(2),@birth datetime,@post char(10),@department char(10))
as
begin
insert into TeacherTable1 values(@userid ,@username,@sex,@birth,@post,@department)
end
return
IsExistsStu :
create proc IsExistsStu(@id char(10))
as
begin
select * from StudentTable1
where StudentID=@id
end
IsExistsTea:
create proc IsExistsTea(@id char(10))
as
begin
select * from Teachertable1
where TeacherID=@id
end
ProcAllStu :
create proc ProcAllStu
as
begin
select * from StudentTable1
end
ProcAllTea
create proc ProcAllTea
as
begin
select * from TeacherTable1
end
ProcLogon:
create proc ProcLogon(@userid char(16),@password char(10))
as
begin
select *
from Logon
where UserID=@userid and Password=@password
end
return
ProcModify:
create proc ProcModify(@id char(10),@password char(16))
as
begin
update Logon
set Password=@password
where UserID=@id
end
ProcStudent :
create proc ProcStudent(@id char(10))
as
begin
select * from StudentTable1
where StudentID=@id
end
SelectCourse :
create proc SelectCourse(@id char(10),@courseid char(16))
as
begin
insert into ScoreTable
values(@courseid,@id,null)
end
SelectedCourse:
create proc SelectedCourse(@id char(10))
as
begin
select * from ScoreTable
where @id=StudentID
end
SelectedCourseNum:
create proc SelectedCourseNum(@id char(10))
as
begin
select COUNT(*)
from Scoretable
where StudentID=@id
end
SelectedDetail :
create proc SelectedDetail(@id char(10))
as
begin
select x.CourseID,x.CourseName,x.Point ,y.TeacherName,y.Post,z.Location,s.Score
from Course x,TeacherTable1 y,TeachTable z,ScoreTable s
where @id=s.StudentID and x.CourseID=z.CourseID and z.TeacherID=y.TeacherID
and x.CourseID=s.CourseID
end
4.系统界面设计
(1)窗体功能描述
登录界面Logon.java
管理员以帐号Admin密码123登录成功进入管理员的信息管理界面,通过点击“学生信息管理”和“教师信息管理”进入不同的管理界面,学生信息管理界面如下:
教师信息管理界面如下:
以学生帐号(如:帐号:S001001,密码:S001001)登陆成功后进入如下界面,首先显示的 是学生的基本信息:
点击“课程列表”按钮进入如下界面
根据自己的跟人兴趣选择课程,选择的课程数目不能超过3门否则弹出对话框如下:
点击“确定”跳转到已选课程列表。
在主界面点击“已选课程”按钮也可以进入下面的界面查看已经选择的课程:
以教师帐号(如:帐号:T01001,密码:T01001)登陆成功后进入如下界面,首先显示的 是教师的基本信息,
点击“录入成绩”可以通过表格来录入学生的成绩,界面如下图所示:
(2)页面/窗体关系结构图
为了方便管理,将数据库的封装分成两部分,数据库资源配置文件和封装数据库操作的类SqlManager.java:
#Sepecify the system type: window or unix
system-type=windows
#specify the database's type
database-type=sqlserver
#specify some parameters
DBhost=localhost
DBport=1433
DBname=StudentManager
DBuser=sa
DBpassword=
封装数据库操作的类**:SqlManager.java**
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
public class SqlManager {
private static SqlManager p=null;
private PropertyResourceBundle bundle;
private static String jdbcDriver=null;
private static String split=null;
private String DBType=null;
private String DBhost="localhost";
private String DBname="";
private String DBport="";
private String DBuser="";
private String DBpassword="";
private Connection Sqlconn=null;
private Statement Sqlstmt=null;
private String strCon=null;
private SqlManager(){
try{
bundle=new PropertyResourceBundle(SqlManager.class.
getResourceAsStream("/sysConfig.properties"));
this.DBhost=getString("DBhost");
this.DBname=getString("DBname");
this.DBport=getString("DBport");
this.DBuser=getString("DBuser");
this.DBpassword=getString("DBpassword");
String system_type=getString("system-type");
if(system_type!=null){
if(system_type!=null){
if(system_type.toLowerCase().equals("widows"))
split=";";
else if(system_type.toLowerCase().equals("unix"))
split=":";
}
String database_type=getString("database-type");
this.DBType=database_type;
if(database_type!=null){
if(database_type.toLowerCase().equals("mysql")){
jdbcDriver="com.mysql.jdbc.Driver";
strCon="jdbc:mysql://"+DBhost+":"+DBport+"/"+DBname;
}
else if(database_type.toLowerCase().equals("oracle")){
jdbcDriver="oracle.jdbc.driver.OracleDriver";
strCon="jdbc:oracle:thin:@"+DBhost+":"+DBport+":"+DBname;
}
else if(database_type.toLowerCase().equals("sqlserver")){
jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
strCon="jdbc:microsoft:sqlserver://"+DBhost+":"+DBport+";DatabaseName="+DBname;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static SqlManager createInstance(){
if(p==null)
{
p=new SqlManager();
p.initDB();
}
return p;
}
private String getString(String s)
{
return this.bundle.getString(s);
}
public void initDB(){
System.out.println(strCon);
System.out.println(jdbcDriver);
try{
Class.forName(jdbcDriver);
}catch(Exception ex){
System.err.println("Can't Find Database Driver.");
}
}
public void connectDB(){
try{
System.out.println("SqlManager:Connecting to database...");
Sqlconn=DriverManager.getConnection(strCon,DBuser,DBpassword);
Sqlstmt=Sqlconn.createStatement();
}catch(SQLException ex){
System.err.println("connectDB"+ex.getMessage());
}
System.out.println("SqlManager:Connect to database successful.");
}
public void closeDB(){
try{
System.out.println("SqlManager:Close connection to database...");
Sqlstmt.close();
Sqlconn.close();
}catch(SQLException ex){
System.err.println("closeDB:"+ex.getMessage());
}
System.out.println("Sqlmanager:Close connection successful.");
}
public int executeUpdate(String sql){
int ret=0;
try{
ret=Sqlstmt.executeUpdate(sql);
}catch(SQLException ex)
{
System.out.println("executeUpdate:"+ex.getMessage());
}
return ret;
}
public ResultSet executeQuery(String sql){
ResultSet rs=null;
try{
rs=Sqlstmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("executeQuery:"+ex.getMessage());
}
return rs;
}
public static void main(String args[]){
SqlManager.createInstance().connectDB();
SqlManager.createInstance().closeDB();
}
}
该系统主要实现了学生选课的功能,这个系统是我独立完成,从需求分析,界面的搭建,到数据库的连接,表格,存储过程和存储过程等的建立,在这段时间的摸索中,我确实学到了很多东西,特别是对以前不太了解的Java Swing组件有了更深刻的了解。比如JTable,对于它的用法我在网上找了很多资料,JTable的建立有各种不同的方法,可以使用DefaultTableModel类来实现,如DefaultTableModel dtm=new DefaultTableModel(new Object [] {"","课程编号","课程名称","学分","任课教师","教师职称","上课地点","以选人数"},0));然后再table.setModel(dtm); 或者继承AbstractTableModel类,还有对于如何在JTable中添加Swing组件,原本我是直接新建一个JcheckBox对象直接添加到表格的 单元格里,结果发现只能显示出一串字符串,上网查找后才知道,要用DefaultCellEditor来添加Swing组件,再设置setCellRenderer(new MyTableRenderer()) 这是一个实现了TableCellRenderer接口的JCheckBox。TableCellRenderer可以看做是Swing组件的呈现器,这样Table就会把内容显示绘制交给JCheckBox了。对于数据库,我尽量将对数据库的操作放在存储过程中,这样的抽象和封装使得源程序代码更加容易理解,而且在web应用系统中也可以避免发生不安全的状况,我想这是一个号的程序员应当要养成的习惯,在这次的课程设计中,层次化,模块化,抽象化也是我学到的一个重要的经验,参考一些资料后发现模块化能使程序设计更加简单,设计代码时目标更加明确,效率更高,以前虽然也知道这些道理,但自己真正实施起来却感到无从下手,比如前面的数据库操作和数据库资源配置文件,就是我从书中看来的,这样做的好处是,在程序中操作数据库的时候避免了使用很多try和catch语句,是代码更加简洁,更容易理解,此外需要连接不同的数据库时只要修改数据库的资源配置文件sysConfig.properties就可以了。原本我是想用jsp 做一个web应用程序的,因为对于学生选课系统做成单版的确实没什么实用性,但是我对jsp还不太熟悉,所以这次先做个单机版的,以后我会尝试用jsp来做这个系统。
public class AdmPanel extends JPanel implements ActionListener,ItemListener,MouseListener
{
JButton b1,b2,b3,b4,b5,b6;
JPanel p1,p2,pCenter;
CardLayout card=null;
JTextField t1=new JTextField(10),
t2=new JTextField(10),
t3=new JTextField(10),
tt1=new JTextField(10),
tt2=new JTextField(10),
tt3=new JTextField(10);
ButtonGroup sex1=new ButtonGroup(),
sex2=new ButtonGroup();
JRadioButton radio1=new JRadioButton("男",true),
radio2=new JRadioButton("女");
JRadioButton r1=new JRadioButton("男",true),
r2=new JRadioButton("女");
JComboBox year,month,date,yy,mm,dd,post;
JTable table1,table2;
Vector vectorColName1=new Vector(),
vectorColName2=new Vector();
Vector vector1=new Vector(),
vector2=new Vector();
DefaultTableModel model1=new DefaultTableModel(vectorColName1,0){
public boolean isCellEditable(int row, int column)
{
return false;
}
};
DefaultTableModel model2=new DefaultTableModel(vectorColName2,0){
public boolean isCellEditable(int row, int column)
{
return false;
}
};
AdmPanel()
{
setLayout(new BorderLayout());
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
tt1=new JTextField(10);
tt2=new JTextField(10);
tt3=new JTextField(10);
sex1.add(radio1);
sex1.add(radio2);
sex2.add(r1);
sex2.add(r2);
year=new JComboBox();
month=new JComboBox();
date=new JComboBox();
yy=new JComboBox();
mm=new JComboBox();
dd=new JComboBox();
post=new JComboBox();
post.addItem("助教");
post.addItem("讲师");
post.addItem("副教授");
post.addItem("教授");
for(int i=1980;i<1995;i++)
year.addItem(i);
for(int i=1;i<=12;i++)
month.addItem(i);
for(int i=1;i<=31;i++)
date.addItem(i);
for(int i=1950;i<1995;i++)
yy.addItem(i);
for(int i=1;i<=12;i++)
mm.addItem(i);
for(int i=1;i<=31;i++)
dd.addItem(i);
year.addItemListener(this);
month.addItemListener(this);
yy.addItemListener(this);
mm.addItemListener(this);
post.addItemListener(this);
b1=new JButton("学生信息管理");
b2=new JButton("教师信息管理");
b3=new JButton("输入");
b4=new JButton("删除");
b5=new JButton("输入");
b6=new JButton("删除");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
JPanel p0=new JPanel();
p0.add(b1);
p0.add(b2);
pCenter=new JPanel();
card=new CardLayout();
pCenter.setLayout(card);
p1=createStuPanel();
p2=createTeaPanel();
pCenter.add("学生信息管理",p1);
pCenter.add("教师信息管理",p2);
add(p0,BorderLayout.NORTH);
add(pCenter,BorderLayout.CENTER);
}
private JPanel createStuPanel()
{
JPanel p=new JPanel();
JScrollPane p1;
JPanel p2=new JPanel();
p.setLayout(new GridLayout(2,1));
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
String sql="exec ProcAllStu";
ResultSet rs=DBm.executeQuery(sql);
vectorColName1.addElement("学号");
vectorColName1.addElement("姓名");
vectorColName1.addElement("性别");
vectorColName1.addElement("生日");
vectorColName1.addElement("班级");
model1.setDataVector(vector1,vectorColName1);
table1=new JTable(model1);
table1.addMouseListener(this);
p1=new JScrollPane(table1);
p.add(p1);
try{
while(rs.next()){
Vector rec_vector=new Vector();//从结果集中取数据放入向量rec_vector中
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
rec_vector.addElement(rs.getString(4));
rec_vector.addElement(rs.getString(5));
vector1.addElement(rec_vector);//向量rec_vector加入向量vect中
}
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
DBm.closeDB();
//构造p2
Box base=Box.createHorizontalBox(),
boxleft=Box.createVerticalBox(),
boxright=Box.createVerticalBox();
JPanel pp1=new JPanel(),
pp2=new JPanel(),
pp3=new JPanel(),
pp4=new JPanel(),
pp5=new JPanel(),
pp6=new JPanel(),
psex=new JPanel(),
pbirth=new JPanel();
psex.add(radio1);
psex.add(radio2);
pbirth.add(year);
pbirth.add(month);
pbirth.add(date);
pp1.add(new JLabel("学生编号:"));
pp1.add(t1);
pp2.add(new JLabel("学生姓名:"));
pp2.add(t2);
pp3.add(new JLabel("学生性别:"));
pp3.add(psex);
pp4.add(new JLabel("学生生日:"));
pp4.add(pbirth);
pp5.add(new JLabel("所在班级:"));
pp5.add(t3);
pp6.add(b3);
pp6.add(b4);
boxleft.add(Box.createVerticalStrut(30));
boxleft.add(pp1);
boxleft.add(Box.createVerticalStrut(20));
boxleft.add(pp2);
boxleft.add(Box.createVerticalStrut(20));
boxleft.add(pp3);
boxright.add(Box.createVerticalStrut(20));
boxright.add(pp4);
boxright.add(Box.createVerticalStrut(20));
boxright.add(pp5);
boxright.add(Box.createVerticalStrut(20));
boxright.add(pp6);
base.add(boxleft);
base.add(Box.createHorizontalStrut(80));
base.add(boxright);
p2.add(base);
p.add(p2);
return p;
}
private JPanel createTeaPanel()
{
JPanel p=new JPanel();
JScrollPane p1;
JPanel p2=new JPanel();
p.setLayout(new GridLayout(2,1));
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
String sql="exec ProcAllTea";
ResultSet rs=DBm.executeQuery(sql);
vectorColName2.addElement("教师编号");
vectorColName2.addElement("教师姓名");
vectorColName2.addElement("教师性别");
vectorColName2.addElement("生日");
vectorColName2.addElement("职称");
vectorColName2.addElement("所在院系");
model2.setDataVector(vector2,vectorColName2);
table2=new JTable(model2);
table2.addMouseListener(this);
p1=new JScrollPane(table2);
p.add(p1);
try{
while(rs.next()){
Vector rec_vector=new Vector();//从结果集中取数据放入向量rec_vector中
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
rec_vector.addElement(rs.getString(4));
rec_vector.addElement(rs.getString(5));
rec_vector.addElement(rs.getString(6));
vector2.addElement(rec_vector);//向量rec_vector加入向量vect中
}
//tm.fireTableStructureChanged();//更新表格,显示向量vect的内容
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
DBm.closeDB();
//构造p2
Box base=Box.createHorizontalBox(),
boxleft=Box.createVerticalBox(),
boxright=Box.createVerticalBox();
JPanel pp1=new JPanel(),
pp2=new JPanel(),
pp3=new JPanel(),
pp4=new JPanel(),
pp5=new JPanel(),
pp6=new JPanel(),
pp7=new JPanel(),
psex=new JPanel(),
pbirth=new JPanel();
psex.add(r1);
psex.add(r2);
pbirth.add(yy);
pbirth.add(mm);
pbirth.add(dd);
pp1.add(new JLabel("教职工号:"));
pp1.add(tt1);
pp2.add(new JLabel("教师姓名:"));
pp2.add(tt2);
pp3.add(new JLabel("教师性别:"));
pp3.add(psex);
pp4.add(new JLabel("教师生日:"));
pp4.add(pbirth);
pp5.add(new JLabel("教师职称:"));
pp5.add(post);
pp6.add(new JLabel("所在院系:"));
pp6.add(tt3);
pp7.add(b5);
pp7.add(b6);
boxleft.add(Box.createVerticalStrut(40));
boxleft.add(pp1);
boxleft.add(Box.createVerticalStrut(20));
boxleft.add(pp2);
boxleft.add(Box.createVerticalStrut(20));
boxleft.add(pp3);
boxright.add(Box.createVerticalStrut(30));
boxright.add(pp4);
boxright.add(Box.createVerticalStrut(10));
boxright.add(pp5);
boxright.add(Box.createVerticalStrut(10));
boxright.add(pp6);
boxright.add(pp7);
base.add(boxleft);
base.add(Box.createHorizontalStrut(80));
base.add(boxright);
p2.add(base);
p.add(p2);
return p;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
card.show(pCenter, "学生信息管理");
}
else if(e.getSource()==b2)
{
card.show(pCenter, "教师信息管理");
}
if(e.getSource()==b3)
{
String userid=t1.getText(),
username=t2.getText(),
classs=t3.getText();
String sex="男";
if(radio2.isSelected())
{
sex="女";
}
String birth=String.valueOf((Integer)year.getSelectedItem())+"-"
+String.valueOf((Integer)month.getSelectedItem())
+"-"+String.valueOf((Integer)date.getSelectedItem());
String sql="exec InsertStudent'"+userid+"','"+username+"','"+sex+"','"+birth+"','"+classs+"'";
String s="exec IsExistsStu'"+userid+"'";
String inserlog="exec InsertLogon'"+userid+"'";
System.out.println(sql);
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
ResultSet rset=DBm.executeQuery(s);
System.out.println(s);
try{
if (rset.next())
{
JOptionPane.showMessageDialog(this,"学生信息插入失败,该学生ID号已存在",
"警告",JOptionPane.WARNING_MESSAGE);
rset.close();
}
else{
ResultSet rs=DBm.executeQuery(sql);
if(rs.next())
{
JOptionPane.showMessageDialog(this,"学生信息插入成功",
"成功",JOptionPane.WARNING_MESSAGE);
DBm.connectDB();
DBm.executeUpdate(inserlog);
t1.setText("S");
t2.setText(null);
t3.setText(null);
radio1.setSelected(true);
//this.post.setSelectedIndex(0);
year.setSelectedIndex(0);
month.setSelectedIndex(0);
date.setSelectedIndex(0);
//sql="select * from Inserted";
//ResultSet rs=DBm.executeQuery(sql);
Vector rec_vector=new Vector();//从结果集中取数据放入向量rec_vector中
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
rec_vector.addElement(rs.getString(4));
rec_vector.addElement(rs.getString(5));
vector1.addElement(rec_vector);//向量rec_vector加入向量vect中
model1.fireTableStructureChanged();//更新表格,显示向量vect的内容
rs.close();
}
else{
JOptionPane.showMessageDialog(this,"学生信息插入失败",
"警告",JOptionPane.WARNING_MESSAGE);
rs.close();
}
}
}catch(SQLException ex){
ex.printStackTrace();
}
DBm.closeDB();
}
if(e.getSource()==b4)
{
//同步删除数据库中的数据
int selectrow = 0;
selectrow =table1.getSelectedRow();
String id =table1.getValueAt(selectrow,0).toString();
String sql="exec DeleteStudent'"+id+"'";
String deletelog="exec DeleteLogon'"+id+"'";
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
if(DBm.executeUpdate(sql)==1)
{
JOptionPane.showMessageDialog(this,"学生信息已删除",
"删除成功",JOptionPane.INFORMATION_MESSAGE);
DBm.connectDB();
DBm.executeUpdate(deletelog);
model1.removeRow(selectrow);
t1.setText("S");
t2.setText(null);
t3.setText(null);
radio1.setSelected(true);
year.setSelectedIndex(0);
month.setSelectedIndex(0);
date.setSelectedIndex(0);
}
DBm.closeDB();
}
if(e.getSource()==b5)
{
String userid=tt1.getText(),
username=tt2.getText(),
depart=tt3.getText(),
post=(String)this.post.getSelectedItem();
String sex="男";
if(r2.isSelected())
{
sex="女";
}
String birth=String.valueOf((Integer)yy.getSelectedItem())+"-"+String.valueOf((Integer)mm.getSelectedItem())
+"-"+String.valueOf((Integer)dd.getSelectedItem());
String sql="exec InsertTeacher'"+userid+"','"+username+"','"+sex+"','"+birth+"','"+post+"','"+depart+"'";
String s="exec IsExistsTea'"+userid+"'";
String insertlog="exec InsertLogon'"+userid+"'";
System.out.println(sql);
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
ResultSet rset=DBm.executeQuery(s);
System.out.println(s);
try{
if (rset.next())
{
JOptionPane.showMessageDialog(this,"教师信息插入失败,该教师ID号已存在",
"警告",JOptionPane.WARNING_MESSAGE);
rset.close();
}
else{
ResultSet rs=DBm.executeQuery(sql);
if(rs.next())
{
JOptionPane.showMessageDialog(this,"教师信息插入成功",
"成功",JOptionPane.WARNING_MESSAGE);
DBm.connectDB();
DBm.executeUpdate(insertlog);
tt1.setText("T");
tt2.setText(null);
tt3.setText(null);
r1.setSelected(true);
this.post.setSelectedIndex(0);
yy.setSelectedIndex(0);
mm.setSelectedIndex(0);
dd.setSelectedItem(0);
Vector rec_vector=new Vector();//从结果集中取数据放入向量rec_vector中
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
rec_vector.addElement(rs.getString(4));
rec_vector.addElement(rs.getString(5));
rec_vector.addElement(rs.getString(6));
vector2.addElement(rec_vector);//向量rec_vector加入向量vect中
model2.fireTableStructureChanged();//更新表格,显示向量vect的内容
rs.close();
}
else{
JOptionPane.showMessageDialog(this,"教师信息插入失败",
"警告",JOptionPane.WARNING_MESSAGE);
rs.close();
}
}
}catch(SQLException ex){
ex.printStackTrace();
}
DBm.closeDB();
}
if(e.getSource()==b6)
{
//同步删除数据库中的数据
int selectrow = 0;
selectrow =table2.getSelectedRow();
String id =table2.getValueAt(selectrow,0).toString();
String sql="exec DeleteTeacher'"+id+"'";
String deletelog="exec DeleteLogon'"+id+"'";
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
if(DBm.executeUpdate(sql)==1)
{
JOptionPane.showMessageDialog(this,"教师信息信息已删除",
"删除成功",JOptionPane.INFORMATION_MESSAGE);
DBm.connectDB();
DBm.executeUpdate(deletelog);
model2.removeRow(selectrow);
tt1.setText("T");
tt2.setText(null);
tt3.setText(null);
r1.setSelected(true);
yy.setSelectedIndex(0);
mm.setSelectedIndex(0);
dd.setSelectedIndex(0);
}
DBm.closeDB();
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDraged(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==table1)
{
int selectrow = 0;
selectrow =table1.getSelectedRow();
String id =table1.getValueAt(selectrow,0).toString();
String name=table1.getValueAt(selectrow,1).toString();
String sex=table1.getValueAt(selectrow,2).toString();
String birth=table1.getValueAt(selectrow,3).toString().substring(0,10);
String classs=table1.getValueAt(selectrow,4).toString();
t1.setText(id);
t2.setText(name);
t3.setText(classs);
if(sex.trim().equals("男"))
radio1.setSelected(true);
else if(sex.equals("女"))
radio2.setSelected(true);
int y=Integer.parseInt(birth.substring(0, 4));
int m=Integer.parseInt(birth.substring(5, 7));
int d=Integer.parseInt(birth.substring(8));
year.setSelectedIndex(y-1980);
month.setSelectedIndex(m-1);
date.setSelectedIndex(d-1);
}
else if(e.getSource()==table2)
{
int selectrow = 0;
selectrow =table2.getSelectedRow();
String id =table2.getValueAt(selectrow,0).toString();
String name=table2.getValueAt(selectrow,1).toString();
String sex=table2.getValueAt(selectrow,2).toString();
String birth=table2.getValueAt(selectrow,3).toString().substring(0,10);
String position=table2.getValueAt(selectrow,4).toString();
String depart=table2.getValueAt(selectrow,5).toString();
tt1.setText(id);
tt2.setText(name);
tt3.setText(depart);
if(sex.trim().equals("男"))
r1.setSelected(true);
else if(sex.equals("女"))
r2.setSelected(true);
int y=Integer.parseInt(birth.substring(0, 4));
int m=Integer.parseInt(birth.substring(5, 7));
int d=Integer.parseInt(birth.substring(8));
yy.setSelectedIndex(y-1950);
mm.setSelectedIndex(m-1);
dd.setSelectedIndex(d-1);
if(position.equals("助教"))
post.setSelectedIndex(0);
else if(position.equals("讲师"))
post.setSelectedIndex(1);
else if(position.equals("副教授"))
post.setSelectedIndex(2);
else if(position.equals("教授"))
post.setSelectedIndex(3);
}
}
public void itemStateChanged(ItemEvent e)
{//主要实现生日日期的选择(略)
}
public class CourseList extends JPanel implements ActionListener
{
int count=0;//可供选择的课程数
MyTable table1;
JTable table2;
TableColumnModel tcm;
JButton button;
CourseList()
{
setLayout(new BorderLayout());
button=new JButton("确定");
button.addActionListener(this);
JPanel p=new JPanel();
p.add(button);
table1=new MyTable();
JTable table2=initTable(table1);
JScrollPane sp=new JScrollPane(table2);
add(sp,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
}
private JTable initTable(JTable table) {
DefaultTableModel dtm = new DefaultTableModel(
new Object [] {"","课程编号","课程名称","学分","任课教师","教师职称","上课地点","以选人数"},0);
SqlManager DBm=SqlManager.createInstance();
DBm.connectDB();
String sql="exec AllCourse";//所有选修课,如果选课人数没有达到5时设置MyTable.b=true;
ResultSet rs=DBm.executeQuery(sql);
try{
while(rs.next())
{
dtm.addRow(new Object[] {new Boolean(false),rs.getString(1),rs.getString(2),
rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7)});
count++;
}
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
DBm.closeDB();
table.setModel(dtm);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(0).setCellRenderer(new MyTableRenderer());
tcm.getColumn(0).setPreferredWidth(20);
tcm.getColumn(0).setWidth(20);
tcm.getColumn(0).setMaxWidth(20);
return table;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
if(selectcourse()>0)
{
JOptionPane.showMessageDialog(this,"选课成功\n点击\"确定\"查看以选课程",
"选课成功",JOptionPane.INFORMATION_MESSAGE);
ChooseList.updateTable();
StuPanel.card.show(StuPanel.pCenter, "已选课程");
}
}
}
public int selectcourse()
{
int selectedCount=ChooseList.getSelectedCount();
int a=xianzhi();
int c=0;
String[] courseID=new String[selectedCount];
if(selectedCount==3)
{
JOptionPane.showMessageDialog(this,"选课数目不能超过3门,你已经选择了3门\n点击\"确定\"查看以选课程",
"警告",JOptionPane.WARNING_MESSAGE);
ChooseList.updateTable();
StuPanel.card.show(StuPanel.pCenter, "已选课程");
return c ;
}
else{
if(a+selectedCount>3)
{
JOptionPane.showMessageDialog(this,"选课数目不能超过3门,你已经选择了"+selectedCount+"门\n点击\"确定\"查看以选课程",
"警告",JOptionPane.WARNING_MESSAGE);
ChooseList.updateTable();
StuPanel.card.show(StuPanel.pCenter, "已选课程");
return c;
}
else
{//不能选重复了
String sql="exec SelectedCourse'"+Logon.userid+"'";
//System.out.println(sql+"不能选重复了");
SqlManager DBm=SqlManager.createInstance();
DBm.connectDB();
ResultSet rs=DBm.executeQuery(sql);
try{
int i=0;
while(rs.next()){
courseID[i]=rs.getString(1).trim();
System.out.println(courseID[i]+"changdu");
}
rs.close();
}catch(SQLException e){
e.printStackTrace();
return c;
}
DBm.closeDB();
for(int i=0;i0)return false;
else return true;
}
}
class MyTableRenderer extends JCheckBox implements TableCellRenderer
{
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 Logon extends JPanel
{
JTextField t1;
JPasswordField t2;
JButton button1,button2;
static String info[];
static String userid,password;
Logon()
{
JLabel label=new JLabel("登陆");
label.setFont(new Font("TimesRoman",Font.BOLD,24));
t1=new JTextField(20);
t2=new JPasswordField(20);
button1=new JButton("登陆");
button2=new JButton("取消");
JPanel p1,p2,p3,p4;
Box box=Box.createVerticalBox();
p1=new JPanel();//p1.setBackground(Color.PINK);
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p1.add(label);
p2.add(new Label("用户名:"));
p2.add(t1);
p3.add(new Label("口 令:"));
p3.add(t2);
p4.add(button1);
p4.add(button2);
box.add(p1);
box.add(p2);
box.add(p3);
box.add(p4);
add(box);
}
public char logon() throws SQLException
{
String tuserid=t1.getText();
String tpassword=new String(t2.getPassword());
String sql1=null,sql2=null;
ResultSet rs=null;
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
sql1="exec ProcLogon'"+tuserid+"','"+tpassword+"'";
rs=DBm.executeQuery(sql1);
if(rs.next())//如果存储过程有结果返回,则将用户信息读入并保存
{
System.out.println(rs.getString(1)+"如果存储过程有结果返回,则将用户信息读入并保存");
userid=rs.getString(1);
password=rs.getString(2);
if(userid.startsWith("T"))
{
TeaInfo.saveTea(userid);
}
else if(userid.startsWith("S"))
{
StuInfo.saveStu(userid);
}
else if(userid.startsWith("A"))
{
sql2=sql1;
DBm.connectDB();
rs=DBm.executeQuery(sql2);
}
rs.close();
DBm.closeDB();
return userid.substring(0, 1).toCharArray()[0];
}
else{
userid=null;
password=null;
JOptionPane.showMessageDialog(this,"用户名或密码错误,请重新填写",
"警告",JOptionPane.WARNING_MESSAGE);
rs.close();
DBm.closeDB();
return '0';
}
}
private static void dispResultSet(ResultSet rs) throws SQLException
{
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
info=new String[numCols];
while(rs.next())
{
for(int i=1;i<=numCols;i++)
{
info[i-1]=rs.getString(i);
if(i>1)System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println("");
}
}
public void reset()
{
t1.setText(null);
t2.setText(null);
t1.requestFocusInWindow();
}
}