• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

DSN list

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
xxMariusxx
Asked:
xxMariusxx
1 Solution
 
superschlonzCommented:
I found some settings in the registry under
"HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources"
0
 
ozymandiasCommented:
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
 
ozymandiasCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
skullfaceCommented:
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
 
xxMariusxxAuthor Commented:
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
 
xxMariusxxAuthor Commented:
*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
 
xxMariusxxAuthor Commented:
*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
 
ozymandiasCommented:
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
 
xxMariusxxAuthor Commented:
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now