6.Oracle DataSource and SingleTon:
import oracle.jdbc.pool.OracleDataSource;
import java.sql.Connection;
import java.sql.*;
public class OracleSingletonDataSource {
static private OracleDataSource ods;
private OracleSingletonDataSource() {
try{
ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@localhost:1521:qixy");
ods.setUser("scott");
ods.setPassword("tiger");
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception {
if (ods==null)
{
new OracleSingletonDataSource();
// ods.getConnection();
}
Connection con =null;
try {
con = ods.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
return con;
}
}
更多请见:http://www.mark-to-win.com/tutorial/java_10_OracleDataSourceandSingleTon.html