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 Dynamic DSN creation 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 "Dynamic DSN creation" Watch "Dynamic DSN creation" New topic

Dynamic DSN creation

abhinav singhal

Joined: Jun 19, 2003
Posts: 29
Hi All,
I want to Dynamically generate the DSN for database connectivity, so that i do not need to go to ODBC and create DSN for the database.
Is there any way by which we can create the DSN for a particular database in our code itself and then use it for querrying.

Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
The DSN for a Windows ODBC connection is held in the registry. You can export a specific DSN into a .reg file which you can then reload into the registry.

It's possible that you could use a variation of that technique to create the DSN. Of course your code would have to have permission to run on each relevant machine.

Another alternative may be to use an API to register the DSN in the registry.

Hope this helps.

Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64

You can use system dsn,so that any user of the system can use it.Else,create file dsn and you are not needed to create every time,just set the location of the file dsn.

Dsn file basically contains the location of the driver name,location,datasource name,username etc.

If you do not want dsn to be used,go for type 2 or type 4 Driver.There will be no need to craete dsn.

Here is the sample for them

Type 2 - The Oracle OCI JDBC driver requires an installation of Oracle's SQL*Net to provide communication to the database server.

Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc : oracle : oci8 : @hostname : Databasename", "scott", "tiger");

Type 4 - 100% Java Driver
mainly used for webappl.Fast access.Nothing is needed preinstalled.
Oracle provides thin driver for this.

Driver driver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection("jdbc : oracle : thin : @ bname", "scott", "tiger")

[ August 05, 2004: Message edited by: Mohd Afroz Ahmed ]

The value of an idea lies in the usage of it.
I agree. Here's the link:
subject: Dynamic DSN creation
It's not a secret anymore!