轉自: http://lavasoft.blog.51cto.com/62575/212616/
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="driverUrl">
<value>jdbc:oracle:thin:@192.168.104.192:1521:tim</value>
</property>
<property name="user" value="tim"/>
<property name="password" value="tim_8968888"/>
<property name="alias" value="Pool_dbname"/>
<property name="maximumActiveTime" value="300000"/>
<property name="prototypeCount" value="0"/>
<property name="maximumConnectionCount" value="50"/>
<property name="minimumConnectionCount" value="2"/>
<property name="simultaneousBuildThrottle" value="50"/>
<property name="houseKeepingTestSql" value="select CURRENT_DATE"/>
</bean>
</beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-10-15 10:21:38
*/
public class ApplicationContextUtil {
private static ApplicationContext applicationContext;
static {
if (applicationContext == null)
applicationContext = rebuildApplicationContext();
}
/**
* 重新構建Spring應用上下文環境
*
* @return ApplicationContext
*/
public static ApplicationContext rebuildApplicationContext() {
return new ClassPathXmlApplicationContext("/syndsconfig.xml");
}
/**
* 獲取Spring應用上下文環境
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 簡單的上下文環境測試
*/
public static void main(String[] args) {
rebuildApplicationContext();
if (applicationContext == null) {
System.out.println("ApplicationContext is null");
} else {
System.out.println("ApplicationContext is not null!");
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-10-10 17:59:47
*/
public class TestProxool {
public static String dburl = "jdbc:oracle:thin:@192.168.104.192:1521:tim";
public static String user = "tim";
public static String password = "tim_8968888";
/**
* JDBC方式測試
*
* @throws Exception
*/
public static void test1() throws Exception {
String testsql = "select * from village t where lastid = 346";
//1:註冊驅動類
Class.forName("oracle.jdbc.driver.OracleDriver");
//2:創建數據庫連接
// Connection conn = DriverManager.getConnection(dburl, user, password);
DataSource ds = (DataSource) ApplicationContextUtil.getApplicationContext().getBean("dataSource");
Connection conn = ds.getConnection();
//3:創建執行SQL的對象
Statement stmt = conn.createStatement();
//4:執行SQL,並獲取返回結果
ResultSet rs = stmt.executeQuery(testsql);
//5:處理返回結果,此處打印查詢結果
while (rs.next()) {
System.out.print(rs.getLong("id") + "\t");
System.out.print(rs.getString("name") + "\t");
System.out.println();
}
//6:關閉數據庫連接
conn.close();
}
public static void main(String[] args) throws Exception {
test1();
}
}
2009-10-15 12:37:03 - INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [syndsconfig.xml]
2009-10-15 12:37:04 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=9737354]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy
2009-10-15 12:37:04 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - 1 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=9737354]
2009-10-15 12:37:04 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@10655dd]
2009-10-15 12:37:04 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default[org.springframework.context.event.SimpleApplicationEventMulticaster@c8f6f8]
2009-10-15 12:37:04 - INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy]
5411 張一村
5412 張二村
5413 張三村
5414 張四村
5415 南原村
5416 辛莊村
5417 凡村
5418 西陽村
5419 人馬村
5420 前關村
5421 後關村
5422 趙村
5423 水淆村
5424 溝東村
5425 陳村
5426 窯店村
5427 坡頭村
20588 大安頭
20589 澗裏村
20590 人馬寨
20591 白草村
20592 窯院村
20593 寺下村
20594 反上村
33651 小安頭
33652 五花嶺
33653 東溝
33654 西溝
33655 南溝
33656 王村
33657 營前
33659 東陽
33661 太陽
33663 豐陽
33665 宜村
33667 窯頭
32225 石原村
32226 廟上村
32227 廟窪
38739 丁管營
38841 澗西
2009-10-15 12:37:05 - INFO org.logicalcobwebs.proxool.Pool_dbname - Shutting down 'Pool_dbname' pool immediately [Shutdown Hook]
2009-10-15 12:37:05 - INFO org.logicalcobwebs.proxool.PrototyperController - Stopping Prototyper thread
2009-10-15 12:37:05 - INFO org.logicalcobwebs.proxool.HouseKeeperController - Stopping HouseKeeper thread
Process finished with exit code 0
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="driverUrl">
<value>jdbc:oracle:thin:@192.168.104.192:1521:tim</value>
</property>
<property name="user" value="tim"/>
<property name="password" value="tim_8968888"/>
<property name="alias" value="proxool.a2"/>
<property name="maximumActiveTime" value="300000"/>
<property name="prototypeCount" value="0"/>
<property name="maximumConnectionCount" value="50"/>
<property name="minimumConnectionCount" value="2"/>
<property name="simultaneousBuildThrottle" value="50"/>
<property name="houseKeepingTestSql" value="select CURRENT_DATE"/>
</bean>
<bean id="dataSource2"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="driverUrl">
<value>jdbc:oracle:thin:@192.168.104.164:1521:orcl</value>
</property>
<property name="user" value="rural"/>
<property name="password" value="rural"/>
<property name="alias" value="proxool.a1"/>
<property name="maximumActiveTime" value="300000"/>
<property name="prototypeCount" value="0"/>
<property name="maximumConnectionCount" value="50"/>
<property name="minimumConnectionCount" value="2"/>
<property name="simultaneousBuildThrottle" value="50"/>
<property name="houseKeepingTestSql" value="select CURRENT_DATE"/>
</bean>
</beans>
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2009-10-15 17:39:50
*/
public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = ApplicationContextUtil.getApplicationContext();
DataSource ds1 = (DataSource)ctx.getBean("dataSource");
DataSource ds2 = (DataSource)ctx.getBean("dataSource2");
Connection conn1 = ds1.getConnection();
Connection conn2 = ds2.getConnection();
Statement stmt = conn1.createStatement();
//4:執行SQL,並獲取返回結果
ResultSet rs = stmt.executeQuery("select * from city");
//5:處理返回結果,此處打印查詢結果
while (rs.next()) {
System.out.print(rs.getLong("id") + "\t");
System.out.print(rs.getString("name") + "\t");
System.out.println();
Statement stmt2 = conn2.createStatement();
//4:執行SQL,並獲取返回結果
// ResultSet rs2 = stmt2.executeQuery("select * from city");
ResultSet rs2 = stmt2.executeQuery("select * from lm where lm_id = "+rs.getLong("id"));
//5:處理返回結果,此處打印查詢結果
while (rs2.next()) {
System.out.println(rs2.getLong(1));
}
System.out.println("<<<<");
//6:關閉數據庫連接
}
//6:關閉數據庫連接
conn1.close();
conn2.close();
// System.out.println("----------");
//
// Statement stmt2 = conn2.createStatement();
// //4:執行SQL,並獲取返回結果
// ResultSet rs2 = stmt2.executeQuery("select count(*) from lm");
// //5:處理返回結果,此處打印查詢結果
// while (rs2.next()) {
// System.out.println(rs2.getLong(1));
// }
// //6:關閉數據庫連接
// conn2.close();
}
}
2009-10-15 18:25:37 - INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [syndsconfig.xml]
2009-10-15 18:25:37 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=6161922]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,dataSource2]; root of BeanFactory hierarchy
2009-10-15 18:25:37 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - 2 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=6161922]
2009-10-15 18:25:37 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@18dfef8]
2009-10-15 18:25:37 - INFO org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default[org.springframework.context.event.SimpleApplicationEventMulticaster@134bed0]
2009-10-15 18:25:37 - INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,dataSource2]; root of BeanFactory hierarchy]
410100 鄭州市
<<<<
410200 開封市
<<<<
410300 洛陽市
<<<<
410400 平頂山市
<<<<
410500 安陽市
<<<<
410600 鶴壁市
<<<<
410700 新鄉市
<<<<
410800 焦作市
<<<<
410881 濟源市
<<<<
410900 濮陽市
<<<<
411000 許昌市
<<<<
411100 漯河市
<<<<
411200 三門峽市
<<<<
411300 南陽市
<<<<
411400 商丘市
<<<<
411500 信陽市
<<<<
411600 周口市
<<<<
411700 駐馬店市
<<<<
2009-10-15 18:25:39 - INFO org.logicalcobwebs.proxool.proxool.a2 - Shutting down 'proxool.a2' pool immediately [Shutdown Hook]
2009-10-15 18:25:39 - INFO org.logicalcobwebs.proxool.proxool.a1 - Shutting down 'proxool.a1' pool immediately [Shutdown Hook]
2009-10-15 18:25:39 - INFO org.logicalcobwebs.proxool.PrototyperController - Stopping Prototyper thread
2009-10-15 18:25:39 - INFO org.logicalcobwebs.proxool.HouseKeeperController - Stopping HouseKeeper thread
Process finished with exit code 0