Forums Register Login

How to bind DataSource using context.bind. Connection pooling etc.

+Pie Number of slices to send: Send
Hi folks,
Please help me out here.

I created a simple java project in NetBean 7.0 and added jar file sqljdbc4.jar for MS SQl support.

I created a class where i have created a datasource (code below) for MQ SQL.
It connects database using the datasource and gets record count (504) successfully. (I have a Product table which has 504 records)

But it throws error when tried binding to Context. Could someone help me to suggest what should i enter to fill up the "???" in the code (red) below?

#####################Code##########################
package datasourcetest;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import java.sql.*;
import java.util.Hashtable;
/**
*
* @author admin
*/
public class dataSource {

public static void main(String [] args)
{

SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("admin-PC\\SQLEXPRESS");
ds.setPortNumber(1433);
ds.setUser("sa");
ds.setPassword("admin");

try{

System.out.println("PART 1");
Connection con = ds.getConnection();
if(con !=null)
{
System.out.println("Connection Success");
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from AdventureWorks.Production.Product") ;
while(rs.next())
{
int count = rs.getInt(1);
System.out.println("Total Record: "+ count);
}

/* Bind the DataSource to JNDI so that we can look for the datasource by the name given**/
System.out.println("PART 2");
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY," ??? ");
env.put(Context.PROVIDER_URL, " ??? ");
Context ctx = new InitialContext(env);
ctx.bind("jdbc/myDatasource",ds);

SQLServerDataSource myDs = (SQLServerDataSource)ctx.lookup("jdbc/myDatasource");

}
catch(SQLException se)
{
System.out.println("Failed PART 1");
}
catch(NamingException ne)
{
System.out.println("Failed PART 2");
ne.printStackTrace();

}
}

}
##################################################
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1248 times.
Similar Threads
WAS 6.1 JDBC problem
Getting DataSource object through JNDI in WebSphere 6.1
Getting DataSource object through JNDI in WebSphere 6.1
Oracle10gDataStoreHelper
Access Datasouce in java main method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:38:17.