JdbcUtil类:

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

c3p0-config.xml

  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:

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

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

 

JDBC链接数据库版本三,使用C3P0,使用jar文件两个的更多相关文章

  1. jdbc链接数据库的三种方式

    /** * jdbc连接数据库 * @author APPle * */ public class Demo1 { //连接数据库的URL private String url = "jdb ...

  2. 4、原生jdbc链接数据库常用资源名

    原生jdbc链接数据库要素:#MySql:String url="jdbc:mysql://localhost:3306/数据库名";String name="root& ...

  3. jdbc链接数据库,获取表名,字段名和数据

    import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import  ...

  4. 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

    原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...

  5. JDBC操作数据库的三种方式比较

    JDBC(java Database Connectivity)java数据库连接,是一种用于执行上sql语句的javaAPI,可以为多种关系型数据库提供统一访问接口.我们项目中经常用到的MySQL. ...

  6. JDBC链接数据库步骤

    java中定义链接数据库的标准:JDBC 1.导包:不同数据库有不同的jdbc驱动包,而且jdbc驱动包和数据库版本必须对应 2.测试 3.写代码 try { 1.//加载JDBC驱动    Clas ...

  7. jdbc链接数据库

    JDBC简介 JDBC全称为:Java Data Base Connectivity (java数据库连接),可以为多种数据库提供填统一的访问.JDBC是sun开发的一套数据库访问编程接口,是一种SQ ...

  8. Java JDBC链接数据库

     1.注册驱动Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动2.获取数据库连接java.sql.Connectio ...

  9. 1019 JDBC链接数据库进行修删改查

    package com.liu.test01; import java.sql.Statement; import java.sql.Connection; import java.sql.Drive ...

随机推荐

  1. [Android Pro] Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题

    参考博客: http://blog.csdn.net/t12x3456/article/details/9256609 http://blog.csdn.net/lihenair/article/de ...

  2. LINUX安全设置

    3. 为单用户引导加上密码   在“/etc/lilo.conf”文件中加入三个参数:time-out,restricted,password.这三个参数可以使你的系统在启动lilo时就要求密码验证. ...

  3. Android之jni入门

    jni即java native interface,使用jni我们可以在JAVA中调用C代码,提高了效率,可以复用代码,可以灵活的应用于各种场景 怎么使用JNI 安装软件 1.NDK 用于将C代码编译 ...

  4. JUC回顾之-volatile的原理和使用

    1.计算机内存模型的相关概念 计算机在执行程序时,每条指令都是在CPU中执行的,在指令的执行过程中,涉及到数据的读取和写入.由于程序在运行的过程中数据是放在"主存"中的, 由于数据 ...

  5. nginx增加ssl服务方法

    1.将申请到的ssl加密证书文件拷贝到nginx的conf目录下 如:server.pem.server.key 2.vim nginx.conf 例子: server { listen 443 ss ...

  6. hdu 4280 最大流sap

    模板套起来 1 5 7 //5个结点,7个边 3 3 //坐标 3 0 3 1 0 0 4 5 1 3 3 //相连的结点和流 2 3 4 2 4 3 1 5 6 4 5 3 1 4 4 3 4 2 ...

  7. Kinect学习笔记(五)——更专业的深度图

           这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用sdk里面封装好的功能来减少测量的误差,以及避免转换为灰度图时,出现绿巨人时候的掉针的bug.       下面直接 ...

  8. 【练习】ViewPager标签滑动

    效果图: 布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:a ...

  9. js:语言精髓笔记4----面向对象概要与运算符二义性

    实例创建:obj = new contructor[(arguments)]; //如果没有参数可以忽略括号:所以注意这不是函数调用: 直接量与初始器:在之前的基本表达式中将直接量与初始器分开,这时因 ...

  10. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...