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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

How to get DSN(Data Source Name)s using java

Hi there,
    How can i get all the DSN(Data Source Name)s created in my system using java.any help..........

sample code would be appreciated.

Thanx inadvance.

Madhusr
0
madhusr
Asked:
madhusr
  • 3
1 Solution
 
BillyAbbottCommented:
i'm fauirly sure that you can;t using normal java, but there might be a microsoft extensiojn which does it. as far as i know dsn's are only used in wondows systems, so an ms only extension shouldn't be too much of a problem.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
ODBC Data Sources are available from the Windows registry as subkeys of HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources.

There is no builtin Java facility for accessing the Windows registry. What you need to do is call the Win32 Registry APIs using JNI. If you've never done any JNI programming before, this can be a daunting task.

There is a free collection (for commercial or non-commercial use) of Java classes and native code called JObjects that will give you access to the Windows registry to create, read, and delete keys from a Java program. It can be downloaded from http://www.jobjects.com/products/jst/.

Another, distributed under the GPL with source can be found at http://www.trustice.com/java/jnireg/index.shtml.

This is a comment, not an answer, because I haven't personally used either of these packages and cannot post code as you requested. However, If I understand your question correctly, this is what you will need to do.

Best regards,
Jim Cakalic
0
 
Jim CakalicSenior Developer/ArchitectCommented:
ODBC Data Sources are available from the Windows registry as subkeys of HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources.

There is no builtin Java facility for accessing the Windows registry. What you need to do is call the Win32 Registry APIs using JNI. If you've never done any JNI programming before, this can be a daunting task.

There is a free collection (for commercial or non-commercial use) of Java classes and native code called JObjects that will give you access to the Windows registry to create, read, and delete keys from a Java program. It can be downloaded from http://www.jobjects.com/products/jst/.

Another, distributed under the GPL with source can be found at http://www.trustice.com/java/jnireg/index.shtml.

This is a comment, not an answer, because I haven't personally used either of these packages and cannot post code as you requested. However, If I understand your question correctly, this is what you will need to do.

Best regards,
Jim Cakalic
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Well, shucks! I updated the page in my browser and it posted my last comment again. Sorry.

Here is some example code using the JObjects toolkit. This is a direct ripoff of one of the sample classes included in the package.

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

public class WindowsDataSources {

    public WindowsDataSources() {
        // check registry support
        if (!SystemToolkit.doesSupportRegistry()) {
            System.out.println( "No registry support." );
        }
    }

    public void list() {
        int topkey = RegistryKey.KEY_LOCAL_MACHINE;
        String subkey = "Software\\ODBC\\ODBC.INI\\ODBC Data Sources";
        try {
            // get registry manager instance
            RegistryManager regManager = SystemToolkit.getRegistryManager();
            // create key reference
            RegistryKey key = regManager.openRegistryKey(topkey, subkey);
           
            if (key.exists()) {
                // enumerate properties
                Enumeration e = key.properties();
                while (e.hasMoreElements()) {
                    String name = (String)e.nextElement();
                    System.out.println(name);
                    //String val = key.getStringProperty(name);
                    //System.out.println( "  " + name + " = " + val );
                }
            }
        } catch (RegistryException re) {
            re.printStackTrace();
        }
    }

    public static void main(String[] args) {
        WindowsDataSources dataSources = new WindowsDataSources();
        dataSources.list();
    }
}

Jim Cakalic
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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