图书馆部分代码



package com.fangfa;


import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;


public class DAOBook {
    private static final String MYSQL_CON_URL ="jdbc:mysql://localhost:3306/tushuguan";
    private static final String USER = "****";
    private static final String PASSWORD = "****";
    
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("找不到驱动程序!");
        }
    }
    public static Connection getConnection(){
        Connection con = null;
        try {
            con = (Connection) DriverManager.getConnection(MYSQL_CON_URL,USER,PASSWORD);
        } catch (SQLException ex) {
            System.out.println("数据库连接失败!");
            ex.printStackTrace();
        }
        return con;
    }
    
    public Book find(String name){
        String sqlStr = "select * from bookxinxitianjia where bookid='" + name + "'";
        Book book = null;
        try(Connection con = getConnection();
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sqlStr)){
            while(rs.next()){
                book = new Book();
                book.setId(rs.getInt("bookid"));
                book.setTushubianhao(rs.getString("tushubianhao"));
                book.setTushumingcheng(rs.getString("tushumingcheng"));
                book.setZuozhe(rs.getString("zuozhe"));
                book.setYizhe(rs.getString("yizhe"));
                book.setJiage(rs.getString("jiage"));
                book.setTushuleibie_id(rs.getInt("tushuleibie_id"));
                book.setChubanshe_id(rs.getInt("chubanshe_id"));
                book.setDinggouxinshu_id(rs.getInt("dinggouxinshu_id"));
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return book;     
    }
    
    public void add(Book book){
        String sqlStr = "insert into bookxinxitianjia values(null,'" + book.getTushubianhao()+"','" + book.getTushumingcheng()  + "','" + book.getZuozhe()  + "','" + book.getYizhe()  + "','" + book.getJiage()  + "','" + book.getTushuleibie_id()  + "','" + book.getChubanshe_id()  + "','" + book.getDinggouxinshu_id() + "')";
        try(Connection con = getConnection();
                Statement stmt = con.createStatement();){
            int result = stmt.executeUpdate(sqlStr);
            if(result>0){
                JOptionPane.showMessageDialog(null, "添加成功!");
            }else{
               JOptionPane.showMessageDialog(null, "添加失败!");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    public void update(Book book){
        String sqlStr = "update bookxinxitianjia set tushumingcheng='" + book.getTushumingcheng() + "',zuozhe='" + book.getZuozhe() + "',yizhe='" + book.getYizhe() + "',jiage='" + book.getJiage() + "',tushuleibie_id='" + book.getTushuleibie_id() + "',chubanshe_id='" + book.getChubanshe_id() + "','2222-22-22' where tushubianhao=" + book.getTushubianhao();
        try(Connection con = getConnection();
                Statement stmt = con.createStatement();){
            int result = stmt.executeUpdate(sqlStr);
            if(result>0){
                JOptionPane.showMessageDialog(null, "修改成功!");
            }else{
               JOptionPane.showMessageDialog(null, "修改失败!");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    
    public void remove(Book book){
        String sqlStr = "delete from bookxinxitianjia where bookid=" + book.getId();
        try(Connection con = getConnection();
                Statement stmt = con.createStatement();){
            int result = stmt.executeUpdate(sqlStr);
            if(result>0){
                JOptionPane.showMessageDialog(null, "删除成功!");
            }else{
               JOptionPane.showMessageDialog(null, "删除失败!");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    public List<Book> getBooks(){
        List<Book> books = new ArrayList<>();
        String sqlStr = "select * from bookxinxitianjia";
        try(Connection con = getConnection();
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sqlStr)){
            while(rs.next()){
                Book book = new Book();
                book.setId(rs.getInt("bookid"));
                book.setTushubianhao(rs.getString("tushubianhao"));
                book.setTushumingcheng(rs.getString("tushumingcheng"));
                book.setZuozhe(rs.getString("zuozhe"));
                book.setYizhe(rs.getString("yizhe"));
                book.setJiage(rs.getString("jiage"));
                book.setTushuleibie_id(rs.getInt("tushuleibie_id"));
                book.setChubanshe_id(rs.getInt("chubanshe_id"));
                books.add(book);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return books;       
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值