需要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;
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.*;
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();
}
}
}