首页 > 数据库 >图书连接数据库所有类

图书连接数据库所有类

时间:2023-06-20 23:58:04浏览次数:61  
标签:bid getString rs stm 数据库 tag sql 连接 图书

package com.rain.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import com.rain.bean.AdminBean;
import com.rain.bean.BookBean;
import com.rain.bean.HistoryBean;
import com.rain.bean.TypeBean;
import com.rain.util.DBUtil;
/**
* 关于图书连接数据库的所有操作的类
*/
public class BookDao {

/**
* 添加图书信息,传入所有的信息
* @param card
* @param name
* @param type
* @param author
* @param press
* @param num
*/
public void addBook(String card, String name, String type, String author, String press, int num) {
Connection conn = DBUtil.getConnectDb();
String sql = "insert into book(card,name,type,author,press,num) values(?,?,?,?,?,?)";
int rs = 0;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, card);
stm.setString(2, name);
stm.setString(3, type);
stm.setString(4, author);
stm.setString(5, press);
stm.setInt(6, num);
rs = stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 获取所有的图书信息,返回的是ArrayList数组形式
* @return
*/
public ArrayList<BookBean> get_ListInfo(){
ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from book";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while(rs.next()){
BookBean tag = new BookBean();
tag.setBid(rs.getInt("bid"));
tag.setName(rs.getString("name"));
tag.setCard(rs.getString("card"));
tag.setType(rs.getString("type"));
tag.setAuthor(rs.getString("author"));
tag.setPress(rs.getString("press"));
tag.setNum(rs.getInt("num"));
tag_Array.add(tag);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 获取借阅记录的全部信息,传入的条件有status,aid,表示搜索正在借阅的,或者已经还书的信息,aid代表当前登录用户
* @param status
* @return
*/
public ArrayList<HistoryBean> get_HistoryListInfo(int status,String aid){
ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from history where aid='"+aid+"' and status='"+status+"'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while(rs.next()){
HistoryBean tag = new HistoryBean();
tag.setHid(rs.getInt("hid"));
tag.setAid(rs.getInt("aid"));
tag.setBid(rs.getInt("bid"));
tag.setBookname(rs.getString("bookname"));
tag.setCard(rs.getString("card"));
tag.setAdminname(rs.getString("adminname"));
tag.setUsername(rs.getString("username"));
tag.setBegintime(rs.getString("begintime"));
tag.setEndtime(rs.getString("endtime"));
tag.setStatus(rs.getInt("status"));
tag_Array.add(tag);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 获取借阅记录的全部信息,传入的条件有status,表示搜索正在借阅的,或者已经还书的信息
* @param status
* @return
*/
public ArrayList<HistoryBean> get_HistoryListInfo2(int status){
ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from history where status='"+status+"'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while(rs.next()){
HistoryBean tag = new HistoryBean();
tag.setHid(rs.getInt("hid"));
tag.setAid(rs.getInt("aid"));
tag.setBid(rs.getInt("bid"));
tag.setBookname(rs.getString("bookname"));
tag.setCard(rs.getString("card"));
tag.setAdminname(rs.getString("adminname"));
tag.setUsername(rs.getString("username"));
tag.setBegintime(rs.getString("begintime"));
tag.setEndtime(rs.getString("endtime"));
tag.setStatus(rs.getInt("status"));
tag_Array.add(tag);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 获取单个图书的信息,根据传入的bid来查找,返回一个BookBean数据类型
* @param bid
* @return
*/
public BookBean get_BookInfo(int bid){
BookBean tag = new BookBean();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from book where bid='"+bid+"'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while(rs.next()){
tag.setBid(rs.getInt("bid"));
tag.setName(rs.getString("name"));
tag.setCard(rs.getString("card"));
tag.setType(rs.getString("type"));
tag.setAuthor(rs.getString("author"));
tag.setPress(rs.getString("press"));
tag.setNum(rs.getInt("num"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.CloseDB(rs, stm, conn);
}
return tag;
}
/**
* 修改图书的信息,bid作为条件,
*/
public void updateBook(int bid, String card, String name, String type, String author, String press, int num) {
// TODO Auto-generated method stub
Connection conn = DBUtil.getConnectDb();
String sql = "update book set name=?,card=?,type=?,author=?,press=?,num=? where bid=?";
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, name);
stm.setString(2, card);
stm.setString(3, type);
stm.setString(4, author);
stm.setString(5, press);
stm.setInt(6, num);
stm.setInt(7, bid);
stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 删除图书信息,根据传入的bid作为条件
* @param bid
*/
public void deleteBook(int bid) {
Connection conn = DBUtil.getConnectDb();
String sql = "delete from book where bid=?";
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setInt(1, bid);
stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
//System.out.println(uid);

}
/**
* 用户查找图书,根据输入的名称,使用like进行模糊查询,然后返回一个ArrayList数组类型
* @param name
* @return
*/
public ArrayList<BookBean> getLikeList(String name) {
ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from book where name like '%"+name+"%'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
System.out.println(sql);
rs = stm.executeQuery();
while(rs.next()){
BookBean tag = new BookBean();
tag.setBid(rs.getInt("bid"));
tag.setName(rs.getString("name"));
tag.setCard(rs.getString("card"));
tag.setType(rs.getString("type"));
tag.setAuthor(rs.getString("author"));
tag.setPress(rs.getString("press"));
tag.setNum(rs.getInt("num"));
tag_Array.add(tag);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 图书借阅函数,根据传入bid图书id,adminbean当前登录用户的信息,在借阅记录数据表中新插入一条记录
* @param bid
* @param adminbean
*/
public void borrowBook(int bid, AdminBean adminbean) {
BookBean bookbean = new BookBean();
bookbean = this.get_BookInfo(bid);
//生成日期的功能
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DATE);
//生成借阅开始日期
String begintime = ""+year+"-"+(month+1)+"-"+day;
month = month + 1;
//生成截止还书日期
String endtime = ""+year+"-"+(month+1)+"-"+day;
Connection conn = DBUtil.getConnectDb();
String sql = "insert into history(aid,bid,card,bookname,adminname,username,begintime,endtime,status) values(?,?,?,?,?,?,?,?,?)";
int rs = 0;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setInt(1, adminbean.getAid());
stm.setInt(2, bookbean.getBid());
stm.setString(3, bookbean.getCard());
stm.setString(4, bookbean.getName());
stm.setString(5, adminbean.getUsername());
stm.setString(6, adminbean.getName());
stm.setString(7, begintime);
stm.setString(8, endtime);
stm.setInt(9, 1);
rs = stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
updateBookNum(bookbean.getBid(),true);
}
/**
* 修改图书的数量信息,bid作为条件,
*/
public void updateBookNum(int bid,boolean flag) {
// TODO Auto-generated method stub
Connection conn = DBUtil.getConnectDb();
String sql =null;
if(flag){
//借阅
sql = "update book set num=num-1 where bid=?";
}else{
//还书
sql = "update book set num=num+1 where bid=?";
}

PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setInt(1, bid);
stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 还书功能的函数,根据传入的hid借阅记录id,讲status字段的值改为0,并将还书日期改变为当前日期
* @param hid
*/
public void borrowBook2(int hid,int bid) {
//生成日期
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DATE);
//生成还书日期
String endtime = ""+year+"-"+(month+1)+"-"+day;
Connection conn = DBUtil.getConnectDb();
String sql = "update history set endtime=?,status=? where hid=?";
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, endtime);
stm.setInt(2, 0);
stm.setInt(3, hid);
stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
updateBookNum(bid,false);
}

标签:bid,getString,rs,stm,数据库,tag,sql,连接,图书
From: https://www.cnblogs.com/lmzh/p/17495177.html

相关文章

  • 读者账号的连接数据库操作,登录验证,注册,修改账号,修改密码
    packagecom.rain.dao;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importcom.rain.bean.AdminBean;importcom.rain.bean.BookBean;importcom.rain.util.DBUtil;/*......
  • 图书分类的类
    packagecom.rain.dao;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importcom.rain.bean.TypeBean;importcom.rain.util.DBUtil;/***图书分类的类*/publicclassTyp......
  • 【230620-2】如图,在菱形ABCD中,AB=8,角D=60度,点F是CD的中点。点E是BC上一动点,连接AE、BF
    【230620-2】如图,在菱形ABCD中,AB=8,角D=60度,点F是CD的中点。点E是BC上一动点,连接AE、BF,点G、H分别是AE、BF的中点,连接GH,则GH的最小值是?......
  • 数据库和表的操作
    1、深入浅出了解数据库1、数据库1、存储结构逻辑存储结构:指的是数据库是由哪些性质的信息组成。所有与数据处理操作相关的信息都存储在数据库中物理存储结构:讨论数据库文件是如何在磁盘上存储的,数据库在磁盘上是以文件为单位存储的,由......
  • 蓝牙连接
    privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");privatevoidconnect(BluetoothDevicedevice){m_Device=device;BluetoothSockettmp=null;//GetaBluetoothSocketforaconn......
  • labview操作access和sql server数据库很实用的例子,含有创建表格,读写数据,按时间段读取,
    labview操作access和sqlserver数据库很实用的例子,含有创建表格,读写数据,按时间段读取,读取表格名称,还有部分sql指令操作等提供数据库软件,提供远程安装等ID:96128597927697257......
  • 数据库驱动异常:不接受的url异常
    不接受的url异常:Drivercom.mysql.jdbc.DriverclaimstonotacceptjdbcUrl,说明是yml文件的数据库url问题仔细检查url是否有编写错误即可。D:\softTool\Java\jdk1.8.0_281\bin\java.exe-XX:TieredStopAtLevel=1-noverify-Dspring.output.ansi.enabled=always"-javaagent:D......
  • MySQL的7种连接
    MySQL的七种连接刚学习Java和数据库,文章中讲的不对的地方,还请各位大神多多指教!本文参考链接(https://blog.csdn.net/qq_50596778/article/details/123145434)————————————————版权声明:本文为CSDN博主「NeverOW」的原创文章,遵循CC4.0BY-SA版权协议,转载请附......
  • 数据库转移到sd卡AsyncTask,ProgressDialog使用
    privateclassExportDatabaseFileTaskextendsAsyncTask<String,Void,Boolean>{privatefinalProgressDialogdialog=newProgressDialog(ctx);//canuseUIthreadhereprotectedvoidonPreExecute(){this.di......
  • 【数据库原理、编程与性能】The Relational Model
    ChapterTwo-TheRelationalModel文章目录ChapterTwo-TheRelationalModel1.CAP数据库2.RelationalAlgebra2.1集合运算(SetTheoreticOperations)2.1.1交(intersection:)2.1.2并(union:)2.1.3差(difference:)2.1.4笛卡尔积(production:)2.2专门的关系运算(NativeSpecialOperat......