Configuration file
<?xml version= "1.0" encoding= "UTF-8"?> <c3p0-config> <!--default configuration, only one occurrence of--> <default-config> &L t;!
--Connection Timeout Set 30 sec--> <property name= "Checkouttimeout" >3000</property> <!--30 seconds Check Connection idle--> <property name= "Idleconnectiontestperiod" >30</property> <!--initialized Pool size--> <property name= "Initia Lpoolsize ">2</property> <!--up to one connection idle time--> <property name=" MaxIdleTime ">30</prope Rty> <!--How many connections can be connection--> <property name= "maxpoolsize" >5</property> <!--the fewest pools have several connections --> <property name= "minpoolsize" >2</property> <!--batch statements--> <property name= "Maxstateme NTS ">50</property> <!--each grow several connections--> <property name=" acquireincrement ">3</property> <p Roperty name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Jdbcurl" > <! [cdata[jdbc:mysql://127.0.0.1:3306/bookstore?useunicode=true&characterencoding=utf-8]]> </property> <property name= "user" >root </property> <property name= "password" >root</property> </default-config> </c3p0-config >
Get Database Links
Import java.sql.Connection;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
public class Datasourceutils {
private static DataSource ds;
Declare ThreadLocal container object
private static threadlocal<connection> tl = new threadlocal<connection> ();
static {
ds =//default configuration
new Combopooleddatasource () in read c3p0-config.xml;
}
public static DataSource Getdatassource () {return
ds;
}
Provides a method to return Connection public
static Connection Getconn () {
Connection con = null;
First get data from tl
con = tl.get ();
if (con==null) {
try{
con = Getdatassource (). getconnection ();
Put to TL
tl.set (con);
catch (Exception e) {
e.printstacktrace ();
}
}
return con;
}
public static void Remove () {
tl.remove ();
}
}
Jsp
<TD align= "center" valign= "Top" style= "width:120px;" >
<a target= "Dataframe" href= "<c:url value= '/bookservlet '/>" > All </a><br/>
<c:foreach items= "${types}" var= "T" >
<a target= "Dataframe" href= "<c:url value= '/bookservlet?typeid=" ${t.id} '/> ' >${t.name}</a><br/>
</c:forEach>
Bookservlet
Private Bookservice service = new Bookservice ();
/**
* forward to books.jsp
* Query Some books
/@Override public
void Execute (httpservletrequest req, HttpServletResponse resp)
throws Exception {
String typeid=req.getparameter ("typeID");
Query
list<book> List = Service.query (typeid);
Encapsulated to req
req.setattribute ("list", list);
Forward to books.jsp
req.getrequestdispatcher ("/jsps/book/books.jsp"). Forward (req, resp);
public class Bookservice {
private Bookdao dao = new Bookdao ();
Public list<book> query (String typeid) {return
dao.query (typeid);
}
Import java.util.List;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Org.apache.commons.dbutils.handlers.BeanListHandler;
Import Cn.itcast.domain.Book;
Import Cn.itcast.utils.QueryRunner;
Import static cn.itcast.utils.datasourceutils.*;
public class Bookdao {
/**
* Query All Books
* parameter is type ID
/public list<book> query (String typeid) {
String sql = "SELECT * from books";
if (typeid!=null) {
sql = ' Select b.* from books b inner join BookType BT on B.id=bt.bookid ' +
' where bt.typeid= ' "+typeid+" ";
}
Queryrunner run = new Queryrunner (Getdatassource ());
list<book> list = Run.query (sql,new beanlisthandler<book> (Book.class));
return list;
}
Queryrunner
@Override public
<T> T query (String sql, resultsethandler<t> rsh) {
try {return
super.query ( SQL, rsh);
catch (SQLException e) {
throw new RuntimeException (E.getmessage (), E);
}
}
Jump JSP
<body style= "margin-top:0px;" >
below are all books <br/>
<c:foreach items= "${list}" var= "book" >
<div>
<a href= "< C:url value= '/bookservlet?cmd=detail&bookid=${book.id} '/> ' > ' >
</a>
<br/>
${book.name}
<c:choose>
<c:when test= "${book.rebate==1}" >
${book.price}
</c:when>
<c:otherwise>
<font style= " Text-decoration:line-through; " >
${book.price}
</font>
<fmt:formatnumber value= "${book.price*book.rebate}" pattern= "#,###.00"/>
</c:otherwise>
</c:choose>
yuan
</div>
</c: Foreach>
</body>