Solved

getting the path of an ODBC database

Posted on 2002-03-26
9
189 Views
Last Modified: 2010-03-31
All,

Please can anyone tell me how to get the full pathname of an existing ODBC database.

I have the ODBC name, but can't seem to find the appropriate class to get the actually database path in which the ODBC is pointing to.

Thanks in advance.
0
Comment
Question by:Cuong
  • 6
  • 3
9 Comments
 
LVL 3

Expert Comment

by:msterjev
ID: 6896186
You need to put jdbc:odbc in front of the ODBC name. For example, if you have registered Northwind database the exact path is:

jdbc:odbc:Nortwind
0
 

Author Comment

by:Cuong
ID: 6896232
I tried doing this!

Maybe I should have mentioned, that I need to create a File object using the path.

I tried the following, and it doesn't seem to create the File

String DBName = "jdbc:odbc"+ODBCName;
File DBFile = new File(DBName);

DBFile.getAbsolutePath() returns jdbc:odbc+ODBCName



0
 
LVL 3

Expert Comment

by:msterjev
ID: 6896765
Did you loaded the approapriate driver before connecting to the database?You must type this:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
0
 

Author Comment

by:Cuong
ID: 6896873
Yes.

I have adatabase class, and I make a connection in the constructor.

It is from this Database object, that i need to get the path in which the Database is pointing at.

0
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

 
LVL 3

Accepted Solution

by:
msterjev earned 50 total points
ID: 6898852
OK. You need some JNI code. Write this class:

public class JODBCInfo
{
     static
     {
          System.loadLibrary("JODBCInfo");
     }
     
     public static native String getDatabasePath(String dbName);
}

Then generate header file with:

javah JODBCInfo

Then start C++ and make new Win32Dll project.

The JNI code is as follows:

#include "JODBCInfo.h"
#include <windows.h>

JNIEXPORT jstring JNICALL Java_JODBCInfo_getDatabasePath(JNIEnv * env, jclass c, jstring dbName)
{
     
     HKEY hKey;
     long ret;
     char dbPath[MAX_PATH];
     DWORD cbDbPath=MAX_PATH;
     DWORD lType;
     char regKey[100];
     jstring path=NULL;
     const char *db = env->GetStringUTFChars(dbName, 0);
     lstrcpy(regKey,"Software\\ODBC\\ODBC.INI\\");
     lstrcat(regKey,db);
      ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,regKey,0,KEY_QUERY_VALUE,&hKey);
     if(ret==ERROR_SUCCESS)
     {
          ret=RegQueryValueEx(hKey,"DBQ",NULL,&lType,(unsigned char *)dbPath,&cbDbPath);
          if(ret==ERROR_SUCCESS)
          {
               path=env->NewStringUTF(dbPath);
          }
          RegCloseKey(hKey);
     }
     env->ReleaseStringUTFChars(dbName,db);
     return path;
}

Put the generated JODBCInfo.dll in the same directory with the JODBCInfo class.

Test this:

public class Test
{
     public static void main(String[] args)
     {
          System.out.println(JODBCInfo.getDatabasePath("Northwind"));
     }
}


I think the result is exactly what do you want to achieve. If you are not coding  with JNI, don't be afraid, the code is short and complete!The task can only be done this way!
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6901792
It seems you are not willing to give me the points!The proposed solutions works fine and is definitelly what you are looking for!
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6904588
The points ;-)
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6909725
I really don't know what is wrong with the above solution. I wrote this JNI code only for you, and I think it is worth a little price!
0
 

Author Comment

by:Cuong
ID: 6912305
Thanks!

I only gave average cause it's not the type of solution i was looking for.  I.e. the time to implement the solution did not warrent the problem.

I haven't actually tried it yet, but I'll take your word that it works.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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

19 Experts available now in Live!

Get 1:1 Help Now