CourseTable许可之后可以更新显示出来,删除的话显示不出来
月见寒潭 2015-03-17 10:52:15 CourseTable.java
public class CourseTable extends JPanel
{ private ResultSet rs;
private String stu_id;//声明标志学生学号的引用
private String[] s_head={"讲次","星期一","星期二","星期三",
"星期四","星期五","星期六","星期日"};//初始化表头
private String[][] s_data=new String[5][8];//创建存放课程信息的二维数组
private JTable jt;//声明表格引用
private JScrollPane jsp;
public CourseTable(String stu_id,String host)
{
this.host=host;
this.stu_id=stu_id;
this.initialData();//初始化数据
this.initialTable();//初始化表格
this.initialFrame();//初始化窗体
}
public void initialData()
{//初始化表格数据
//初始化第一列数据
s_data[0][0]="第一讲";s_data[1][0]="第二讲";s_data[2][0]="第三讲";
s_data[3][0]="第四讲";s_data[4][0]="第五讲";
try{//查询数据库,初始化数据
String sql="select cou_name,teacher,cou_day,cou_time from course,courseinfo,"+
"grade where grade.stu_id='"+stu_id+"' and grade.cou_id=course.cou_id and "+
"grade.cou_id=courseinfo.cou_id and grade.isdual=0";
this.initialConnection();
rs=stmt.executeQuery(sql);
while(rs.next()){
String cou_name=new String(rs.getString(1).getBytes("gb2312"));
String teacher=new String(rs.getString(2).getBytes("gb2312"));
int cou_day=Integer.parseInt(rs.getString(3));
int cou_time=Integer.parseInt(rs.getString(4));
s_data[cou_time-1][cou_day]=cou_name+"\n(教师:"+teacher+")";
}
}
catch(Exception e){e.printStackTrace();}
}
public void updataview()//更新表格模型
{
((DefaultTableModel)jt.getModel()).setDataVector(s_data,s_head);
((DefaultTableModel)jt.getModel()).fireTableStructureChanged();
}
//自定义的初始化数据库连接的方法
//关闭连接的方法
//自定义的表格绘制器
class TableViewRenderer extends JTextArea implements TableCellRenderer
{
public TableViewRenderer()
{
//将表格设为自动换行
setLineWrap(true);
}
public Component getTableCellRendererComponent(JTable jtable, Object obj,
boolean isSelected, boolean hasFocus, int row, int column)
{
setText(obj == null ? "" : obj.toString());
return this;
}
}
}
ChoseTable.java
public class ChoseCourse extends JPanel implements ActionListener
{
public void initialData()//自定义的初始化数据的方法
{ //初始化表头
v_head.add("课程号");v_head.add("课程名");v_head.add("星期几");v_head.add("第几讲");
v_head.add("学分");v_head.add("老师");v_head.add("所属专业");v_head.add("开课学院");
try{//初始化表格数据
this.initialConnection();
String sql="select courseinfo.cou_id,course.cou_name,courseinfo.cou_day,"+
"courseinfo.cou_time,course.xuefen,courseinfo.teacher,dept.dept_name,"+
"college.coll_name from course,courseinfo,dept,college where "+
"courseinfo.cou_id=course.cou_id and course.dept_id=dept.dept_id and "+
"course.coll_id=college.coll_id and courseinfo.onchosing='1'";
rs=stmt.executeQuery(sql);
while(rs.next()){
Vector v=new Vector();
String cou_id=rs.getString(1);
v_couid.add(cou_id);
String cou_name=new String(rs.getString(2).getBytes("gb2312"));
String cou_day=rs.getString(3);
String cou_time=rs.getString(4);
String xuefen=rs.getDouble(5)+"";
String teacher=new String(rs.getString(6).getBytes("gb2312"));
String dept_name=new String(rs.getString(7).getBytes("gb2312"));
String coll_name=new String(rs.getString(8).getBytes("gb2312"));
v.add(cou_id);v.add(cou_name);v.add(cou_day);v.add(cou_time);
v.add(xuefen);v.add(teacher);v.add(dept_name);v.add(coll_name);
v_data.add(v);
}
rs.close();
}
catch(Exception e){e.printStackTrace();}
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb||e.getSource()==jtf)
{//按下提交按钮的处理代码
String cou_id=jtf.getText().trim();//获取输入的课程号
{//开始添加课程
String sql="insert into grade(stu_id,cou_id) values"+
"('"+stu_id+"','"+cou_id+"')";
int i=stmt.executeUpdate(sql);
if(i==1)
{//添加成功
}
else
{//添加失败
}
}
}
rs.close();
}
}
}
//自定义的初始化数据库连接的方法
//关闭数据库连接的方法
DeleteCourse.java
public class DeleteCourse extends JPanel implements ActionListener{
public void initialData()//自定义的初始化数据的方法
{ //初始化表头
v_head.add("课程号");v_head.add("课程名");v_head.add("星期几");v_head.add("第几讲");
v_head.add("学分");v_head.add("老师");
try{//初始化表格数据
this.initialConnection();
String sql="select course.cou_id,cou_name,cou_day,cou_time,course.xuefen,courseinfo.teacher "+
"from course,courseinfo,grade where grade.stu_id='"+stu_id+"'" +
"and grade.cou_id=course.cou_id and "+
"grade.cou_id=courseinfo.cou_id and grade.isdual=0";
rs=stmt.executeQuery(sql);
while(rs.next()){
Vector v=new Vector();
String cou_id=rs.getString(1);
v_couid.add(cou_id);
String cou_name=new String(rs.getString(2).getBytes("gb2312"));
String cou_day=rs.getString(3);
String cou_time=rs.getString(4);
String xuefen=rs.getDouble(5)+"";
String teacher=new String(rs.getString(6).getBytes("gb2312"));
v.add(cou_id);v.add(cou_name);v.add(cou_day);v.add(cou_time);
v.add(xuefen);v.add(teacher);
v_data.add(v);
}
rs.close();
}
catch(Exception e){e.printStackTrace();}
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb||e.getSource()==jtf)
{//按下提交按钮的处理代码
String cou_id=jtf.getText().trim();//获取输入的课程号
try
{//开始添加课程
String sql="delete from grade where stu_id='"+stu_id+"' "+
"and cou_id='"+cou_id+"' ";
int i=stmt.executeUpdate(sql);
if(i==1)
{//添加成功
}
else
{//添加失败
}
rs.close();
}
catch(Exception ea){ea.printStackTrace();}
}
}
//自定义的初始化数据库连接的方法
//关闭连接的方法
上述方法的调用
else if(id.equals("31"))
{
cl.show(jpy,"chosecourse");
}
else if(id.equals("32"))
{
//在显示之后立即更新数据
cl.show(jpy,"coursetable");
coursetable.initialData();
coursetable.updataview();
}
else if(id.equals("33"))
{
cl.show(jpy,"deletecourse");
}