My Code Review
检查清单:
1.包名的以及类名的大小写
2.检查import中是否带有“*”,以及删除无用的import
3.避免对参数直接赋值
4.关键字大写
5.不要魔法数
@JAVA:
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.Book;
import bean.User;
import dao.BookDao;
public class BookDaoImp implements BookDao{
public Connection con = null;
public PreparedStatement ps = null;
public ResultSet rs = null;
public int addBook(Book book) {
// TODO Auto-generated method stub
int result = 0;
con = ConnectionManager.getConnection();
String sql = "insert into book(bid,bname,brest,bauthor) value(?,?,?,?)";
try {
ps = con.prepareStatement(sql);
ps.setString(1, book.getbId());
ps.setString(2, book.getbName());
ps.setString(3, book.getbRest());
ps.setString(4, book.getbAuthor());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public Book searchBook(String bid) {
// TODO Auto-generated method stub
Book book = new Book();
con = ConnectionManager.getConnection();
String sql = "select * from book where bid=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, bid);
rs = ps.executeQuery();
while (rs.next()) {
book.setbId(rs.getString("bid"));
book.setbName(rs.getString("bname"));
book.setbAuthor(rs.getString("bauthor"));
book.setbRest(rs.getString("brest"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
public int update(Book book) {
// TODO Auto-generated method stub
int result = 0;
con = ConnectionManager.getConnection();
String sql = "update book set bname=?, bauthor=?,brest=? where bid=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, book.getbName());
ps.setString(2, book.getbAuthor());
ps.setString(3, book.getbRest());
ps.setString(4, book.getbId());
result = ps.executeUpdate();
if (result != 0) {
result = 1;
} else {
result = 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public List getAllBook() {
// TODO Auto-generated method stub
con = ConnectionManager.getConnection();
String sql = "select*from book";
List<Book> list = new ArrayList<Book>();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setbId(rs.getString("bid"));
book.setbName(rs.getString("bname"));
book.setbAuthor(rs.getString("bauthor"));
book.setbRest(rs.getString("brest"));
System.out.println(rs.getString("bid") + rs.getString("bname"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public int delete(String bid) {
// TODO Auto-generated method stub
int result = 0;
con = ConnectionManager.getConnection();
String sql = "delete from book 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) {
e.printStackTrace();
}
return result;
}
public List mohuSearch(String bname) {
// TODO Auto-generated method stub
con = ConnectionManager.getConnection();
String sql = "select*from book where bname like '%" + bname + "%'";
List<Book> list = new ArrayList<Book>();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setbId(rs.getString("bid"));
book.setbName(rs.getString("bname"));
book.setbAuthor(rs.getString("bauthor"));
book.setbRest(rs.getString("brest"));
System.out.println(rs.getString("bid") + rs.getString("bname"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
修改的代码:
包名小写
package imp;// 包名小写
import java.sql.Connection;
import java.sql.PreparedStatement;
删除无用import
import tools.ConnectionManager;
import bean.Book;//删除无用import
import bean.User;//删除无用import
类名首字母,应该大写
public class BookDaoImp implements BookDao{//类名首字母,应该大写
/删除没有用的代码
// TODO Auto-generated method stub//删除没有用的代码
类名首字母,应该大写
public Book searchBook(String bid) {//类名首字母,应该大写,改为SearchBook
public int update(Book book) {//类名首字母,应该大写,改为UpDate
public int delete(String bid) {//类名首字母,应该大写,改为Delete
public List mohuSearch(String bname) {//类名首字母,应该大写,改为MohuSearch,并且最好都是英文单词,不是汉语拼音,改为Vageu