Jdbcutil class:
Package com.xiaohui.jdbc.util;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
Public final class Jdbcutil {private static Combopooleddatasource DataSource;
static {DataSource = new Combopooleddatasource ();
}//Get link public static Connection getmysqlconnection () throws SQLException {return datasource.getconnection ();
}//public static DataSource Getdatasource () {return DataSource; }//close link public static void close (Connection conn) throws SQLException {if (conn! = null) {try {Conn.close
();
} catch (SQLException e) {e.printstacktrace ();
Throw e;
}}} public static void Close (PreparedStatement pstate) throws SQLException {if (pstate!=null) {pstate.close ();
}} public static void Close (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= "Driverclass" >com.mysql.jdbc.Driver</property>
<property name= "user" >root</property >
<property name= "password" >root</property>
<property name= "Jdbcurl" >jdbc:mysql:// 127.0.0.1:3306/mysql4</property>
</default-config>
</c3p0-config>
one DAO for pagination:
Package Com.xiaohui.cusSys.dao;
Import java.sql.SQLException;
Import java.util.List;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Org.apache.commons.dbutils.handlers.BeanListHandler;
Import Org.apache.commons.dbutils.handlers.ScalarHandler;
Import Com.xiaohui.cusSys.domain.Customer;
Import Com.xiaohui.cusSys.util.JdbcUtil;
public class Dao {private Queryrunner QR = new Queryrunner (Jdbcutil.getdatasource ()); Returns the Customer object based on the ID public customer Getcustomerbyid (int id) throws SQLException {String sql = "SELECT * FROM Custome
r WHERE id =? ";
Customer cus = (customer) qr.query (SQL, New Beanhandler (customer.class), id);
return cus; }//Pagination returns public list<customer> getfylist (int start, int size) throws SQLException {list<customer> List
= NULL;
String sql = "SELECT * FROM Customer limit?,?";
List = Qr.query (sql, New Beanlisthandler (Customer.class), new object[] {start, size}); ReTurn list;
}//Returns the total number of records public int getallrecordscount () throws SQLException {String sql = "SELECT COUNT (*) from customer";
Long temp = qr.query (sql, New Scalarhandler ());
return Temp.intvalue (); }//Delete the specified record according to ID public void Deleterecordbyid (int id) throws SQLException {String sql = "Delete from customer where
id =? ";
Qr.update (SQL, id); }//update record information based on ID public void Updaterecordbyid (Customer newcus) throws SQLException {String sql = "Update Customer set Name=, address=?, tel=?, mail=?, birthday=?
where id=? ";
Qr.update (SQL, new object[] {newcus.getname (), newcus.getaddress (), Newcus.gettel (), Newcus.getmail (),
Newcus.getbirthday (), Newcus.getid ()}); }//Add record public void AddRecord (Customer newcus) throws SQLException {String sql = "INSERT INTO Customer (name,addres
S,tel,mail,birthday) VALUES (?,?,?,?,?) "; Qr.update (SQL, new object[] {newcus.getname (), newcus.getaddress (), Newcus.gettel (), Newcus.getmail (),////Will JAVA.util.date converted to Java.sql.Date//New Java.sql.Date (Newcus.getbirthday (). GetTime ()) Newcus.getbirthday ()});
}
}
jar file:c3p0-0.9.1.2.jar (key) Mysql-connector-java-5.1.22-bin.jar (critical) in DAO Use to Commons-dbutils-1.5.jar