//图书管类
public class Book {
private Integer id;
private String b_name;
private double b_price;
private Integer b_number;
private Date b_date;
//无参构造方法
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(Integer id, String b_name, double b_price, Integer b_number, Date b_date) {
this.id = id;
this.b_name = b_name;
this.b_price = b_price;
this.b_number = b_number;
this.b_date = b_date;
}
public Book( String b_name, double b_price, Integer b_number,Date b_date) {
this.b_name = b_name;
this.b_price = b_price;
this.b_number = b_number;
this.b_date=b_date;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
public double getB_price() {
return b_price;
}
public void setB_price(double b_price) {
this.b_price = b_price;
}
public Integer getB_number() {
return b_number;
}
public void setB_number(Integer b_number) {
this.b_number = b_number;
}
public Date getB_date() {
return b_date;
}
public void setB_date(Date b_date) {
this.b_date = b_date;
}
@Override
public String toString() {
return this.id+" "+this.b_name+" "+this.b_price+" "+this.b_number
+" "+this.b_date;
}
}
//用户类
public class MyUser {
private Integer id;
private String u_name;
private String u_pass;
public MyUser() {
super();
// TODO Auto-generated constructor stub
}
public MyUser(Integer id,String u_name, String u_pass) {
this.id = id;
this.u_name = u_name;
this.u_pass = u_pass;
}
public MyUser(String u_name, String u_pass) {
this.u_name = u_name;
this.u_pass = u_pass;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public String getU_pass() {
return u_pass;
}
public void setU_pass(String u_pass) {
this.u_pass = u_pass;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.id+" "+this.u_name+" "+this.u_pass;
}
}
//图书接口
public interface Bookdao {
public abstract boolean add(Book b);
public abstract boolean del(Integer id);
public abstract boolean change(Integer id,double price);
public abstract Book find(Integer id);
public abstract List<Book>findAll();
}
//用户接口
public interface MyUserDao {
//根据id与密码来进行登录
public abstract MyUser find(String name,String u_pass);
//注册
public abstract boolean register(MyUser my);
}
//jdbc工具类,注册驱动获取连接数据库对象 释放资源
ublic class jdbcUtils {
//定义初始化
private static String urls;
private static String users;
private static String passwords;
private static String drivers;
//注册驱动
static{
//需要读取资源文件
try {
//创建属性集对象
Properties pro=new Properties();
//通过当前类的字节码文件对象来获取类加载器对象
ClassLoader classLoader=jdbcUtils.class.getClassLoader();
//使用类加载器对象来获取当前项目下的src下的配置文件
URL res=classLoader.getResource("jdbc.properties");
//使用res对象获取数据库的路径
String path=res.getPath();
//打印路径
System.out.println(path);
//通过属性集对象来加载文件
pro.load(new FileReader(path));
//使用属性集对象,来获取配置文件中的数据
urls=pro.getProperty("url");
users=pro.getProperty("user");
passwords=pro.getProperty("password");
drivers=pro.getProperty("driver");
//注册驱动
Class.forName(drivers);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接数据库对象
public static Connection getConnetion() throws SQLException{
return DriverManager.getConnection(urls,users,passwords);
}
//释放资源两个参数 三参数
public static void close(Statement sta,Connection con){
if(sta!=null){
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//三个参数
public static void close(ResultSet rs,Statement sta,Connection con){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(sta!=null){
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//该工具类,是为了将获取毫秒值转换为我们想要的日期格式对象
public class Myutils {
public static Date getDate(String date){
Date myDate=null;
//创建日期解析格式化对象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//通过解析对象将对象字符串转换为日期对象
try {
myDate=sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myDate;
}
}
//图书接口实现类,具体来完成 增删改查 查看所有功能操作
public class BookDaoImpI implements Bookdao {
@Override
public boolean add(Book b) {
Connection con=null;
PreparedStatement ps=null;
try {
//使用jdbc 工具类来注册驱动,获取数据库连接对象
con=jdbcUtils.getConnetion();
//定义sql语句
String sql="insert into Book(b_name,price,number,b_date)values(?,?,?,?)";
//获取执行sql对象
ps=con.prepareStatement(sql);
//给?赋值
ps.setString(1,b.getB_name());
ps.setDouble(2,b.getB_price());
ps.setInt(3,b.getB_number());
ps.setDate(4,b.getB_date());
//执行sql对象
int i=ps.executeUpdate();
//处理结果
return i>0? true:false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtils.close(ps, con);
}
return false;
}
@Override
public boolean del(Integer id) {
Connection con=null;
PreparedStatement ps=null;
try {
//使用jdbc连接数据库,注册驱动,获取连接数据库对象
con=jdbcUtils.getConnetion();
//定义sql
String sql="delete from Book where id =?";
//获取执行sql对象
ps=con.prepareStatement(sql);
//给?赋值
ps.setInt(1,id);
//执行sql语句
int i=ps.executeUpdate();
//处理结果
return(i>0)?true:false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtils.close(ps, con);
}
return false;
}
@Override
public boolean change(Integer id, double price) {
PreparedStatement ps=null;
Connection con=null;
try {
//注册驱动,获取数据库连接对像
con=jdbcUtils.getConnetion();
//定义sql语句
String sql="update Book set price=? where id=?";
//获取执行sql对象
ps=con.prepareStatement(sql);
//给?赋值;
ps.setDouble(1,price);
ps.setInt(2,id);
//执行sql语句
int i=ps.executeUpdate();
//处理结果
return i>0? true:false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtils.close(ps, con);
}
return false;
}
@Override
public Book find(Integer id) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
//创建book对象
Book b=null;
try {
//使用jdbc 连接数据库,注册驱动,获取连接数据库连接对象
con=jdbcUtils.getConnetion();
//定义sql
String sql="select*from Book where id=?";
//获取执行sql对象
ps=con.prepareStatement(sql);
//给?赋值
ps.setInt(1, id);
//执行sql语句
rs=ps.executeQuery();
//处理结果
if(rs.next()){
//获取结果集中的数据
Integer b_id=rs.getInt(1);
String b_name=rs.getString(2);
Double b_price=rs.getDouble(3);
Integer b_number=rs.getInt(4);
Date b_date=rs.getDate(5);
//创建book对象,并给对象添加值
b=new Book(b_id,b_name,b_price,b_number,b_date);
}
return b;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtils.close(rs,ps, con);
}
return null;
}
@Override
public List<Book> findAll() {
//创建集合对象
List<Book>list=null;
try {
//实用工具类来注册驱动,获取数据连接对象
Connection con=jdbcUtils.getConnetion();
//定义sql语句
String sql="select*from Book";
//获取执行sql对象
PreparedStatement ps=con.prepareStatement(sql);
//执行sql对象
ResultSet rs=ps.executeQuery();
//处理结果,判断结果集中是否有数据,如果有数据,那么,需要给集合添加对象
list=new ArrayList<Book>();
Book b=null;
while(rs.next()){
//获取结果集中的数据
Integer id=rs.getInt(1);
String name=rs.getString(2);
double price=rs.getDouble(3);
Integer number=rs.getInt(4);
Date date=rs.getDate(5);
//将结果集中获取的数据,给添加到BOOK实体对象中
b=new Book(id,name,price,number,date);
//给集合添加数据
list.add(b);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
public class myUserDaoImpI implements MyUserDao {
@Override//登录
public MyUser find(String name, String u_pass) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//注册驱动,获取数据库连接对象
con=jdbcUtils.getConnetion();
//定义sql语句,使用?来作为占位符
String sql="select * from myUser where id=? and m_pass=?";
//获取执行sql对象
ps=con.prepareStatement(sql);
//给?赋值
ps.setString(1,name);
ps.setString(2,u_pass);
//执行sql对象
rs=ps.executeQuery();
if(rs.next()){
int b_id = rs.getInt(1);
String b_name=rs.getString(2);
String b_pass=rs.getString(3);
return new MyUser(b_id,b_name,b_pass);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtils.close(rs,ps, con);
}
return null;
}
@Override
public boolean register(MyUser my) {
Connection con=null;
PreparedStatement ps =null;
try {
con=jdbcUtils.getConnetion();
String sql="insert into myUser(m_name,m_pass) values(?,?)";
ps=con.prepareStatement(sql);
ps.setString(1,my.getU_name());
ps.setString(2,my.getU_pass());
int i=ps.executeUpdate();
return(i!=0)?true:false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtils.close(ps, con);
}
return false;
}
}
public class server {
Scanner sc=new Scanner(System.in);
// MyUserDao ud=new myUserDaoImpI();
myUserDaoImpI ud = new myUserDaoImpI();
Bookdao gd=new BookDaoImpI();
//定义程序开始方法
public void start(){
menu();
}
public void menu(){
System.out.println("===某某图书馆注册登录管理系统====");
System.out.println("1注册");
System.out.println("2登录");
System.out.println("3退出");
System.out.println("请选择");
int n=sc.nextInt();
switch (n) {
case 1:
regist();
break;
case 2:
islogin();
break;
case 3:
System.out.println();
System.exit(0);
default:
break;
}
System.out.println();
menu();
}
public void regist(){
System.out.println("===xx图书管理注册界面===");
// System.out.println("请输入要注册的id");
// Integer id=sc.nextInt();
System.out.println("请输入要注册的用户名");
String name=sc.next();
System.out.println("请输入要注册的密码");
String password=sc.next();
boolean b=ud.register(new MyUser(name,password));
if(b){
System.out.println("注册成功");
}else{
System.out.println("注册失败");
return;
}
//登录
public void islogin(){
System.out.println("==图书管理登录界面");
System.out.println("请输入登录的id编号");
String name=sc.next();
System.out.println("请输入登录的密码");
String password=sc.next();
MyUser mu=ud.find(name, password);
//判断用户对象是否存在
if(mu!=null){
System.out.println("登陆成功");
System.out.println();
System.out.println("可以进入二级菜单");
menu2();
}else{
System.out.println("登录失败");
}
}
public void menu2(){
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("请选择");
int n = sc.nextInt();
switch (n) {
case 1:
BookFindAll();
break;
case 2:
OneByAdd();
break;
case 3:
delByID();
break ;
case 4:
changByld();
break;
case 5:
findByld();
break;
case 6:
System.out.println("程序已退出");
System.exit(0);
break;
default:
System.out.println("选择错误,请重新选择");
break;
}
System.out.println();
menu2();
}
public void BookFindAll(){
System.out.println("图书编号 图书名称 图书价格 图书库存 图书日期 ");
//调用接口抽象方法
List<Book>list=gd.findAll();
//判断接口是否有数据
if(list==null||list.isEmpty()){
System.out.println("该对象是没有数据的");
return;
}
for(Book b: list){
System.out.println(b);
}
}
public void OneByAdd(){
System.out.println("请输入书名称");
String name=sc.next();
System.out.println("请输入书价格");
double price=sc.nextDouble();
System.out.println("请输入书库存");
Integer number=sc.nextInt();
System.out.println("请输入书生产日期");
String strDate=sc.next();
//将键盘录入的日期转换Date对象
Date dat=Myutils.getDate(strDate);
long cratetime=dat.getTime();
java.sql.Date date=new java.sql.Date(cratetime);
//调用接口中的抽象方法
boolean b=gd.add(new Book(name,price,number,date));
if(b){
System.out.println("添加成功");
return;
}else{
System.out.println("添加失败");
}
}
public void delByID(){
System.out.println("请输入要删除图书的编号");
int id=sc.nextInt();
Book b=gd.find(id);
if(b==null){
System.out.println("图书不存在");
}else{
//调用接口删除方法
boolean g=gd.del(id);
if(g){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}
public void changByld(){
System.out.println("请输入要修改价格图书编号");
int id=sc.nextInt();
System.out.println("请输入要修改的价格");
double price=sc.nextDouble();
Book b=gd.find(id);
if(b==null){
System.out.println("该图书不存在");
return;
}else{
boolean g=gd.change(id, price);
if(g){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
return;
}
}
}
public void findByld(){
System.out.println("请输入要查询图书的编号");
int i=sc.nextInt();
//调用接口中的查找方法
Book b=gd.find(i);
if(b!=null){
System.out.println("图书编号 图书名称 图书单价 图书库存 生产日期");
System.out.println(b);
}else{
System.out.println("该图书不存在");
return;
}
}
}
//测试
public class test {
public static void main(String[] args) {
new server().start();
}