JDBC链接数据库版本三,使用C3P0,使用jar文件两个

JdbcUtil类:

[java] view plain copy
 
  1. package com.xiaohui.jdbc.util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import javax.sql.DataSource;  
  8. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  9. public final class JdbcUtil {  
  10.     private static ComboPooledDataSource dataSource;  
  11.     static {  
  12.         dataSource = new ComboPooledDataSource();  
  13.     }  
  14.     // 取得链接  
  15.     public static Connection getMySqlConnection() throws SQLException {  
  16.         return dataSource.getConnection();  
  17.     }  
  18.     //  
  19.     public static DataSource getDataSource(){  
  20.         return dataSource;  
  21.     }  
  22.   
  23.     // 关闭链接  
  24.     public static void close(Connection conn) throws SQLException {  
  25.         if (conn != null) {  
  26.             try {  
  27.                 conn.close();  
  28.             } catch (SQLException e) {  
  29.                 e.printStackTrace();  
  30.                 throw e;  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     public static void close(PreparedStatement pstate) throws SQLException {  
  36.         if(pstate!=null){  
  37.             pstate.close();  
  38.         }  
  39.     }  
  40.     public static void close(ResultSet rs) throws SQLException {  
  41.         if(rs!=null){  
  42.             rs.close();  
  43.         }  
  44.     }  
  45.       
  46. }  


c3p0-config.xml

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <c3p0-config>  
  3.     <default-config>  
  4.         <property name="driverClass">com.mysql.jdbc.Driver</property>  
  5.         <property name="user">root</property>  
  6.         <property name="password">root</property>  
  7.         <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mysql4</property>  
  8.     </default-config>  
  9. </c3p0-config>  


 

分页的一个dao:

[java] view plain copy
 
  1. package com.xiaohui.cusSys.dao;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.List;  
  5. import org.apache.commons.dbutils.QueryRunner;  
  6. import org.apache.commons.dbutils.handlers.BeanHandler;  
  7. import org.apache.commons.dbutils.handlers.BeanListHandler;  
  8. import org.apache.commons.dbutils.handlers.ScalarHandler;  
  9. import com.xiaohui.cusSys.domain.Customer;  
  10. import com.xiaohui.cusSys.util.JdbcUtil;  
  11.   
  12. public class Dao {  
  13.     private QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());  
  14.   
  15.     // 根据id返回 Customer 对象  
  16.     public Customer getCustomerById(int id) throws SQLException {  
  17.         String sql = "select * from customer where id = ?";  
  18.         Customer cus = (Customer) qr.query(sql,  
  19.                 new BeanHandler(Customer.class), id);  
  20.         return cus;  
  21.     }  
  22.   
  23.     // 分页返回  
  24.     public List<Customer> getFyList(int start, int size) throws SQLException {  
  25.         List<Customer> list = null;  
  26.         String sql = "select * from customer limit ?,?";  
  27.         list = qr.query(sql, new BeanListHandler(Customer.class), new Object[] {  
  28.                 start, size });  
  29.         return list;  
  30.     }  
  31.   
  32.     // 返回记录的总数目  
  33.     public int getAllRecordsCount() throws SQLException {  
  34.         String sql = "select count(*) from customer";  
  35.         Long temp = qr.query(sql, new ScalarHandler());  
  36.         return temp.intValue();  
  37.     }  
  38.   
  39.     // 根据ID删除指定的记录  
  40.     public void deleteRecordById(int id) throws SQLException {  
  41.         String sql = "delete from customer where id = ?";  
  42.         qr.update(sql, id);  
  43.     }  
  44.   
  45.     // 根据id更新记录信息  
  46.     public void updateRecordById(Customer newCus) throws SQLException {  
  47.         String sql = "update customer set name= ?,address= ?,tel= ?,mail= ?,birthday= ? where id= ?";  
  48.         qr.update(  
  49.                 sql,  
  50.                 new Object[] { newCus.getName(), newCus.getAddress(),  
  51.                         newCus.getTel(), newCus.getMail(),  
  52.                         newCus.getBirthday(), newCus.getId() });  
  53.     }  
  54.   
  55.     // 添加记录  
  56.     public void addRecord(Customer newCus) throws SQLException {  
  57.         String sql = "insert into customer(name,address,tel,mail,birthday) values(?,?,?,?,?)";  
  58.         qr.update(sql, new Object[] { newCus.getName(), newCus.getAddress(),  
  59.                 newCus.getTel(), newCus.getMail(),  
  60.                 // //将java.util.Date 转换为 java.sql.Date  
  61.                 // new java.sql.Date( newCus.getBirthday().getTime())  
  62.                 newCus.getBirthday() });  
  63.     }  
  64.   
  65. }  


 

jar文件:c3p0-0.9.1.2.jar (关键)  mysql-connector-java-5.1.22-bin.jar(关键) 在dao中 使用到commons-dbutils-1.5.jar

 
posted @ 2016-06-21 14:58  疯子110  阅读(2588)  评论(0编辑  收藏  举报