在开发过程中,代码的评审是对源代码进行系统性检查的过程,可以自我评审,组对互评,团队评审.

  这里我使用之前我们实训中我编写的一段数据库的操作类进行自评

package imp;

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

import bean.Admin;
import bean.Book;
import bean.Lend;
import bean.User;

import tools.ConnectionManager;
import dao.AdminDao;

public class AdminDaoImp implements AdminDao {

    public Connection con = null;
    PreparedStatement ps = null;
    public ResultSet rs = null;

    @Override
    public Admin login(String a_name, String a_password) {
        Admin admin = new Admin();
        con = ConnectionManager.getConnection();
        String sql = "select * from admin where a_username=? and a_password=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, a_name);
            ps.setString(2, a_password);
            rs = ps.executeQuery();
            if (rs.next() == false) {
                admin.setA_username("00000000");
            } else {
                admin.setA_username("a_name");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public int addbooks(Book book) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "insert into bookinfo(b_name,b_price,b_writer,b_press,b_type,b_location,b_barcoad) values(?,?,?,?,?,?,?)";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, book.getB_name());
            ps.setString(2, book.getB_price());
            ps.setString(3, book.getB_writer());
            ps.setString(4, book.getB_press());
            ps.setString(5, book.getB_type());
            ps.setString(6, book.getB_location());
            ps.setString(7, book.getB_barcoad());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<User> getAllUser() {
        List<User> list = new ArrayList<User>();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("select * from userinfo");
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setR_id(rs.getString("r_ID"));
                user.setR_name(rs.getString("r_name"));
                user.setR_sex(rs.getString("r_sex"));
                user.setR_occ(rs.getString("r_occ"));
                user.setR_IDnum(rs.getString("r_IDnum"));
                user.setTel(rs.getString("r_tel"));
                user.setR_mail(rs.getString("r_mail"));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Book> getAllBook() {
        List<Book> list = new ArrayList<Book>();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("select * from bookinfo");
            rs = ps.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setB_ID(rs.getString("b_ID"));
                book.setB_name(rs.getString("b_name"));
                book.setB_price(rs.getString("b_price"));
                book.setB_writer(rs.getString("b_writer"));
                book.setB_press(rs.getString("b_press"));
                book.setB_type(rs.getString("b_type"));
                book.setB_location(rs.getString("b_location"));
                book.setB_barcoad(rs.getString("b_barcoad"));
                list.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deletBook(String bid) {
        int result = 0;
        Connection connection = ConnectionManager.getConnection();
        String sql = "DELETE FROM bookinfo WHERE b_ID=?";

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, bid);
            result = preparedStatement.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Book getbook(String bid) {
        Book book = new Book();
        con = ConnectionManager.getConnection();
        String sql = "select * from bookinfo where b_ID=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, bid);
            rs = ps.executeQuery();
            while (rs.next()) {
                book.setB_ID(rs.getString("b_ID"));
                book.setB_name(rs.getString("b_name"));
                book.setB_price(rs.getString("b_price"));
                book.setB_writer(rs.getString("b_writer"));
                book.setB_press(rs.getString("b_press"));
                book.setB_type(rs.getString("b_type"));
                book.setB_location(rs.getString("b_location"));
                book.setB_barcoad(rs.getString("b_barcoad"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    @Override
    public int updatebook(String bid, Book book) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "UPDATE bookinfo SET b_name=?, b_price=?, b_writer=?, b_press=?, b_type=?, b_location=?, b_barcoad=? WHERE b_ID=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, book.getB_name());
            ps.setString(2, book.getB_price());
            ps.setString(3, book.getB_writer());
            ps.setString(4, book.getB_press());
            ps.setString(5, book.getB_type());
            ps.setString(6, book.getB_location());
            ps.setString(7, book.getB_barcoad());
            ps.setString(8, bid);
            result = ps.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int borrowBook(Lend lend) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "insert into lending(r_tel,b_barcoad,l_borrowtime,l_backtime,l_return,l_punishment) values(?,?,?,?,?,?)";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, lend.getR_tel());
            ps.setString(2, lend.getB_barcoad());
            ps.setString(3, lend.getL_borrowtime());
            ps.setString(4, lend.getL_backtime());
            ps.setString(5, lend.getL_return());
            ps.setString(6, lend.getL_punishment());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int returnbook(Lend lend) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "UPDATE lending SET l_return=? WHERE r_tel=? AND b_barcoad=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, 1);
            ps.setString(2, lend.getR_tel());
            ps.setString(3, lend.getB_barcoad());
            result = ps.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void overdueBook() {
        List<Lend> time = getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < time.size(); i++) {
            Lend lend = new Lend();
            lend = (Lend) time.get(i);
            try {
                Date date = new Date();
                String times = df.format(date);
                Date d1 = df.parse(lend.getL_backtime());
                Date d2 = df.parse(times);
                long diff = d1.getTime() - d2.getTime();
                long punishment = diff / (1000 * 60 * 60 * 24);
                if (punishment < 0) {
                    punishment(lend.getID());
                }
            } catch (Exception e) {
            }
        }
    }


    public void punishment(String ID) {
        con = ConnectionManager.getConnection();
        String sql = "UPDATE lending SET l_punishment=1 WHERE ID=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, ID);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Lend> getTime() {
        List<Lend> list = new ArrayList<Lend>();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("SELECT * FROM lending WHERE l_punishment = 0;");
            rs = ps.executeQuery();
            while (rs.next()) {
                Lend lend = new Lend();
                lend.setL_backtime(rs.getString("l_backtime"));
                lend.setID(rs.getString("ID"));
                list.add(lend);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Lend> getoverdue() {
        overdueBook();
        List<Lend> list = new ArrayList<Lend>();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("select * from lending WHERE l_punishment = 1;");
            rs = ps.executeQuery();
            while (rs.next()) {
                Lend lend = new Lend();
                lend.setR_tel(rs.getString("r_tel"));
                lend.setB_barcoad(rs.getString("b_barcoad"));
                lend.setL_backtime(rs.getString("l_backtime"));
                list.add(lend);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}

  在评审中,是要提出问题给代码的编写者,而不是在代码中直接修改.

  下面是我对我的这个类的Code Review.

  1. 在import里面,最好不要用通配符*.
  2. 已经定义了connection,preparedStatement的成员变量,在deletBook()方法中又重新定义,增加代码的复杂性.
  3. 在overduBook方法中Date的名子d1,d2太随意.
  4. 在类中没有注释,方法的功能不明确,参数值意义不清楚.