Solved

DSN list

Posted on 2001-06-15
9
412 Views
Last Modified: 2010-05-18
Anyone know how I can get a list of the ODBC DSN's on my machine?  I'd like the user to be able to select a database from a list, but I want to avoid hardcoding the DSN name Strings into a list or having to write a properties file or resource bundle.  I'd rather try pulling them on the fly from the local environment settings if I can.

Has anyone done this before?  Thanks!

Marius
0
Comment
Question by:xxMariusxx
9 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6197172
I found some settings in the registry under
"HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources"
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6197793
You can use the Registry Toolkit from the JST bu JObjects (http://www.jobjects.com/products/jst/index.html).

You get get a list of all System DSNs bu enumerating
 
  "HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources"

and all User DSNs by enumerating

   "HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources"
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6197821
Here is an example of a program that lists all the DSNs using the JObjects JST to access the registry :

import com.jobjects.jst.*;
import java.util.*;

public class ODBCDSNLister{

     public ODBCDSNLister()
       {
         if( !SystemToolkit.doesSupportRegistry() )  {
           System.out.println( "No registry support." );
           return;
       }

       int topKey1 = RegistryKey.KEY_LOCAL_MACHINE;
       int topKey2 = RegistryKey.KEY_CURRENT_USER;
       String subKey = "Software\\ODBC\\ODBC.INI\\ODBC Data Sources";

       try{

            RegistryManager regm = SystemToolkit.getRegistryManager();
            RegistryKey key = regm.openRegistryKey(topKey1, subKey);

            if(!key.exists()){
                 System.out.println("No system DSN information found.");
                return;
            }

            System.out.println();
            System.out.println( "System DSNs:" );
            for( Enumeration e = key.properties(); e.hasMoreElements(); ){
                 String name = (String) e.nextElement();
                 String val = key.getStringProperty( name );
                 System.out.println( "  " + name + " = " + val );
            }

            key = regm.openRegistryKey(topKey2, subKey);

            if(!key.exists()){
                 System.out.println("No user DSN information found.");
                 return;
            }
            System.out.println();
            System.out.println( "User DSNs:" );
            for( Enumeration e = key.properties(); e.hasMoreElements(); ){
                 String name = (String) e.nextElement();
                 String val = key.getStringProperty( name );
                 System.out.println( "  " + name + " = " + val );
            }
       }catch(RegistryException re){
            re.printStackTrace();
       }
  }

  public static void main(String[] args){
       ODBCDSNLister o = new ODBCDSNLister();
  }
}
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Expert Comment

by:skullface
ID: 6198717
Most information about DSN's is stored in the registry. HKLM\Software\ODBC\ODBCINST.INI contains a list of all ODBC drivers installed on your machine. HKLM\Software\ODBC\ODBC.INI contains all of your system DSN's stored as separate subkeys. ODBC.INI has two other subkeys, ODBC Data Sources that contains a list of all your system DSN's, and ODBC FileDSN, which contains the name of the folder where File DSN's are stored (C:\Program Files\Common Files\ODBC\Data Sources is the default). User DSN's are stored in Hkey_Current_User\Software\ODBC, but it has only an ODBC.INI key.

0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6198751
Hmmm...I can't use jobjects.  I'm on a government computer and ya know how they can be.  Unless it's approved software part of their ITK, it's off limits (meaning even though the package is free and probably works great, I can't use it...but then whadaya expect from them huh?  I've got Visual Age bound to the JDK 1.2...1.3 is almost a thought for their new ITK release, but it's not a fast process...and forget 1.4!)

I wonder if DSN lookups can be done via JNDI...any thoughts (maybe I'd hafta up the points, huh)?

Marius
0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6198769
*smacks forehead* ...or I could just read directly out of C:\\WinNT\\ODBC.ini itself!

If anyone knows how to access the registry with JNDI, I'd up the points to 100 just for kicks!

Marius
0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6198805
*smacks forehead* ...or I could just read directly out of C:\\WinNT\\ODBC.ini itself!

If anyone knows how to access the registry with JNDI, I'd up the points to 100 just for kicks!

Marius
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 50 total points
ID: 6198808
You could recreate the work done by Jobjects yourself.
The full source code of their registry access DLL comes with the Jobjects package when you download it. It just straigh forward JNI into C++ dll.

If all you wanted to do was get registry entries for ODBC you could write you own JNI dll easily.
0
 
LVL 2

Author Comment

by:xxMariusxx
ID: 6198930
I need a real quick fix for right now, so I've written the following code to grab the entries from the .ini file itself.

private String[] getDSNs() {
     try {
          FileInputStream fis = new FileInputStream("C:\\winnt\\odbc.ini");
          BufferedReader br = new BufferedReader(new InputStreamReader(fis));
          Vector entries = new Vector();
          boolean read = false;
          String s;

          while(br.ready()) {
               s = br.readLine();
               if(!read && s.toUpperCase().startsWith("[ODBC 32 BIT DATA SOURCES]"))
                    read = true;
               else
                    if(s.startsWith("["))
                         read = false;

               if(read && !s.toUpperCase().startsWith("[ODBC 32 BIT DATA SOURCES]")) {
                    s = s.substring(0, s.indexOf("="));
                    entries.addElement(s);
               }
          }
          fis.close();
          return (String[])entries.toArray(new String[0]);
     
     } catch(Exception e) {
          return new String[0];
     }
}

This won't work when we move to Windoze 2000 probably, so I'll be sure to write my own JNI DLL sometime in the next week (before I forget I did this little temporary patch...nothing quite like watching 'temporary' patches become permanent, huh?).  I thought I'd post my code up here in case anyone else might ever find it useful though.

Thanks guys for the help...ozymandias, I'll look at the jobjects source code.  Thanks again!

Marius
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 18
jsp login check 12 43
Which non-HTML GUI front end to use with Java? 3 24
Glassfish admin console not working 1 34
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question