web层代码如下:
package beyondwsq. web; import java. io. IOException;
import java. sql. SQLException;
import java. util. List; import javax. servlet. ServletException;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse; import beyondwsq. domain. Product;
import beyondwsq. service. AdminProductService; public class AdminProductListServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AdminProductService service = new AdminProductService ( ) ; List< Product> productList = null; try { productList = service. findAllProduct ( ) ; } catch ( SQLException e) { e. printStackTrace ( ) ; } request. setAttribute ( "productList" , productList) ; request. getRequestDispatcher ( "/admin/product/list.jsp" ) . forward ( request, response) ; ; } public void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet ( request, response) ; }
}
service层方法如下:
package beyondwsq. service; import java. sql. SQLException;
import java. util. List; import beyondwsq. dao. AdminProductDao;
import beyondwsq. domain. Product; public class AdminProductService { public List< Product> findAllProduct ( ) throws SQLException { AdminProductDao dao = new AdminProductDao ( ) ; return dao. findAllProduct ( ) ; } }
dao层代码如下:
package beyondwsq. dao; import java. sql. SQLException;
import java. util. List; import javax. activation. DataSource; import org. apache. commons. dbutils. QueryRunner;
import org. apache. commons. dbutils. handlers. BeanListHandler;
import org. apache. taglibs. standard. tag. common. sql. DataSourceUtil; import beyondwsq. domain. Product;
import beyondwsq. utils. DataSourceUtils; public class AdminProductDao { public List< Product> findAllProduct ( ) throws SQLException { QueryRunner runner = new QueryRunner ( DataSourceUtils. getDataSource ( ) ) ; String sql = "select *from product" ; List< Product> productList = runner. query ( sql, new BeanListHandler < Product> ( Product. class ) ) ; return productList; } }
domain代码:
package beyondwsq. domain; public class Product { private String pid; private String pname; private double market_price; private double shop_price; private String pimage; private String pdate; private int is_hot; private String pdesc; private int pflag; public String getPid ( ) { return pid; } public void setPid ( String pid) { this . pid = pid; } public String getPname ( ) { return pname; } public void setPname ( String pname) { this . pname = pname; } public double getMarket_price ( ) { return market_price; } public void setMarket_price ( double market_price) { this . market_price = market_price; } public double getShop_price ( ) { return shop_price; } public void setShop_price ( double shop_price) { this . shop_price = shop_price; } public String getPimage ( ) { return pimage; } public void setPimage ( String pimage) { this . pimage = pimage; } public String getPdate ( ) { return pdate; } public void setPdate ( String pdate) { this . pdate = pdate; } public int getIs_hot ( ) { return is_hot; } public void setIs_hot ( int is_hot) { this . is_hot = is_hot; } public String getPdesc ( ) { return pdesc; } public void setPdesc ( String pdesc) { this . pdesc = pdesc; } public int getPflag ( ) { return pflag; } public void setPflag ( int pflag) { this . pflag = pflag; } public String getCid ( ) { return cid; } public void setCid ( String cid) { this . cid = cid; } private String cid;
}
utils代码如下:
package beyondwsq. utils; import java. sql. Connection;
import java. sql. ResultSet;
import java. sql. SQLException;
import java. sql. Statement; import javax. sql. DataSource; import com. mchange. v2. c3p0. ComboPooledDataSource; public class DataSourceUtils { private static DataSource dataSource = new ComboPooledDataSource ( ) ; private static ThreadLocal< Connection> tl = new ThreadLocal < Connection> ( ) ; public static DataSource getDataSource ( ) { return dataSource; } public static Connection getConnection ( ) throws SQLException{ return dataSource. getConnection ( ) ; } public static Connection getCurrentConnection ( ) throws SQLException { Connection con = tl. get ( ) ; if ( con == null) { con = dataSource. getConnection ( ) ; tl. set ( con) ; } return con; } public static void startTransaction ( ) throws SQLException { Connection con = getCurrentConnection ( ) ; if ( con != null) { con. setAutoCommit ( false ) ; } } public static void rollback ( ) throws SQLException { Connection con = getCurrentConnection ( ) ; if ( con != null) { con. rollback ( ) ; } } public static void commitAndRelease ( ) throws SQLException { Connection con = getCurrentConnection ( ) ; if ( con != null) { con. commit ( ) ; con. close ( ) ; tl. remove ( ) ; } } public static void closeConnection ( ) throws SQLException { Connection con = getCurrentConnection ( ) ; if ( con != null) { con. close ( ) ; } } public static void closeStatement ( Statement st) throws SQLException { if ( st != null) { st. close ( ) ; } } public static void closeResultSet ( ResultSet rs) throws SQLException { if ( rs != null) { rs. close ( ) ; } }
}
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
< c3p0-config> < default-config> < property name = " user" > root</ property> < property name = " password" > wsq</ property> < property name = " driverClass" > com.mysql.jdbc.Driver</ property> < property name = " jdbcUrl" > jdbc:mysql:///web17</ property> </ default-config>
</ c3p0-config>