Link to home
Start Free TrialLog in
Avatar of Chakri25
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.?

SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chakri25
Chakri25

ASKER

Does this mean than i don;t need a APP Server to use DATASOURCE? Cld u pls explain in detail the difference between DriverMgr and DataSource and what all you need for both
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.BasicDataSource;

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("DataSource 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.properties 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("DataSourceHelper.properties"));
            BasicDataSource ds = new BasicDataSource();
            ds.setDriverClassName(props.getProperty("classname"));
            ds.setUsername(props.getProperty("username"));
            ds.setPassword(props.getProperty("password"));
            ds.setUrl(props.getProperty("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.properties 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.getConnection();
        stmt = conn.prepareStatement(...);
        rset = stmt.executeQuery();
    } catch (SQLException e) {
    } finally {
        DataSourceHelper.close(conn, stmt, rset);
    }