Solved

getting the path of an ODBC database

Posted on 2002-03-26
9
195 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

729 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