Chakri25
asked on
Can u create a DATASOURCE object in a stand alone java applciation
Hi,
I have used WSAD to create a DATASOURCE to oracle using all the GUI which comes with WSAD, now i know DATASOURCE provided by javax.sql.datasource and the APP server uses Driver Manager internally and gives u a pool of connections which is bound to JNDI
Now presently i have eclipse and was just wondering if u could use DATASOURCE object in a public static void main program.?
I have used WSAD to create a DATASOURCE to oracle using all the GUI which comes with WSAD, now i know DATASOURCE provided by javax.sql.datasource and the APP server uses Driver Manager internally and gives u a pool of connections which is bound to JNDI
Now presently i have eclipse and was just wondering if u could use DATASOURCE object in a public static void main program.?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So in summary
for the standalone program mentioned in the above link we should put
//commons-pool.jar
// * commons-dbcp.jar
// * j2ee.jar (for the javax.sql classes)
in the classpath
and in WSAD when u configure the DATASOURCE, then WSAD internally has some jar files, which implement the connection pool.
right?
for the standalone program mentioned in the above link we should put
//commons-pool.jar
// * commons-dbcp.jar
// * j2ee.jar (for the javax.sql classes)
in the classpath
and in WSAD when u configure the DATASOURCE, then WSAD internally has some jar files, which implement the connection pool.
right?
On your project build path you'll need
commons-pool.jar
commons-dbcp.jar
j2ee.jar (for the javax.sql classes)
In your runtime classpath of your application you'll need
commons-collections.jar
commons-pool.jar
commons-dbcp.jar
j2ee.jar (for the javax.sql classes)
the classes for your JDBC driver
You won't configure a DataSource in WebSphere Studio. DataSource configuration is part of an application server configuration. You said you wanted to use DataSource to get connections in a standalone application -- no application server. So I would recommend something like this:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp.Ba sicDataSou rce;
public class DataSourceHelper {
private static BasicDataSource _dataSource;
static {
_dataSource = setup();
}
/**
* Private constructor to prevent instantiation.
*
private DataSourceHelper() {
}
/**
* Get a Connection from the configured DataSource.
*/
public static Connection getConnection() throws SQLException {
if (_dataSource == null) {
throw new IllegalStateException("Dat aSource not initialized");
}
return _dataSource.getConnection( );
}
/**
* Overloaded close methods implement an Exception-free mechanism
* for properly cleaning up JDBC resources.
*/
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// ignore
}
}
public static void close(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
// ignore
}
}
public static void close(ResultSet rset) {
try {
if (rset != null) {
rset.close();
}
} catch (Exception e) {
// ignore
}
}
public static void close(Connection conn, Statement stmt) {
close(stmt);
close(conn);
}
public static void close(Statement stmt, ResultSet rset) {
close(rset);
close(stmt);
}
public static void close(Connection conn, Statement stmt, ResultSet rset) {
close(rset);
close(stmt);
close(conn);
}
/**
* Configure the DataSource.
* Assumes a DataSourceHelper.propertie s file on the classpath with contents:
* classname=<fully-qualified classname of the JDBC driver>
* username=<database username>
* password=<database password>
* url=<database-specific URL for JDBC connection
*/
private static BasicDataSource setup() {
try {
Properties props = new Properties();
props.load(new FileInputStream("DataSourc eHelper.pr operties") );
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(prop s.getPrope rty("class name"));
ds.setUsername(props.getPr operty("us ername"));
ds.setPassword(props.getPr operty("pa ssword"));
ds.setUrl(props.getPropert y("url"));
return ds;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void shutdown() throws SQLException {
if (_dataSource != null) {
_dataSource.close();
}
}
}
This class provides a global access point to a Singleton DataSource. Configuration of the DataSource will occur when the class is loaded. It will look for a properties file on the classpath named DataSourceHelper.propertie s with contents as described in the javadoc. In your application you would use it like this:
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rset = null;
try {
conn = DataSourceHelper.getConnec tion();
stmt = conn.prepareStatement(...) ;
rset = stmt.executeQuery();
} catch (SQLException e) {
} finally {
DataSourceHelper.close(con n, stmt, rset);
}
commons-pool.jar
commons-dbcp.jar
j2ee.jar (for the javax.sql classes)
In your runtime classpath of your application you'll need
commons-collections.jar
commons-pool.jar
commons-dbcp.jar
j2ee.jar (for the javax.sql classes)
the classes for your JDBC driver
You won't configure a DataSource in WebSphere Studio. DataSource configuration is part of an application server configuration. You said you wanted to use DataSource to get connections in a standalone application -- no application server. So I would recommend something like this:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp.Ba
public class DataSourceHelper {
private static BasicDataSource _dataSource;
static {
_dataSource = setup();
}
/**
* Private constructor to prevent instantiation.
*
private DataSourceHelper() {
}
/**
* Get a Connection from the configured DataSource.
*/
public static Connection getConnection() throws SQLException {
if (_dataSource == null) {
throw new IllegalStateException("Dat
}
return _dataSource.getConnection(
}
/**
* Overloaded close methods implement an Exception-free mechanism
* for properly cleaning up JDBC resources.
*/
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// ignore
}
}
public static void close(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
// ignore
}
}
public static void close(ResultSet rset) {
try {
if (rset != null) {
rset.close();
}
} catch (Exception e) {
// ignore
}
}
public static void close(Connection conn, Statement stmt) {
close(stmt);
close(conn);
}
public static void close(Statement stmt, ResultSet rset) {
close(rset);
close(stmt);
}
public static void close(Connection conn, Statement stmt, ResultSet rset) {
close(rset);
close(stmt);
close(conn);
}
/**
* Configure the DataSource.
* Assumes a DataSourceHelper.propertie
* classname=<fully-qualified
* username=<database username>
* password=<database password>
* url=<database-specific URL for JDBC connection
*/
private static BasicDataSource setup() {
try {
Properties props = new Properties();
props.load(new FileInputStream("DataSourc
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(prop
ds.setUsername(props.getPr
ds.setPassword(props.getPr
ds.setUrl(props.getPropert
return ds;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void shutdown() throws SQLException {
if (_dataSource != null) {
_dataSource.close();
}
}
}
This class provides a global access point to a Singleton DataSource. Configuration of the DataSource will occur when the class is loaded. It will look for a properties file on the classpath named DataSourceHelper.propertie
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rset = null;
try {
conn = DataSourceHelper.getConnec
stmt = conn.prepareStatement(...)
rset = stmt.executeQuery();
} catch (SQLException e) {
} finally {
DataSourceHelper.close(con
}
ASKER