资源描述
package org、hxl、oracle、demo03;
import java、sql、Connection;
import java、sql、DriverManager;
import java、sql、PreparedStatement;
import java、sql、ResultSet;
import java、util、ArrayList;
import java、util、Iterator;
import java、util、List;
import java、util、Scanner;
/*
* 课程信息管理系统,要求:
* 1、 课程信息包括:Cno(课程号) ,Cname(课程名),period(课时数),term(上课学期),
* ﻩproperty(课程性质)
* 2、包括课程得增加,删除,修改
* 查询(查询全部得课程信息,按关键字查询课程信息,按课程号查询课程信息)
* 3、将所有得记录保存在oracle数据库中
* */
//vo类(与数据库中得一张表相对应,一个对象对应表中得一条记录,一个属性对应表中得一个字段)
class Course implements parable<Course〉{
private int cno ; //课程号
ﻩprivate String cname ; //课程名
ﻩprivate int period ; //课时数
private String term ; //上课学期
private String property ; //课程性质
public Course(){
ﻩ//无参构造函数
}
ﻩpublic Course(int cno,String cname,int period,String term,String property){
ﻩthiso = cno ;
ﻩ thisame = cname ;
ﻩthis、term = term ;
ﻩﻩthis、period = period ;
ﻩ this、property = property ;
ﻩ}
public void setCno(int cno){
thiso = cno ;
}
public void setCname(String cname){
ﻩﻩthisame = cname ;
}
public void setPeriod(int period){
this、period = period ;
}
ﻩpublic void setTerm(String term){
ﻩ this、term = term ;
}
public void setProperty(String property){
this、property = property ;
ﻩ}
ﻩpublic int getCno(){
ﻩﻩreturn thiso ;
}
public String getCname(){
return thisame ;
}
ﻩpublic int getPeriod(){
ﻩﻩreturn this、period ;
}
ﻩpublic String getTerm(){
ﻩﻩreturn this、term ;
}
public String getProperty(){
return this、property ;
ﻩ}
ﻩOverride
ﻩpublic String toString(){
ﻩﻩreturn "课程号:" + this、getCno() + ”,课程名:" + this、getCname() + ",课程得学时:” + this、getPeriod()
ﻩ+”,上课学期:" + this、getTerm() + ",课程性质:" + this、getProperty();
}
ﻩOverride
public int pareTo(Course course) {
ﻩif(thiso>courseo){
ﻩreturn -1 ;
ﻩ}else if(thiso<courseo){
return 1 ;
ﻩﻩ}else{
ﻩ return 0 ;
ﻩﻩ}
}
ﻩOverride
ﻩpublic int hashCode(){
ﻩﻩreturn thiso * super、hashCode() ;
}
ﻩOverride
public boolean equals(Object obj){
ﻩif(obj==null){
ﻩreturn false ;
}
if(this == obj){
return true ;
ﻩ }
ﻩ if(!(obj instanceof Course)){
ﻩ ﻩreturn false ;
ﻩ}
ﻩ Course course = (Course)obj ;
if(thiso==courseo){
ﻩ //课程号相同,则一定相同,课程号就是唯一标识符
ﻩ return true ;
ﻩﻩ}else{
ﻩreturn false ;
ﻩ }
ﻩ}
}
//DatebaseConnection数据库连接与关闭
class DatebaseConnection{
ﻩ//数据库驱动程序
ﻩprivate final static String DBDRIVER = ”oracle、jdbc、driver、OracleDriver” ;
ﻩ//数据库得连接地址
private final static String DBURL = "jdbc:oracle:thin:localhost:1521:MLDN" ;
ﻩ//数据库得用户名
ﻩprivate final static String DBUER = "scott" ;
//数据库得密码
ﻩprivate final static String DBPASS = ”tiger” ;
ﻩ//数据库得连接对象
ﻩprivate Connection connection ;
ﻩpublic DatebaseConnection() throws Exception{
ﻩ try{
ﻩﻩ //1、加载驱动程序
ﻩ Class、forName(DBDRIVER) ;
ﻩ ﻩ//2、建立数据库连接
this、connection = DriverManager、getConnection(DBURL, DBUER, DBPASS) ;
}catch(Exception e){
ﻩﻩthrow e ; //将异常交给被调用处处理
ﻩﻩ}
}
public Connection getConnection(){
ﻩﻩreturn this、connection ; //取得数据库连接对象得实例
}
public void close() throws Exception{
ﻩ try{
ﻩ if(this、connection!=null){ //关闭数据库连接对象
ﻩﻩﻩthis、connection、close() ;
ﻩﻩ }
ﻩﻩ}catch(Exception e){
ﻩthrow e ; //异常交给被调用处处理
ﻩ}
}
}
interface ICourseDAO{
ﻩ/*
ﻩ * 1、数据得增加操作,用于向数据库中添加一条课程记录
* param course:表示一个课程对象
ﻩ * return boolean:就是否添加成功
ﻩ * throws Exception:异常交给被调用处处理
ﻩ * */
ﻩpublic abstract boolean doCreate(Course course)throws Exception ;
/*
ﻩ * 2、数据得删除操作,用于删除数据库中得某条记录
ﻩ * param cno:表示要删除得课程得课程号
* return boolean:删除就是否成功
* throws Exception:异常交给被调用处处理
* */
ﻩpublic abstract boolean doRemove(int cno)throws Exception ;
ﻩ/*
* 3、数据得更新操作
ﻩ * param cno:表示要更新得课程得课程号
* return boolean:更新就是否成功
* throws Exception:异常交给被调用处处理
ﻩ * */
public abstract boolean doUpdate(int cno)throws Exception ;
ﻩ/*
* 4、查瞧数据库中得所有记录
ﻩ * param void
ﻩ * return List<Course〉:将查询结果以链表得形式返回给被调用出
ﻩ * throws Exception:异常交给被调用处处理
* */
public abstract List<Course> findAll()throws Exception ;
/*
* 5、根据关键字查询
ﻩ * param keyWord:查询得关键字
ﻩ * return List<Course〉:将查询得结果以链表得形式返回给被调用出
ﻩ * throws Exception:异常交给被调用处处理
* 函数得重载
* */
public abstract List<Course> findAll(String keyWord)throws Exception ;
ﻩ/*
ﻩ * 6、根据课程号查询
* param cno:要查询得课程号
ﻩ * return Course:将查询得结果返回给被调用处
* throws Exception:将异常抛出交给被调用处处理
ﻩ * */
ﻩpublic abstract Course find(int cno)throws Exception ;
/*
* 7、查询当前数据库中得记录数
* param void
ﻩ * return int :将查询得记录数返回给调用处
ﻩ * throws Exception:将异常抛出交给被调用处处理
ﻩ * */
ﻩpublic abstract int getAllCount()throws Exception ;
ﻩ/*
ﻩ * 8、得到根据关键字查询得记录数
* param keyWord:要查询得关键字
ﻩ * return int : 将查询得记录数返回给被调用处
* throws Exception:将异常抛出交给被调用处处理
* 函数得重载
ﻩ * */
public abstract int getAllCount(String keyWord)throws Exception ;
}
//真就是主题实现类,负责具体得操作
class CourseDAOImpl implements ICourseDAO{
ﻩprivate Connection conn ;
ﻩprivate PreparedStatement pstmt ;
public CourseDAOImpl(Connection conn){
//接收外部得数据库连接对象完成数据库得连接
ﻩﻩthis、conn = conn ;
}
Override
ﻩpublic boolean doCreate(Course course) throws Exception {
boolean flag = false ;
String sql = "Insert into Course(Cno,Cname,period,term,property) Values(?,?,?,?,?)" ;
ﻩﻩthis、pstmt = this、conn、prepareStatement(sql) ;
ﻩﻩthis、pstmt、setInt(1, course、getCno()) ;
this、pstmt、setString(2, course、getCname()) ;
ﻩﻩthis、pstmt、setInt(3, course、getPeriod()) ;
ﻩﻩthis、pstmt、setString(4, course、getTerm()) ;
this、pstmt、setString(5, course、getProperty()) ;
ﻩif(this、pstmt、executeUpdate()〉0){
ﻩ ﻩflag = true ;
ﻩﻩ}
ﻩthis、pstmt、close() ;
ﻩreturn flag;
}
ﻩOverride
public boolean doRemove(int cno) throws Exception {
ﻩﻩboolean flag = false ;
ﻩString sql = ”Delete From Course Where Cno = ? " ;
ﻩthis、pstmt = this、conn、prepareStatement(sql) ;
this、pstmt、setInt(1, cno) ;
ﻩif(this、pstmt、executeUpdate()>0){
ﻩﻩ flag = true ;
ﻩﻩ}
ﻩ this、pstmt、close() ;
ﻩ return flag ;
}
Override
ﻩpublic boolean doUpdate(int cno) throws Exception {
ﻩﻩboolean flag = false ;
ﻩﻩString sql = "Update Course Set Cname = ?,period = ?,term = ?,property = ? Where Cno = ?" ;
ﻩﻩthis、pstmt = this、conn、prepareStatement(sql) ;
System、out、println(”输入您想要添加得课程得课程名:");
ﻩString cname = new Scanner(System、in)、next() ;
ﻩ System、out、println(”输入您想要添加课程得学时:");
ﻩ int period = new Scanner(System、in)、nextInt() ;
ﻩ System、out、println("输入您想要添加得课程得上课学期:”);
ﻩ String term = new Scanner(System、in)、next() ;
System、out、println("请输入您想要添加得课程得课程性质:”);
ﻩﻩString property = new Scanner(System、in)、next() ;
ﻩ this、pstmt、setString(1, cname) ;
ﻩﻩthis、pstmt、setInt(2, period) ;
ﻩthis、pstmt、setString(3, term) ;
ﻩthis、pstmt、setString(4, property) ;
ﻩﻩthis、pstmt、setInt(5, cno) ;
ﻩﻩif(this、pstmt、executeUpdate()>0){
ﻩﻩ flag = true ;
ﻩ}
this、pstmt、close() ;
ﻩreturn flag ;
}
Override
public List<Course> findAll() throws Exception {
ﻩList<Course> courses = new ArrayList〈Course〉() ;
ﻩﻩString sql = "Select Cno,Cname,period,term,property From Course” ;
ﻩthis、pstmt = this、conn、prepareStatement(sql) ;
ﻩResultSet rs = this、pstmt、executeQuery() ;
ﻩ while(rs、next()){
ﻩﻩﻩCourse course = new Course() ;
ﻩcourse、setCno(rs、getInt(1)) ;
ﻩ course、setCname(rs、getString(2)) ;
ﻩﻩcourse、setPeriod(rs、getInt(3)) ;
ﻩ ﻩcourse、setTerm(rs、getString(4)) ;
ﻩ course、setProperty(rs、getString(5)) ;
ﻩcourses、add(course) ;
ﻩ}
ﻩ this、pstmt、close() ;
ﻩ return courses;
}
ﻩOverride
public List<Course> findAll(String keyWord) throws Exception {
ﻩﻩList<Course> courses = new ArrayList<Course>() ;
String sql = "Select Cno,Cname,period,term,property From Course Where Cname like ?” +
ﻩﻩ ﻩ"OR term like ? OR property like ?" ;
ﻩ this、pstmt = this、conn、prepareStatement(sql) ;
ﻩ this、pstmt、setString(1, "%” + keyWord + "%") ;
ﻩﻩthis、pstmt、setString(2, "%” + keyWord + "%") ;
ﻩ this、pstmt、setString(3, ”%" + keyWord + ”%”) ;
ﻩResultSet rs = this、pstmt、executeQuery() ;
ﻩ while(rs、next()){
ﻩ Course course = new Course() ;
ﻩﻩﻩcourse、setCno(rs、getInt(1)) ;
ﻩ course、setCname(rs、getString(2)) ;
ﻩ course、setPeriod(rs、getInt(3)) ;
ﻩﻩcourse、setTerm(rs、getString(4)) ;
ﻩ ﻩcourse、setProperty(rs、getString(5)) ;
ﻩcourses、add(course) ;
ﻩ}
ﻩthis、pstmt、close() ;
ﻩﻩreturn courses;
}
ﻩOverride
ﻩpublic Course find(int cno) throws Exception {
ﻩ Course course = new Course() ;
ﻩﻩString sql = ”Select Cno,Cname,period,term,property From Course Where Cno = ?” ;
ﻩﻩthis、pstmt = this、conn、prepareStatement(sql) ;
ﻩ this、pstmt、setInt(1, cno) ;
ﻩResultSet rs = this、pstmt、executeQuery() ;
ﻩ while(rs、next()){
ﻩﻩ course、setCno(rs、getInt(1)) ;
ﻩ course、setCname(rs、getString(2)) ;
ﻩﻩcourse、setPeriod(rs、getInt(3)) ;
ﻩ course、setTerm(rs、getString(4)) ;
ﻩcourse、setProperty(rs、getString(5)) ;
ﻩﻩ}
ﻩthis、pstmt、close() ;
ﻩ return course ;
ﻩ}
Override
public int getAllCount() throws Exception {
int count = 0 ;
ﻩList<Course〉 courses = new ArrayList〈Course>() ;
ﻩString sql = "Select Cno,Cname,period,term,property From Course" ;
ﻩ this、pstmt = this、conn、prepareStatement(sql) ;
ﻩﻩResultSet rs = this、pstmt、executeQuery() ;
ﻩﻩwhile(rs、next()){
Course course = new Course() ;
ﻩ ﻩcourse、setCno(rs、getInt(1)) ;
ﻩ course、setCname(rs、getString(2)) ;
ﻩ ﻩcourse、setPeriod(rs、getInt(3)) ;
ﻩcourse、setTerm(rs、getString(4)) ;
ﻩﻩcourse、setProperty(rs、getString(5)) ;
ﻩ ﻩcourses、add(course) ;
ﻩ }
count = courses、size() ;
ﻩﻩthis、pstmt、close() ;
ﻩﻩreturn count;
ﻩ}
Override
public int getAllCount(String keyWord) throws Exception {
ﻩint count = 0 ;
ﻩList〈Course> courses = new ArrayList<Course>() ;
String sql = ”Select Cno,Cname,period,term,property From Course Where Cname like ?" +
ﻩ"OR term like ? OR property like ?” ;
ﻩthis、pstmt = this、conn、prepareStatement(sql) ;
ﻩthis、pstmt、setString(1, ”%" + keyWord + "%”) ;
ﻩthis、pstmt、setString(2, ”%” + keyWord + ”%”) ;
ﻩ this、pstmt、setString(3, ”%” + keyWord + "%") ;
ﻩﻩResultSet rs = this、pstmt、executeQuery() ;
ﻩﻩwhile(rs、next()){
Course course = new Course() ;
ﻩﻩcourse、setCno(rs、getInt(1)) ;
ﻩﻩ course、setCname(rs、getString(2)) ;
ﻩ ﻩcourse、setPeriod(rs、getInt(3)) ;
course、setTerm(rs、getString(4)) ;
ﻩﻩ course、setProperty(rs、getString(5)) ;
ﻩcourses、add(course) ;
ﻩ}
ﻩ count = courses、size() ;
ﻩ this、pstmt、close() ;
ﻩreturn count ;
}
}
//代理实现类,(帮助真就是主题更好得完成操作,负责数据库得打开与关闭,并调用真实主题完成操作)
class CourseDAOProxy implements ICourseDAO{
private DatebaseConnection dbc = null ;
private ICourseDAO dao ;
ﻩpublic CourseDAOProxy() throws Exception{
ﻩ try {
ﻩﻩﻩthis、dbc = new DatebaseConnection() ;
ﻩ ﻩ//为真实主体完成数据库得连接
ﻩ this、dao = new CourseDAOImpl(this、dbc、getConnection()) ;
ﻩﻩ} catch (Exception e) {
ﻩﻩﻩthrow e ;
ﻩ }
ﻩ}
ﻩpublic DatebaseConnection getDbc(){
ﻩ return this、dbc ;
ﻩ}
ﻩOverride
ﻩpublic boolean doCreate(Course course) throws Exception {
ﻩ boolean flag = false ;
ﻩ try{
ﻩ if(this、dao、find(course、getCno())、getCname()==null){//要添加得课程号已经存在则不能添加
ﻩﻩﻩ //调用真就是主题完成数据得增加操作
ﻩﻩﻩ flag = this、dao、doCreate(course) ;
ﻩ}
}catch(Exception e){
ﻩ ﻩthrow e ;
ﻩﻩ}
ﻩﻩreturn flag ;
ﻩ}
ﻩOverride
public boolean doRemove(int cno) throws Exception {
ﻩboolean flag = false ;
ﻩ try{
ﻩﻩ if(this、find(cno)、getCname()!=null){ //要删除得课程得课程号存在,调用真实主题完成数据得删除操作
ﻩ ﻩ flag = this、dao、doRemove(cno) ;
ﻩﻩﻩ}
ﻩﻩ}catch(Exception e){
ﻩ throw e ;
ﻩ}
return flag ;
}
Override
ﻩpublic boolean doUpdate(int cno) throws Exception {
ﻩ boolean flag = false ;
ﻩtry{
if(this、find(cno)!=null){
ﻩ ﻩ //要更新得课程得课程好存在完成数据得更新操作
ﻩﻩ flag = this、dao、doUpdate(cno) ;
ﻩﻩ}
}catch(Exception e){
ﻩﻩ throw e ;
ﻩ }
ﻩreturn flag ;
}
Override
ﻩpublic List<Course> findAll() throws Exception {
ﻩ List<Course> courses = new ArrayList<Course>() ;
ﻩ try{
ﻩﻩcourses = this、dao、findAll() ;
ﻩ }catch(Exception e){
ﻩ throw e ;
}
ﻩ return courses;
}
ﻩOverride
ﻩpublic List〈Course〉 findAll(String keyWord) throws Exception {
List<Course〉 courses = new ArrayList<Course>() ;
try{
ﻩﻩcourses = this、dao、findAll(keyWord) ;
}catch(Exception e){
ﻩ throw e ;
ﻩ}
ﻩﻩreturn courses;
ﻩ}
Override
public Course find(int cno) throws Exception {
Course course = new Course() ;
//ﻩ Course course = null ;
ﻩ try{
ﻩﻩ course = this、dao、find(cno) ;
ﻩ}catch(Exception e){
ﻩ throw e ;
}
ﻩﻩreturn course;
}
Override
ﻩpublic int getAllCount() throws Exception {
ﻩﻩint count = 0 ;
ﻩtry{
ﻩﻩﻩcount = this、dao、getAllCount() ;
}catch(Exception e){
ﻩthrow e ;
ﻩ }
ﻩ return count;
}
ﻩOverride
ﻩpublic int getAllCount(String keyWord) throws Exception {
int count = 0 ;
ﻩtry{
ﻩ ﻩcount = this、dao、getAllCount(keyWord) ;
ﻩﻩ}catch(Exception e){
ﻩ throw e ;
ﻩ }
return count;
ﻩ}
}
//工厂类,解耦合
class DAOFactory{
//以接口为操作标注
public static ICourseDAO getICourseDAOInstance() throws Exception{
ﻩﻩtry{
ﻩ return new CourseDAOProxy() ;
ﻩ }catch(Exception e){
ﻩ ﻩthrow e ;
}
}
}
//显示类,给出用户提示信息
class Menu{
public Menu(){
//无参构造方法
ﻩ}
ﻩpublic void disMenu(){
ﻩSystem、out、println("==============欢迎光临本系统================");
ﻩﻩSystem、out、println("[1]、增加数据”);
ﻩSystem、out、println("[2]、删除数据");
ﻩﻩSystem、out、println(”[3]、修改数据");
ﻩSystem、out、println("[4]、按课程号查瞧数据");
ﻩSystem、out、println("[5]、按关键字查瞧所有数据”);
ﻩﻩSystem、out、println(”[6]、显示所有数据”);
ﻩSystem、out、println(”[7]、显示所有得记录数”);
System、out、println("[8]、按关键字查询得到记录数”);
ﻩ System、out、println("[0]、退出系统”);
System、out、println(”按提示操作");
}
ﻩpublic void selectMenu() throws Exception{
ﻩ int select = 1 ;
ﻩSystem、out、println("请输入您想要得服务类型:”);
ﻩselect = new Scanner(System、in)、nextInt() ; ;
ﻩswitch(select){
ﻩcase 1: this、doCreate() ; break ;
ﻩcase 2: this、doDelete() ; break ;
case 3: this、doUpdate() ; break ;
ﻩcase 4: this、find() ; break ;
ﻩcase 5: this、findAll() ; break ;
ﻩ case 6: this、findall() ; break ;
ﻩﻩcase 7: this、getallCount() ; break ;
ﻩcase 8: this、getAllCount() ; break ;
ﻩﻩcase 0:{//退出时关闭数据库连接
try {
ﻩ ﻩnew CourseDAOProxy()、getDbc()、close() ;
ﻩﻩﻩ} catch (Exception e) {
ﻩﻩﻩthrow e ;
ﻩﻩ}
ﻩ System、exit(1) ;break ; //退出系统
ﻩ }
ﻩﻩdefault:System、out、println("请选择正确得操作”); break;
}
ﻩ}
public void doUpdate() throws Exception {
ﻩSystem、out、println("请输入您想要更新得课程号:");
ﻩﻩint cno = new Scanner(System、in)、nextInt() ;
ﻩ try {
ﻩﻩif(DAOFactory、getICourseDAOInstance()、doUpdate(cno)){
ﻩ ﻩ System、out、println("更新成功!!!");
ﻩﻩ}else{
ﻩ System、out、println("更新失败!!!”);
ﻩﻩ }
ﻩﻩ} catch (Exception e) {
ﻩﻩ throw e ;
ﻩ}
}
public void getallCount() throws Exception{
ﻩ int count = 0 ;
ﻩ try {
ﻩ count = DAOFactory、getICourseDAOInstance()、getAllCount() ;
ﻩ } catch (Exception e) {
ﻩﻩthrow e ;
ﻩ}
ﻩ System、out、println("当前数据库中得记录数为:" + count);
}
public void getAllCount() throws Exception{
ﻩﻩSystem、out、println("请输入您想要查询得关键字:");
ﻩString keyWord = new Scanner(System、in)、next() ;
ﻩint count = 0 ;
try {
ﻩﻩcount = DAOFactory、getICourseDAOInstance()、getAllCount(keyWord) ;
ﻩ} catch (Exception e) {
展开阅读全文