导入jar包:proxool-0.9.1.jar 和 proxool-cglib.jar
一、使用硬编码方式
package com.proxool.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Test1 {
private static final Log LOG = LogFactory.getLog(Test1.class);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connection = null;
try {
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
try {
// connection = DriverManager.getConnection("jdbc:mysql://172.16.220.154:3306/sipityprofile?autoReconnect=true&useUnicode=true&characterEncoding=utf8&user=root&password=12345678");
connection = DriverManager.getConnection("proxool.example:com.mysql.jdbc.Driver:jdbc:mysql://172.16.220.154:3306/sipityprofile?user=root&password=12345678");
// connection = DriverManager.getConnection("proxool.example");
} catch (SQLException e) {
LOG.error("Problem getting connection", e);
}
if (connection != null) {
LOG.info("Got connection :)");
} else {
LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
}
} catch (ClassNotFoundException e) {
LOG.error("Couldn't find driver", e);
} finally {
try {
// Check to see we actually got a connection before we
// attempt to close it.
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
LOG.error("Problem closing connection", e);
}
}
}
}
二、使用 xml 配置文件
package com.proxool.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
public class Test2ByXml {
private static final Log LOG = LogFactory.getLog(Test2ByXml.class);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
JAXPConfigurator.configure("src/proxool.xml", false);
Connection connection = DriverManager.getConnection("proxool.proxool-sipity");
if (null == connection) {
LOG.info("connection is null");
}else {
LOG.info("connection successfull");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from event");
while (resultSet.next()) {
LOG.info(resultSet.getInt(1)+" "+resultSet.getString(2));
}
}
} catch (ProxoolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<something-else-entirely>
<proxool>
<alias>proxool-sipity</alias>
<driver-url>jdbc:mysql://172.16.220.154:3306/sipityprofile</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="12345678" />
</driver-properties>
<maximum-connection-count>10</maximum-connection-count>
<house-keeping-test-sql>select 1</house-keeping-test-sql>
</proxool>
</something-else-entirely>
三、使用 properties 方式
package com.proxool.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
public class Test3ByProp {
private static final Log LOG = LogFactory.getLog(Test3ByProp.class);
static{
try {
PropertyConfigurator.configure("src/proxool.properties");
} catch (ProxoolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
testConnection();
testConnection();
}
private static void testConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection("proxool.proxool-sipity");
if (null == connection) {
LOG.info("connection is null");
}else {
LOG.info("connection successfull");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from event");
while (resultSet.next()) {
//LOG.info(resultSet.getInt(1)+" "+resultSet.getString(2));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
jdbc-0.proxool.alias=proxool-sipity
jdbc-0.proxool.driver-url=jdbc:mysql://172.16.220.154:3306/sipityprofile
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.user=root
jdbc-0.password=12345678
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.house-keeping-test-sql=select 1