Friday, April 9, 2010

ODBC System DSN List Using Java

/**
* OdbcSystemDSNListUtil.java
*/
package odbc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
* @author Nimish
* Apr 9, 2010
*/
public class OdbcSystemDSNListUtil {

public static Set getODBCSystemDNS() {
String PERSONAL_FOLDER_CMD ="HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBC.INI";
String []command = new String[] {"reg","query",PERSONAL_FOLDER_CMD};
Set dsnList = new HashSet();
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader stream = new BufferedReader(new InputStreamReader(process.getInputStream()));
String dsn = "";
while((dsn=stream.readLine())!=null){
if(dsn.indexOf(PERSONAL_FOLDER_CMD+"\\") != -1)
dsnList.add(dsn.substring(dsn.lastIndexOf("\\")+1));
}

} catch (IOException e) {
e.printStackTrace();
}
return dsnList;
}

public static void main(String[] args) {
Set dsnList = getODBCSystemDNS();
for(String dsn:dsnList) {
System.out.println("dsn name: "+ dsn);
}
}
}

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails