?
Solved

getting the path of an ODBC database

Posted on 2002-03-26
9
Medium Priority
?
196 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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