Mutual review

1 源代码

package imp;

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

import tools.ConnectionManager;

import bean.Books;
import dao.BooksDao;


public class BooksDaoImp implements BooksDao{
    public Connection con=null;
    public PreparedStatement ps=null;
    public ResultSet rs=null;
    public int addBooks(Books books) {
        // TODO Auto-generated method stub
        int result=0;
        con=ConnectionManager.getConnection();
        String sql="insert into books(bid,bname,bwriter,btype,bprice) values(?,?,?,?,?)";
        try{
            ps=con.prepareStatement(sql);
            ps.setString(1,books.getbId());
            ps.setString(2,books.getbName());
            ps.setString(3,books.getbWriter());
            ps.setString(4,books.getbType());
            ps.setString(5,books.getbPrice());
            
            result=ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return result;
    }

    public Books searchBooks(String bid) {
        // TODO Auto-generated method stub
        Books books = new Books();
        con=ConnectionManager.getConnection();
        String sql="select * from books where bid=?";
        try {
            ps=con.prepareStatement(sql);
            ps.setString(1, bid);
            rs=ps.executeQuery();
            while(rs.next()){
            System.out.println(rs.getString("bname"));    
            books.setbId(rs.getString("bid"));
            books.setbName(rs.getString("bname"));
            books.setbWriter(rs.getString("bwriter"));
            books.setbType(rs.getString("btype"));
            books.setbPrice(rs.getString("bprice"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
        return books;
    }

    public List getAllBooks() {
        // TODO Auto-generated method stub
        List list=new ArrayList();
        con=ConnectionManager.getConnection();
        String sql="select *from books";
        try{
            ps=con.prepareStatement(sql);
            rs=ps.executeQuery();
            
            while(rs.next()){
                Books books=new Books();
                books.setbId(rs.getString("bid"));
                books.setbName(rs.getString("bname"));
                books.setbWriter(rs.getString("bwriter"));
                books.setbType(rs.getString("btype"));
                books.setbPrice(rs.getString("bprice"));
                
                list.add(books);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return list;

    }

    public int deleteBooks(String bid) {
        // TODO Auto-generated method stub

        int result=0;
        con=ConnectionManager.getConnection();
        String sql="delete from bms.books where bid=?";
        try {
            ps=con.prepareStatement(sql);
            ps.setString(1, bid);
            result=ps.executeUpdate();//执行一个更新
            if(result!=0){
                result=1;
            }else{
                result=0;
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    

        return result;
    }

    public int updateBooks(String bid, Books books) {
        // TODO Auto-generated method stub
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "update books set bname=?, bwriter=?, btype=?, bprice=? where bid=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, books.getbName());
            ps.setString(2, books.getbWriter());
            ps.setString(3, books.getbType());
            ps.setString(4, books.getbPrice());
               ps.setString(5, bid);
            result = ps.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

1缺少关键的标注,解释

2与数据库的链接,更新数据库语句不怎么完善

3功能欠缺,不完善

原文地址:https://www.cnblogs.com/jxy5288/p/6612033.html