package store . dao ;
//省略包导入代码
public class CategoryDAO (
public List < Category > findCategories ( String name , Pager pager ) throws
Exception {
List < Category > list = new ArrayList <>();
Connection con = null ;
PreparedStatement ps = null ;
ResultSet rs = null ;
try {
Class . forName ( DB . JDBC _ DRIVER );
con = DriverManager . getConnection ( DB . JDBC _ URL , DB . JDBC _ USER ,
ps = con . prepareStatement (" select count ( id ) as total from t _ category
ps . setString (1,"%"+ name +"%");
rs = ps . executeQuery ();
if ( rs . next ()){
DB . JDBC _ PASSWORD );
where name like ?");
pager . setTotal ( rs . getInt (" total "));
(
ps = con . prepareStatement (" select * from t _ category where name
like ? limit ?,?");
ps . setString (1,"%"+ name +"%");
ps . setInt (2,( pager . getCurrentPage ()-1)* pager . getPageSize ());
ps . setInt (3, pager . getPageSize ());
rs = ps . executeQuery ();
while ( rs . next ())(
Category c = new Category ();
c . setId ( rs . getInt (" id "));
c . setName ( rs . getString (" name "));
c . setDescription ( rs . getString (" description "));
list . add ( c );
) catch ( Exception e ){
e . printStackTrace ();
throw new Exception ("数据库异常:"+ e . getMessage ());