NetBeansIDE7.4 JTable学生选课退课

这篇博客详细介绍了如何使用NetBeans IDE7.4结合JTable组件连接Oracle数据库,展示course表,并实现学生选课及退课的功能。涉及到的代码分别位于OracleUtils.class(数据库连接)、Course.class(选课退课逻辑)和Ssdcourse.class(界面展示)。博客还展示了相关数据表结构以及登录界面的设计。
摘要由CSDN通过智能技术生成

NetBeansIDE7.4 JTable 连接数据库,显示course表,实现简单选课退课功能

连接数据库的代码(在utils包中的OracleUtils.class文件):

package utils;
import java.sql.Connection;
import java.sql.DriverManager;
public class OracleUtils {
     static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
    static final String USERNAME = "lanqiao_student";
    static final String PASSWORD = "tiger";
    static Connection conn = null;

    public static Connection getConnection()throws Exception{
        Class.forName("oracle.jdbc.OracleDriver");
        conn = DriverManager.getConnection(URL,USERNAME, PASSWORD);
        return conn;
    }

}

实现选课退课功能的代码(在entity包的Course.class文件):

package entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import utils.OracleUtils;
public class Course {
         //显示course表
        public void fillCourseJtable(JTable table,String valueToSearch){
            System.out.println("fillcoursejtable --start");
            
        try {
            //查找course的数据
            String sql = "select * from course";
            Connection conn = OracleUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            DefaultTableModel model = (DefaultTableModel)table.getModel();
            //如果JTable中已经有数据,清除
            while (model.getRowCount()>0){
                      model.removeRow(0);  
             }
            //插入数据
            Object[] row;
            while(rs.next()){
             row = new Object[4];
             row[0]=rs.getString(1);
             row[1]=rs.getString(2);
             row[2]=rs.getString(3);
             row[3]=rs.getString(4);
             model.addRow(row);
            }
        } catch (Exception ex) {
            System.out.println("显示course表失败!");
        }
    }
        //选课
          public void selectCourseJtable(JTable table,int xh,JLabel label){
             System.out.println("selectcoursejtable-----start");
             //得到选中的行数的值并加1变成选择课程的编号
            int i = table.getSelectedRow();
            int c=i+1;
            System.out.println("选中行数加一:" + c);
                        
            try {
                //连接数据库
                Connection conn;
                conn = OracleUtils.getConnection();
                //得到当前选中课程的已选人数
                PreparedStatement ps1 = conn.prepareStatement("select chosecount from course where cno='"+ c +"'");
                ResultSet rs1 = ps1.executeQuery("select chosecount from course where cno='"+ c +"'");
                int psdc = 1000;
                while(rs1.next()) {
                    psdc = rs1.getInt(1);
                }
                System.out.println("************"+ psdc);
                //得到当前选中课程的最大可选
                PreparedStatement ps2 = conn.prepareStatement("select maxcount from course where cno='"+ c+"'");
                ResultSet rs2 = ps2.executeQuery("select maxcount from course where cno='"+ c+"'");
                int psdm = 1000;
                while(rs2.next()) {
                    psdm = rs2.getInt(1);
                }
                System.out.println("mmmmmmmmmmm"+ psdm);
                //判断已选人数是否达到最大人数
                if(psdc<psdm){
                    //得到当前用户本门课程的成绩
                     PreparedStatement ps3 = conn.prepareStatement("select stugrade from stu_c where cno='"+ c+"' and stuno = '"+ xh+"'");
                     ResultSet rs3 = ps3.executeQuery("select stugrade from stu_c where cno='"+ c+"' and stuno = '"+ xh+"'");
                     int psdg = 1000;
                     while(rs3.next()) {
                        psdg = rs3.getInt(1);
                     }
                    if(psdg==1000){
                        //没有当前用户本门课的成绩,用户没有选过本门课,进行选课
                       //修改course表中chosecount的值
                     PreparedStatement ps4 = conn.prepareStatement("update course set chosecount= chosecount + 1 where cno = ' "+ c + "'");
                     int result4 = ps4.executeUpdate("update course set chosecount= chosecount + 1 where cno = ' "+ c + "'");
                     System.out.print("course表有"+ result4 +"行数据被修改");
                     //同时修改stu_c表中的数据
                      PreparedStatement ps5 = conn.prepareStatement("insert into stu_c(stuno,cno,stugrade) values('"+ xh +" ','"+ c +"',0) ");
                      int result5 = ps5.executeUpdate("insert into stu_c(stuno,cno,stugrade) values('"+ xh +" ','"+ c +"',0) ");
                      System.out.print("stu_c表有"+ result5 +"行数据被增加");
                      label.setText("选课成功!"); 
                      System.out.println("选课成功!");
                    }else{
                        label.setText("选课失败!已经选过此课程!"); 
                        System.out.println("选课失败!已经选过此课程!");
                    }
                     
                }else{
                    label.setText("选课失败!已达到最大可选人数!"); 
                    System.out.println("选课失败!已达到最大可选人数!");
                }
                
            } catch (Exception ex) {
                label.setText("选课失败!"); 
                System.out.println("选课失败!");
            }    
    }
         //退课 
        public void dropCourseJtable(JTable table,int xh,JLabel label){
            System.out.println("dropcoursejtable------start");
            //得到选中的行数的值并加1变成选择课程的编号
            int c = table.getSelectedRow() + 1;
            System.out.println("选中行数加一:" + c);
            try {         
                //连接数据库
                Connection conn = OracleUtils.getConnection();
                //得到当前选中课程的成绩
                PreparedStatement ps1 = conn.prepareStatement("select stugrade from stu_c where cno='"+ c+"' and stuno = '"+ xh+"'");
                ResultSet rs1 = ps1.executeQuery();
                int psdg = 1000;
                while(rs1.next()) {
                    psdg = rs1.getInt(1);
                }
                if(psdg==1000){
                    label.setText("退课失败!没有选过此课程!");                    
                }else{
                    //当前用户选过本课程,开始退课
                    //course表中本课程已选人数减一
                    PreparedStatement ps2 = conn.prepareStatement("up
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值