Jdbcutil class:
[Java]View PlainCopy
- 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 links
- 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
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="UTF-8"?>
- <c3p0-config>
- <default-config>
- < name="Driverclass">com.mysql.jdbc.driver</ Property >
- < name="user">root </Property>
- < name="password">root </Property>
- < name="Jdbcurl">jdbc:mysql://127.0.0.1:3306/mysql4</ Property >
- </default-config>
- </c3p0-config>
One DAO for pagination:
[Java]View PlainCopy
- 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 ());
- //Return Customer object by ID
- Public Customer Getcustomerbyid (int id) throws SQLException {
- String sql = "SELECT * FROM customer where id =?";
- Customer cus = (customer) qr.query (SQL,
- New Beanhandler (Customer. Class), id);
- return cus;
- }
- //Paging back
- 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 records by ID
- public void Deleterecordbyid (int id) throws SQLException {
- String sql = "Delete from customer where id =?";
- Qr.update (SQL, id);
- }
- //update record information by 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 a record
- public void AddRecord (Customer newcus) throws SQLException {
- String sql = "INSERT into customer (Name,address,tel,mail,birthday) VALUES (?,?,?,?,?)";
- Qr.update (SQL, new object[] {newcus.getname (), newcus.getaddress (),
- Newcus.gettel (), Newcus.getmail (),
- //// convert java.util.Date to Java.sql.Date
- //New Java.sql.Date (Newcus.getbirthday (). GetTime ())
- Newcus.getbirthday ()});
- }
- }
Jar file: C3p0-0.9.1.2.jar (Critical) Mysql-connector-java-5.1.22-bin.jar (critical) used in DAO to Commons-dbutils-1.5.jar
-
Top
JDBC Link database version three, using C3P0, using jar files of two