概述
package Pritlce.JDbcMINDVD;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* username=scott
* password=oracle
* driverClassName=oracle.jdbc.driver.OracleDriver
* url=jdbc:oracle:thin:@localhost:1521:orcl
*
*
* #driver =com.mysql.jdbc.Driver
* #jdbcUrl =jdbc:mysql://localhost:3306/goods
* ##user=root
* #password=mysqladmin
*
* @author 廖聪聪
* @version 1.0
* 创建时间 2020/4/7 10:29
*/
public class DBUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String user ="root";
private static String password = "mysqladmin";
private static String url ="jdbc:mysql://localhost:3306/ctrlbook";
//1.获取驱动
static {
try {
Class.forName(driver);
}catch (Exception e){
e.printStackTrace();
}
}
//2.创建连接
public static Connection get_conn() throws SQLException {
Connection conn = DriverManager.getConnection(url,user,password);
//System.out.println("数据库连接成功!");
return conn;
}
//3.关闭连接
public static void get_CloseConn(ResultSet rs, PreparedStatement pmst, Connection conn){
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pmst!=null){
try {
pmst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*// 测试数据库
public static void main(String[] args) {
try {
get_conn();
}catch (SQLException e){
e.printStackTrace();
}
}*/
}
package Pritlce.JDbcMINDVD;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author 廖聪聪
* @version 1.0
* 创建时间 2020/4/27 20:47
*/
public class DVDDao {
//输入器
Scanner input = new Scanner(System.in);
private static String sql;
private static Connection conn;
private static PreparedStatement pstm;
private static ResultSet rs;
/**
* 查询所有DVD
* @return 放回list集合
*/
public List<DvDSet> getAll(){
DvDSet dvDSet = null;//实例
//创建集合
List<DvDSet> list = new ArrayList<>();
try {
conn = DBUtil.get_conn();
sql="select * from book";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()){
dvDSet = new DvDSet();
dvDSet.setId(rs.getInt(1));
dvDSet.setName(rs.getString(2));
dvDSet.setState(rs.getInt(3));
dvDSet.setBdate(rs.getDate(4));
dvDSet.setRent(rs.getFloat(5));
dvDSet.setBcount(rs.getInt(6));
list.add(dvDSet);
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.get_CloseConn(rs,pstm,conn);
}
return list;
}
/**
* 查询某个DVD
* @param name
* @return //返回一个DVD的对象
*/
public DvDSet secherDVD(String name){
DvDSet dvDSet = null;
try {
conn=DBUtil.get_conn();
sql="select * from book where name=? ";
pstm = conn.prepareStatement(sql);
pstm.setString(1,name);
rs= pstm.executeQuery();
while (rs.next()){
dvDSet = new DvDSet();
dvDSet.setId(rs.getInt(1));
dvDSet.setName(rs.getString(2));
dvDSet.setState(rs.getInt(3));
dvDSet.setBdate(rs.getDate(4));
dvDSet.setRent(rs.getFloat(5));
dvDSet.setBcount(rs.getInt(6));
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.get_CloseConn(rs,pstm,conn);
}
return dvDSet ;//
}
/**
* 添加DVD
* @param dvDSet
* @return
*/
public boolean addDVDset(DvDSet dvDSet){
boolean flag = false; //添加失败
//插叙该书是存在
DvDSet dvDSet1 = secherDVD(dvDSet.getName());
if (dvDSet1==null){
try {
conn = DBUtil.get_conn();
sql = "insert into book (name ,rent,state) values (?,?,?)";
pstm = conn.prepareStatement(sql);
pstm.setString(1,dvDSet.getName());
pstm.setFloat(2,dvDSet.getRent());
pstm.setInt(3,dvDSet.getState());
pstm.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.get_CloseConn(rs,pstm,conn);
}
flag=true;
}else {
flag = false;
}
return flag;
}
/**
* 删除DVD
* @param name
* @return 返回是否成功删除DVD:1成功删除;-1已借出,删除失败;0不存在,删除失败
*/
public int delDVD(String name){
int flag = -1;//默认初始值不存在
//查询DVD是否存在
DvDSet d1=secherDVD(name);
if (d1==null){
flag=-1;
}
else {
if (d1.getState()==1) {//判断是否是可借状态
try {
conn = DBUtil.get_conn();
sql = "delete from book where name =?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, name);
pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.get_CloseConn(rs, pstm, conn);
}
flag = 1;
}else {
flag=0;//已借出
}
}
return flag;
}
/**
* 借出DVD
* @param name
* @return 返回是否成功 1 借出成功, -1 已借出 0 不存在
*/
public int borrowDVD(String name){
int flag = -1;
//调用方法是否存在
DvDSet d2=secherDVD(name);
if (d2==null){
flag=-1;
}
else {
if (d2.getState()==1) {
d2.setState(0);//修改状态
d2.setBdate(new Date(System.currentTimeMillis()));//获取系统的借出时间
d2.setBcount(d2.getBcount()+1);//借出成功的次数
try {
conn = DBUtil.get_conn();
sql ="update book set state = ? , bdate =?, bcount =? where name =?";
pstm = conn.prepareStatement(sql);
pstm.setInt(1,d2.getState());
pstm.setDate(2,d2.getBdate());
pstm.setInt(3,d2.getBcount());
pstm.setString(4,d2.getName());
pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.get_CloseConn(rs, pstm, conn);
}
flag = 1;
}else {
flag=0;//已借出
}
}
return flag;
}
/**
* 还书功能
* @param name
* @return 返回归还是否成功 1 成功 -1已经可借 0 不存在
*/
public int lendDVD(String name){
int flag = -1;
DvDSet d2=secherDVD(name);
if (d2==null){
flag=-1;
}
else {
if (d2.getState()==0) {
d2.setState(1);
//获取当前日期
java.util.Date utilDate = new java.util.Date();//uilt.Date
Date bdate = d2.getBdate();//获取该DVD上次借出时间
Date date2 = new Date(bdate.getTime());//sql.date 转换成utildate时间
//计算借出时间
int borrowDays = (int)Math.ceil((utilDate.getTime()-date2.getTime())*1.0/1000/60/60/24);
float rent = borrowDays*d2.getRent();
System.out.println("应付租金为:"+rent+"元");
try {
conn = DBUtil.get_conn();
sql ="update book set state=? where name =?";
pstm = conn.prepareStatement(sql);
pstm.setInt(1,d2.getState());
pstm.setString(2,d2.getName());
pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.get_CloseConn(rs, pstm, conn);
}
flag = 1;
}else {
flag=0;//可借状态
}
}
return flag;
}
/**
* 按借出次数进行排序并返回所有DVD信息
* @return
*/
public List<DvDSet> rankMenu() {
DvDSet dvDSet = null;
List<DvDSet> list = new ArrayList<>();
try {
conn = DBUtil.get_conn();
sql="select id,name,state,bdate,rent,bcount from book ORDER BY bcount desc ";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()){
dvDSet = new DvDSet();
dvDSet.setId(rs.getInt(1));
dvDSet.setName(rs.getString(2));
dvDSet.setState(rs.getInt(3));
dvDSet.setBdate(rs.getDate(4));
dvDSet.setRent(rs.getFloat(5));
dvDSet.setBcount(rs.getInt(6));
list.add(dvDSet);
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.get_CloseConn(rs,pstm,conn);
}
return list;
}
}
package Pritlce.JDbcMINDVD;
import java.sql.Date;
/**
*
* @author 廖聪聪
* @version 1.0
* 创建时间 2020/4/27 17:00
*/
public class DvDSet {
//定义属性
private int id;
private String name;//DVD名称
private int state; //DVD借出状态:0已借出/1可借
private Date bdate; //DVD借出日期
private float rent; //每日租金
private int bcount; //借出次数
public DvDSet(int id, String name, int state, Date bdate, float rent, int bcount) {
this.id = id;
this.name = name;
this.state = state;
this.bdate = bdate;
this.rent = rent;
this.bcount = bcount;
}
public float getRent() {
return rent;
}
public void setRent(float rent) {
this.rent = rent;
}
public DvDSet() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public Date getBdate() {
return bdate;
}
public void setBdate(Date bdate) {
this.bdate = bdate;
}
public int getBcount() {
return bcount;
}
public void setBcount(int bcount) {
this.bcount = bcount;
}
}
package Pritlce.JDbcMINDVD;
/**
* @author 廖聪聪
* @version 1.0
* 创建时间 2020/4/27 20:45
*/
public class TestDVD {
/**
* 程序入口
* @param args
*/
public static void main(String[] args) {
DVDMenu dvdMenu = new DVDMenu();
dvdMenu.showMenu();
}
}
最后
以上就是无聊大山为你收集整理的迷你图书管理系统 MYSQL+JDBC的全部内容,希望文章能够帮你解决迷你图书管理系统 MYSQL+JDBC所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复