[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2034
  • Last Modified:

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.?

0
Chakri25
Asked:
Chakri25
  • 2
  • 2
2 Solutions
 
bloodredsunCommented:
I think you will be able to if you implement DBCP from jakarta commons http://jakarta.apache.org/commons/dbcp/
0
 
Chakri25Author Commented:
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
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Hi,

DriverManager can be used to register and load JDBC drivers and then establish connections using those drivers. DataSource is typically a pre-configured object that you lookup using JNDI. However, it can be used in a standalong Java application as well. One feature common to DataSources that is missing from DriverManager is connection pooling. Each time you ask for a Connection from DriverManager, you get a new one. Since Connections are expensive to create, DataSource implementations often provide pooling of both physical and logical Connections. There is no API difference between a Connection returned using DriverManager and a Connection returned from a DataSource.

Here's a link to an example of how to manually instantiate and configure a DataSource for use in a standalong Java application:
    http://mckoi.com/database/maillist/msg04621.html

You would probably want to use this in a Singleton implementation so there could be a global point of access to a single instance of the DataSource.

Best regards,
Jim Cakalic
0
 
Chakri25Author Commented:
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?
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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);
    }
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now