Solved

DSN list

Posted on 2001-06-15
9
405 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:skullface
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:xxMariusxx
Comment Utility
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
Comment Utility
*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
Comment Utility
*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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 75
changePi Challenge 15 75
Checkbox and ListView in Android Layout 4 35
Image decoding from Camera 3 42
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now