Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DSN list

Posted on 2001-06-15
9
Medium Priority
?
424 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 150 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

963 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