SAMPLE: JDBC.exe Demonstrates How to Use JDBC/ODBC Bridge When Using Visual J++ (242921)
The information in this article applies to:
- Microsoft Visual J++ 6.0
- Microsoft Open Database Connectivity 3.0
- Microsoft Open Database Connectivity 3.5
- Microsoft Open Database Connectivity 3.51
- Microsoft Open Database Connectivity 3.6
This article was previously published under Q242921 SUMMARY The JDBC.exe sample demonstrates how to use the JDBC:ODBC
data access technology, when using Visual J++, by accessing local Data Source
Names (DSNs), getting a list of tables in a database, and populating a ListView
control with the table data.
This sample also demonstrates the use
of Windows native API calls through a technique called JDIRECT. In this sample,
the technique is used to call native ODBC APIs, which are used to get the list
of all ODBC DSNs on the local computer. A DSN is a pointer to a relational
database, such as SQL Server, Access, or Oracle. MORE INFORMATIONThe
following file is available for download from the Microsoft Download
Center: Release Date: Feb. 4,
2000 For additional information about how to download Microsoft
Support files, click the following article number to view the article in the
Microsoft Knowledge Base: 119591 How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help to
prevent any unauthorized changes to the file.
|
| JDBCFrontEnd.java | Main code | | JDBCFrontEnd.sln | Workspace solution file | | JDBCFrontEnd.vjp | Workspace project file | | JDBCFrontEnd.class | Class file | | JDBCFrontEnd.exe | Executable file |
To run the JDBC sample application:
- Extract the attached files into a folder named
JDBCFrontEnd.
- Compile and run the code. The sample application will
populate a list box with all the DSN names on the computer.
- Select the DSN and type in the username and password if
required. Click Connect. You should see a confirmation message in the Status box.
- Click any table name; the ListView control will be filled
with data from that table. You can also connect to a database by typing in a
proper JDBC URL and clicking Connect Using this URL. You can also retype your SQL query and click Requery to view the results.
NOTE: This sample application does not support table names with spaces
in them. The most important part of the JDBC sample application is
the JDBCfrontend.java file. When the application starts, the PopulateDSNList function is called. This function uses native ODBC API calls to
get the list of all DSNs on the system, as shown in the code segment below:
public void PopulateDSNList()
{
//int SQL_FETCH_NEXT = 1;
//int SQL_MAX_DSN_LENGTH = 32;
int henv[] = {0};
int NameLength1Ptr[] = {0};
int NameLength2Ptr[] = {0};
StringBuffer Description = new StringBuffer(1024);
StringBuffer DSNName = new StringBuffer(1024);
int rc;
//Allocating Environment Handle
rc = SQLAllocEnv(henv);
rc = SQLDataSources(henv[0],SQLConstant.SQL_FETCH_NEXT,DSNName,
SQLConstant.SQL_MAX_DSN_LENGTH,NameLength1Ptr,
Description,256,NameLength2Ptr);
while (rc!=SQLConstant.SQL_NO_DATA&&rc!=SQLConstant.SQL_ERROR)
{
rc = SQLDataSources(henv[0],SQLConstant.SQL_FETCH_NEXT,DSNName,
SQLConstant.SQL_MAX_DSN_LENGTH,NameLength1Ptr,
Description,256,NameLength2Ptr);
lbxDSNList.addItem((Object)DSNName.toString());
}
}
Here is the syntax for using JDIRECT in Java applications (in this
case, SQLAllocEnv comes from ODBC32.dll):
/** @dll.import("ODBC32") */
public static native short SQLAllocEnv( int henv[] );
Please download and refer to the JDBC.exe sample for the complete
source code.
| Modification Type: | Minor | Last Reviewed: | 8/9/2004 |
|---|
| Keywords: | kbdownload kbfile kbSample KB242921 |
|---|
|