• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}
}

}
##################################################
 
reply
    Bookmark Topic Watch Topic
  • New Topic