package stuManger; import java.sql.ResultSet; //课程类 public class course { //课程属性课程号,课程名,学分,开课学期,任课教师 private String kch,kcm,rkjs; private int xf,kkxq; public String sqlStr; public ResultSet rs = null; public course(){ this.kch = ""; this.kcm = ""; this.rkjs = ""; this.xf = 0; this.kkxq = 0; } public String getKch() { return kch; } public void setKch(String kch) { this.kch = kch; } public String getKcm() { return kcm; } public void setKcm(String kcm) { this.kcm = kcm; } public String getRkjs() { return rkjs; } public void setRkjs(String rkjs) { this.rkjs = rkjs;
} public int getXf() { return xf; } public void setXf(int xf) { this.xf = xf; } public int getKkxq() { return kkxq; } public void setKkxq(int kkxq) { this.kkxq = kkxq; } // 查询
课程信息 public ResultSet search() { try { dataBase db = new dataBase(); if (this.getKch() == null || this.getKch().equals("")) { this.sqlStr = "select *from course"; this.rs = db.stmt.executeQuery(sqlStr); } else { this.sqlStr = "select *from course where kch='" + this.getKch() + "'"; this.rs = db.stmt.executeQuery(sqlStr); } return rs; } catch (Exception e) { System.out.println(e); return null; } } // 添加课程信息 public boolean add_course() { boolean flag = false; try { if (this.getKch() == null || this.getKch().equals(""))
flag = false; else { dataBase db = new dataBase(); this.sqlStr = "insert into course values('" + this.getKch() + "','" + this.getKcm() + "','" + this.getXf() + "','" + this.getKkxq() + "','" + this.getRkjs() + "'"; db.stmt.executeUpdate(sqlStr); flag = true; } return flag; } catch (Exception e) { System.out.println(e); return false; } } // 修改课程信息 public boolean modify() { boolean flag = false; try { if (this.getKch() == null || this.getKch().equals("")) flag = false; else { dataBase db = new dataBase(); this.sqlStr = "select *from course where kch='" + this.getKch() + "'"; rs = db.stmt.executeQuery(sqlStr); if (rs.next()) { flag = true; this.sqlStr = "update course set kch='" + this.getKch() + "',kcm='" + this.getKcm() + "',xf='" + this.getXf() + "',kkxq='" + this.getKkxq() + "',rkjs='" + this.getRkjs() + "'"; db.stmt.executeUpdate(sqlStr); } } return flag; } catch (Exception e) { System.out.println(e); return false; } }
// 删除课程信息 public boolean delete() { boolean flag = false; try { if (this.getKch() == null || this.getKch().equals("")) flag = false; else { dataBase db = new dataBase(); this.sqlStr = "select *from course where kch='" + this.getKch() + "'"; rs = db.stmt.executeQuery(sqlStr); if (rs.next()) { flag = true; this.sqlStr = "delete course where kch='" + this.getKch() + "'"; db.stmt.executeUpdate(sqlStr); } } return flag; } catch (Exception e) { System.out.println(e); return false; } } }
package stuManger; import java.sql.*; //连接数据库的类 public class dataBase { public Connection conn; public Statement stmt; public ResultSet rs; public dataBase(){ this.connect(); }
public bool
ean connect() { try { String DBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(DBDriver); String url "jdbc:sqlserver://localhost:1433;DatabaseName=xuexi;user=sa;password=system"; conn = DriverManager.getConnection(url); stmt = conn.createStatement(); return true; } catch (Exception e) { System.out.println("数据库连接失败"); e.printStackTrace(); return false; } } }
=
package stuManger; import java.sq