<?xml version="1.0" encoding="UTF-8"?>
<!-- 需要导入c3p0驱动jar包和mysql驱动jar包 -->
<!-- c3p0-config.xml配置文件放在src包下 -->
<c3p0-config>
<named-config name="mysqlConnection">
<property name="user">root</property>
<property name="password"></property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property> <property name="initialPoolSize">20</property>
<property name="maxPoolSize">25</property>
<property name="minPoolSize">5</property>
</named-config>
</c3p0-config>
package cn.zr.testfilter.pojo;

public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} public User() {
super();
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
} }
package cn.zr.testfilter.Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.sql.DataSource; import cn.zr.testfilter.pojo.User; import com.mchange.v2.c3p0.ComboPooledDataSource; public class ConnectionUtils { public static void main(String[] args) {
ConnectionUtils utils = new ConnectionUtils();
List<User> list = utils.getData();
System.out.println(list);
} public List<User> getData(){
List<User> list = new ArrayList<User>();
Connection connection = ConnectionUtils.getConnection();
String sql = "SELECT NAME,AGE FROM USERTEST";
PreparedStatement pStatement = null;
ResultSet rSet = null;
try {
pStatement = (PreparedStatement) connection.prepareStatement(sql);
rSet = pStatement.executeQuery();
while (rSet.next()) {
User user = new User(rSet.getString("name"), rSet.getInt("age"));
list.add(user);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (rSet != null) {
rSet.close();
}
if (pStatement != null) {
pStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
} public static Connection getConnection() { DataSource ds = new ComboPooledDataSource("mysqlConnection");
try {
Connection connection = ds.getConnection();
return connection;
} catch (SQLException e) {
e.printStackTrace();
} return null; /*String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mysql";
String user = "root";
String password = "";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;*/ } }

Java c3p0 连接 MySQL的更多相关文章

  1. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  2. Java中连接MySql数据库的例子

    Java中连接MySql数据库的例子: package com.joinmysql.demo; import java.sql.DriverManager; import java.sql.Resul ...

  3. java jdbc 连接mysql 数据库

    JDBC连接MySQL 加载及注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); Class.forName("com. ...

  4. java怎么连接mysql数据库

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  5. java 项目连接MySQL数据库

    1.导入jar包 mysql-connector-java-5.1.35百度云链接如下: 链接:https://pan.baidu.com/s/1DPvIwU_An4MA3mU5bQa6VA 密码:5 ...

  6. java使用c3p0连接mysql,写中文数据乱码的问题

    此文说的乱码,是指所有中文的字符都变成了?. 首先,网上普遍搜索到的解决方案都是告诉你要在数据库连接字符串里面增加编码的定义,完整的连接字符串如下: url="jdbc:mysql://12 ...

  7. Java c3p0连接池

    import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; i ...

  8. java jdbc连接mysql

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  9. java实现连接mysql数据库单元测试查询数据项目分享

    1.按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model .db.dao.test;具体的架构详见下图: 2.根据搭建的项目架构新建数据库test和数据库表t_userinfo并且 ...

随机推荐

  1. MYSQL 重新设置自增值

    说明:当一张有auto_increment主键的表中存有10条数据,现删除5-10条数据, 再insert数据的时候auto_increment将从11开始. 现在我们改变它设为从6开始. ALTER ...

  2. Robot Framework接口测试(2)--http请求之get

    本来打算把http发送请求的get和post方法都介绍一下的,结果发现篇幅有点长,文本编辑也变得混乱,所以这里先介绍一下get方法,下一次再post.其实这些方法大家可以看一下源码里面的介绍只需要在代 ...

  3. 剑指offer-第四章解决面试题的思路(包含min函数的栈)

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数,在该栈中,调用min,push及pop的时间复杂度都是O(1) 思路:定义两个栈分别为dataStack和minStack ...

  4. Socket通信简单实例(WCF调用Socket)

    服务端: 控制台程序监听 /// <summary> /// Server /// </summary> class Program { static Socket serve ...

  5. NSArray倒序输出的方法

    NSMutableArray *array = [NSMutableArray arrayWithObjects:",nil]; NSArray* reversedArray = [[arr ...

  6. LA2572 Viva Confetti

    题意 PDF 分析 两两圆求交点,对每个圆弧按半径抖动. 时间复杂度\(O(T n^2)\) 代码 #include<iostream> #include<cstdio> #i ...

  7. php end()

    end()的用法

  8. 让Eclipse的TomcatPlugin支持Tomcat 8.x

     使用tomcat插件启动项目的优势: 1.TomcatPlugin是一个免重启的开发插件,原始的Servers方式启动tomcat项目,修改xxx.ftl  或者 xxx.jsp 文件后需要重启to ...

  9. java实现一个最简单的tomcat服务

    在了解tomcat的基本原理之前,首先要了解tomcatt最基本的运行原理. 1.如何启动? main方法是程序的入口,tomcat也不例外,查看tomcat源码,发现main是在Bootstrap  ...

  10. 【转】使用ant来调用Jmeter,并定制运行时参数

    为了应对不同的运行需求(主要是不同的线程数),以及可能的变化(host ip),在nongui运行时我对ant build.xml进行了一些修改 1. log目录备份与运行前清除 <tstamp ...