Solved

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

Posted on 2000-03-16
4
599 Views
Last Modified: 2008-02-01
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
Comment
Question by:madhusr
  • 3
4 Comments
 
LVL 1

Expert Comment

by:BillyAbbott
ID: 2624169
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2624359
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2624475
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
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 100 total points
ID: 2624486
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 107
oracle 11g 23 85
Oracle SQL syntax check  without executing 6 57
expectj telnet failing 5 37
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This video teaches viewers about errors in exception handling.

832 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