java----简单迷你DVD管理器(连接数据库)☆☆☆☆☆☆

package com.test;

import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		OracleUtil util = new OracleUtil();
		Scanner input=new Scanner(System.in);
		
		// 欢迎
		int con=-1;
		do {
			System.out.println("欢 迎 使 用 迷 你 DVD 管 理 器");
			System.out.println("-------------------------------------");
			System.out.println("1. 新 增 DVD");
			System.out.println("2. 查 看 DVD");
			System.out.println("3. 删 除 DVD");
			System.out.println("4. 借 出 DVD");
			System.out.println("5. 归 还 DVD");
			System.out.println("6. 退 出DVD");
			System.out.print("--------------------------------------\n");
			System.out.print("请选择: ");
			int choice = input.nextInt();
			switch (choice) {
			case 1:
				System.out.println("实现新增dvd的功能");
				util.addDvd();
				break;
			case 2:
				System.out.println("实现查看dvd的功能");
				util.getAll();
				break;
			case 3:
				System.out.println("实现删除dvd的功能");
				util.deletedvd();
				break;
			case 4:
				System.out.println("实现借出dvd的功能");				
				util.lentdvd();	          
				break;
			case 5:
				System.out.println("实现归还dvd的功能");				
				util.backdvd();
				break;
			case 6:
				System.exit(0);
				break;
			default:
				break;
			}
			System.out.println("输入0继续");
			con=input.nextInt();
		}while(con==0);
	}

}

package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class OracleUtil {
	Connection conn;
	Statement st;
	PreparedStatement ps;
	ResultSet rs;
	
	//建立连接
	public Connection getConn() {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			// 2.建立连接
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
		} catch (Exception e) {
			e.printStackTrace();
		} 
		//System.out.println("getConn  conn  "+conn);
		return conn;
	}
	
	//释放连接
	public void closeConn() {
		try {
			//null  是没有方法的
			//st 就是null
			if(st !=null) {				
				st.close();
			}
			if(rs !=null) {
				rs.close();
			}
			if(conn !=null) {				
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//查看dvd的所有人信息
	public void getAll() {
		try {
			this.getConn();
			String sql="select * from dvds";
			ps=conn.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			System.out.println("序号\t名字\t借出次数\t状态\t归还日期\t");
			while(rs.next()) {
				System.out.println(rs.getInt(1)+"\t"+
			rs.getString(2)+"\t"+rs.getInt(3)+"\t"+
						rs.getInt(4)+"\t"+rs.getInt(5));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeConn();
		}
	}
	
	//新增dvd
	public void addDvd() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入新的DVD名字:");
		String name=input.next();
		//判断是否已经存在的DVD
		if(exists(name)) {
			System.out.println("要新增的DVD已经存在,新增失败!");
			return;   //此方法后面的代码不在执行
		}
		try {
			this.getConn();
			String sql="insert into dvds values(dvds_id.nextval,?,?,?,?)";
			ps=conn.prepareStatement(sql);			
			ps.setString(1, name);
			ps.setInt(2, 0);
			ps.setInt(3, 0);
			ps.setInt(4, 0);
			int i=ps.executeUpdate();
			if(i>0)
				System.out.println("新增dvd成功!");
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeConn();
		}
	}
	//true 存在 false 不存在
	public boolean exists(String name) {
		try {
			this.getConn();
			String sql="select * from dvds where name=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, name);
			ResultSet rs=ps.executeQuery();
			if(rs.next())
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeConn();
		}
		return false;
	}
	//删除DVD
	public void deletedvd()  {
		Scanner input=new Scanner(System.in);
		System.out.println("请输入要删除的DVD名字:");
		String name=input.next();
     	if(!exists(name)) {
		System.out.println("要删除的DVD不存在,删除失败!");
		return;   //此方法后面的代码不再执行	
		}
		try {
			this.getConn();
			String sql="delete from dvds where name=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.executeQuery();
			System.out.println("删除dvd成功!");
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeConn();
		}			
	}
	//借出DVD
	public void lentdvd() {
		Scanner input=new Scanner(System.in);
		System.out.println("输入借出的dvd的名字:");
		String name=input.next();
		if(!exists(name)) {
			System.out.println("要借的DVD不存在,借出失败!");
			return ;
		}			
		try {
			this.getConn();
			String sql="update dvds set status=1,count=count+1 where name=?";
			ps=conn.prepareStatement(sql);			
			ps.setString(1, name);
			System.out.println(ps.executeUpdate());
			System.out.println("借出dvd成功!");
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeConn();
		}
	}
	//归还DVD
	public void backdvd() {
		Scanner input=new Scanner(System.in);
		System.out.println("输入归还的dvd的名字:");
		String name=input.next();	
		try {
			this.getConn();
			String sql="update dvds set status=0,count=count-1,dates=?where name=?";
			ps=conn.prepareStatement(sql);
			Scanner input1 = new Scanner(System.in);
			System.out.println("请输入归还日期:");
			int date = input1.nextInt();
			ps.setInt(1,date);
			ps.setString(2, name);
			System.out.println(ps.executeUpdate());
			System.out.println("归还成功!");
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeConn();
		}
	}
	
	
}

数据库(Oracle)

--建表
create table dvds(
id number,
name varchar2(20),
count number,
status number,
dates number);

select * from dvds;

insert into dvds values(1,'哪吒',13,0,0);
insert into dvds values(2,'姜子牙',14,1,14);
insert into dvds values(3,'花木兰',15,0,0);

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值