Friday, December 14, 2007

creating sql server JDBC pool

Creating Sql Server 2005 JDBC connection pool in Glassfish v2

First we copy sqljdbc.jar file from the sql server jdbc driver distributed from Microsoft available here to the path \Program Files\glassfish-v2-b58g\lib.

Then we open the admin console. The snapshot: -


In resources->JDBC->Connection Pool

The main thing here: -

The name should not contain underscore or other special characters or it may not work

DataSource className: - com.microsoft.sqlserver.jdbc.SQLServerXADataSource

Resource Type: - javax.sql.XADataSource

In the Additional Properties the parameters should be correrct: -


check out the sql server 2005 port number from
Sql server Configuration Manager->Sql Server 2005 network configuration->Protocols for SqlExpress. If the TCP/IP is disabled, enable it.

Right click on TCP/IP -> properties

In the IP addresses tab, in IPAll see the parameter for TCP Dynamic Ports.

same should be in the portNumber in Glassfish server

Save the settings and test the connection by Ping button on the page.

create a JDBC resource using the JDBC connection pool: -


creating a connection to the JDBC resource (from a web page) using the code: -

try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("test");
Connection c = ds.getConnection();
Statement st = c.createStatement();
ResultSet rs = st.executeQuery("....");
.............


...............

c.close();
} catch(NamingException e1) {
label1.setText("Naming exception: " + e1);
} catch (SQLException e2) {
label1.setText("Sql exception: " + e2);
}

the imports should be: -

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

reply your comments.... thanks

Pradyut
http://pradyut.tk

http://pradyutb.googlepages.com/
http://oop-edge.blogspot.com/
http://oop-edge.spaces.live.com/
http://www.flickr.com/photos/praddy
http://groups.google.com/group/oop_programming
New Delhi, India

No comments: