Solved

DSN list

Posted on 2001-06-15
9
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:
Suggested Courses

628 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