Solved

getting the path of an ODBC database

Posted on 2002-03-26
9
192 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 98
Error trying to install RTMT Win7 5 61
Apps blocked by Java 9 81
Running JavaFX on JDeveloper 12C 1 32
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

823 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