File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Getting pooled connection using DSN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Getting pooled connection using DSN" Watch "Getting pooled connection using DSN" New topic

Getting pooled connection using DSN

Shitij Bhargava

Joined: Jun 17, 2010
Posts: 10
Hi !

I am making a connection class in which a method getConnection(dsn,username,password) is there, which returns a connection object.

See the following code, it works : (both the commented and the uncommented ways to get connection). The commented way gets an unpooled connection, but uses the dsn so I dont have to hardcode anything (but I want connection pooling) and if I use the uncommented way, I get a pooled connection but, I have to hardcode the server name and Database name. I dont want to hardcode these. I want to get them through the dsn I've made.

I know I cant access dsn info from java code directly (without using jni probably), so I tried to use the setURL() method in the following way:
(this is my attempt to do what I want, but it doesnt work)

But this gives SQLException saying: The connection string contains a badly formed name or value.
The only thing I can infer from this is that the url in DriverManager.getConnection(String url) and the url in SQLServerConnectionPooledDataSource#setURL(String url) are different in some way.
Then what should I put as url here in setURL() ?? (such a url which uses the dsn name, and I dont have to hardcode server and database name)

Or can you tell some other way to do what I want to do ?
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 195

You can try with the DBCP api. Hopefully it works.

DBCP Example

I agree. Here's the link:
subject: Getting pooled connection using DSN
It's not a secret anymore!