*
The moose likes JDBC and the fly likes How to bind DataSource using context.bind. Connection pooling etc. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to bind DataSource using context.bind. Connection pooling etc." Watch "How to bind DataSource using context.bind. Connection pooling etc." New topic
Author

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

M Das
Greenhorn

Joined: Aug 13, 2011
Posts: 2
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();

}
}

}
##################################################
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to bind DataSource using context.bind. Connection pooling etc.