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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM