<?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. HDU - 3374:String Problem (最小表示法模板题)

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  2. 【转载】Python正则表达式指南

    本文转自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#!comments 1. 正则表达式基础 1.1. 简单介绍 正则表达 ...

  3. 文件上传smart

    package com.bdqn.zhp.util; import java.text.SimpleDateFormat; import java.util.Date; import javax.se ...

  4. codechef Graph on a Table

    codechef Graph on a Table https://www.codechef.com/problems/TBGRAPH 题意 : 一个\(n\times m\)的网格图.\(q\) 个 ...

  5. 【模板】NOIP模板汇总

    图论 数据结构 数学 其他: 洛谷模板:a,b两个字符串,求b串在a串中出现的位置 #include<iostream> #include<cstdio> #include&l ...

  6. 老罗关于binder的链接

    Android进程间通信(IPC)机制Binder简要介绍和学习计划 : http://blog.csdn.net/luoshengyang/article/details/6618363

  7. Linux多网卡的时候执行机器Ip

    在Linux部署的时候,经常会有多网卡的情况出现,这时候项目又需要指定Ip.在这种情况下,要配置linux机子的host,指定里头要使用的ip地址,否则linux机子不知道去找哪个ip. 一.查看本机 ...

  8. 两种方式创建Maven项目【方式二】

    1.不勾选Create a simple project,直接点击下一步 2.选择maven-archetype-webapp下一步 3.填写相关信息,点击下一步完成 4.此时会报一个jsp的错误.我 ...

  9. 平均分割list

    private static List<List<Integer>> splitList(List<Integer> lists,int limit){ int s ...

  10. C#去除数组空格

    static void Main(string[] args) { "}; Console.WriteLine("输出带有空字符串的数组:"); foreach (str ...