日期:2014-05-16 浏览次数:20552 次
需要jar包:
commons-validator.jar
commons-dbcp-1.3.jar
commons-beanutils.jar
commons-pool-1.5.2.jar
commons-logging.jar
ojdbc14.jar
?
?
?
package org.liujian.test.db;
import org.apache.commons.dbcp.BasicDataSource;
/**
?* File Desc: dataSource类
?* Product Name: SIRM
?* Module Name: BaseDase
?* Author:????? 刘建
?* History:???? 11-12-10 created by 刘建
?*/
public class DataSource {
??? private BasicDataSource dataSource;
??? public static BasicDataSource getDataSource() {
??????? BasicDataSource basicDataSource = new BasicDataSource();
??????? basicDataSource.setDefaultAutoCommit(true);
??????? basicDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
??????? basicDataSource.setUrl("jdbc:oracle:thin:@192.168.20.160:1521:orcl");
??????? basicDataSource.setUsername("sirmetltest");
??????? basicDataSource.setPassword("sirmetltest");
??????? return basicDataSource;
??? }
}
?
package org.liujian.test.db;
import java.sql.*;
/**
?* File Desc:
?* Product Name: SIRM
?* Module Name: BaseDase
?* Author:????? 刘建
?* History:???? 11-12-10 created by 刘建
?*/
public class DataSourceFactory {
??? public static void main(String[] args) {
??????? try {
??????????? // Class.forName("oracle.jdbc.driver.OracleDriver");
??????????? // Connection ct = DriverManager.getConnection("jdbc:oracle:thin:@192.168.20.160:1521:orcl", "sirmetltest", "sirmetltest");
??????????? Connection ct = DataSource.getDataSource().getConnection();
??????????? Statement statement = ct.createStatement();
??????????? ResultSet rs = statement.executeQuery("select * from r_database");
??????????? while (rs.next()) {
??????????????? System.out.println(rs.getString("name"));
??????????? }
??????????? rs.close();
??????????? statement.close();
??????????? ct.close();
??????? } catch (Exception ex) {
??????????? ex.printStackTrace();
??????? }
??? }
}