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