Solved

getting the path of an ODBC database

Posted on 2002-03-26
9
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

697 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