mysql增删改查

public class BookTypeDao {
  增加
public int bookTypeAdd(Connection con, BookType bookType) throws SQLException{ String sql="insert into t_bookType values(null,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, bookType.getBookTypeName()); pstmt.setString(2, bookType.getBookTypeDesc()); return pstmt.executeUpdate(); }
  查找
public ResultSet bookTypeList(Connection con,BookType bookType) throws SQLException{ StringBuffer sb=new StringBuffer("select * from t_bookType") ; if(StringUtil.isNotEmpty(bookType.getBookTypeName())){ sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'"); } PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where")); return pstmt.executeQuery(); }
  删除
public int bookTypeDelete(Connection con,String id) throws SQLException{ String sql="delete from t_bookType where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, id); return pstmt.executeUpdate(); }
  修改
public int bookTypeModify(Connection con,BookType bookType) throws SQLException{ String sql="update t_bookType set bookTypeName=?,bookTypeDesc=? where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, bookType.getBookTypeName()); pstmt.setString(2, bookType.getBookTypeDesc()); pstmt.setInt(3, bookType.getId()); return pstmt.executeUpdate(); } }

 

posted @ 2018-03-17 21:42  SnowsF  阅读(262)  评论(0编辑  收藏  举报