Get the JDBC Learning sample

Source: Internet
Author: User
Tags command line documentation dsn odbc requires microsoft office support

As far as JDBC is concerned, the code itself is easy to understand. The most puzzling part is how to make it run on its own particular system. It's confusing because it requires us to know how to get the JDBC driver to load correctly and how to set up a database with our database management software.
Of course, the specific process of operation on different machines will also make a difference. But the operating procedures provided here in the 32-bit Windows environment can effectively help you understand the operations on other platforms.

1. Step 1: Find the JDBC Driver
The above program contains the following statement:
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
This seems to imply a directory structure, but let's not be fooled by it. In my hands. In this JDK 1.1 installation version, there is no file called Jdbcodbcdriver.class. So if you look at this example and look for it, then you will inevitably return in vain. Others provide examples using a fake name, such as "Mydriver.classname", but people literally don't get any help. In fact, the above statement for loading the JDBC-ODBC driver, which is actually the only driver provided with JDK 1.1, appears in many places on the online documentation (especially in a page labeled "Jdbc-odbc Bridge Driver"). If the above load statement does not work, then its name may have changed with the release of the new Java version, and should be found in the online documentation for a new presentation.
If the LOAD statement fails, a violation is received at this time. To verify that the driver Load statement is working correctly, the code that follows the statement until the catch clause is temporarily set as a comment. If the program does not run without an offence, the driver is loaded correctly.

2. Step 2: Configure the database
In the same way, we are limited to working in a 32-bit Windows environment, and you may need to study your own operating system to find a way to configure your platform.
First open the Control Panel. There may be two icons that contain the word "ODBC," and you must select the 32-bit ODBC, because the other is set up to maintain backward compatibility with 16-bit software, and JDBC blending has no result. When you double-click the 32-bit ODBC icon, you should see a card-type dialog box with multiple card tags on it, including User DSN, System DSN, File DSN, and so on. Where "DSN" represents the data source name. They are all related to the Jdbc-odbc bridge, but the only important place to set up the database is the "System DSN". However, because you need to test your own configuration and create queries, you also need to set up your own database in file DSN. This allows the Microsoft Query tool to correctly locate the database (provided with Microsoft Office Support). Note that some software companies have also designed their own query tools.
The most interesting database is the one we've used. Standard ODBC supports multiple file formats, including some that are specific to different companies, such as dBASE. However, it also includes a simple comma-delimited ASCII format that can be generated by almost every data tool. For the present example, I only choose my own "people" database. This is a database I have been maintaining for many years, with various contact management tools in the middle. I'm exporting it as a comma-delimited ASCII file (usually with a. csv extension, which can be used in the same file format when you export a phone book with Outlook Express). In the File DSN area, I press the Add button, select the text driver that controls the comma-delimited ASCII file (Microsoft text Driver), and then undo the "Use current directory" selection so that you can specify the directory yourself when you export the data file.
You'll notice that when you do these things, you don't actually specify a file, just a directory. That's because a database is usually made up of a series of files in a directory (although other forms may be used). Each file typically contains a single "datasheet," and SQL statements can produce results that are extracted from multiple tables in a database (which is called "union" or join) that contain only a single table database (just like the current one) is often called a "flat file database." For most problems, you must use more than one datasheet if you have exceeded the scope of simple data storage and availability. Through "union", so as to achieve the desired results. We called these "relational" databases.

3. Step 3: Test configuration
In order to test the configuration, a way to verify that the database can be "seen" by a program that queries it is needed. Of course, you can simply run the JDBC demonstration program described above and add the following statement:
Connection C = drivermanager.getconnection (
Dburl, user, password);
If a violation is thrown, your configuration is incorrect.
However, it is necessary to use an automated query generation tool at this point. I'm using Microsoft Query, which is provided with Microsoft Office, but you can choose one yourself. The query tool must know where the database is, and Microsoft Query asks me to go to the ODBC Administrator's file DSN card and add a new entry there. Also specifies the text driver and the directory where the database is saved. Although you can name this entry as anything you like, it is best to use the same name as in System DSN.
After you have done this, you will find that your database is available when you create a new query with the query tool.

4. Step 4: Build your own SQL query
The query I created with Microsoft Query not only indicates that the target database exists and is in a good order, but also automatically generates SQL code to insert it into my own Java program. I want this query to be able to check the records for the same "last name" you typed at the command line when you started the Java program. So as a starting point, I searched my own surname "Eckel". In addition, I want to show only those names that have a corresponding e-mail address. The steps to create this query are as follows:
(1) Start a new query and use the Query Wizard (Wizard). Select the People database (equivalent to opening a database connection with a suitable database URL).
(2) Select the "People" table in the database. From this datasheet, select First,last and email columns.
(3) Under "Filter Data" (Filter database), select Last, and select "Equals" (equals), plus the parameter Eckel. Click "and" Radio buttons.
(4) Select email and select ' NOT NULL ' (not empty).
(5) under "Sort by", select a.
The results of the query will show us whether we can get what we want.
You can now press the SQL button. Without any intervention from us, the correct SQL code will immediately bounce out so that we can paste and copy. For this query, the corresponding SQL code is as follows:

SELECT people. The people. Last, people. EMAIL from
people.csv people
WHERE (people. last= ' Eckel ') and 
(people. The EMAIL is isn't Null) order by
people. The


If the query is more complex, manual coding is very easy to make mistakes. But with one query tool, you can test your query interactively and get the correct code automatically. In fact, it's hard to accept the code for these things.

5. Step 5: Modify and paste in your own query
We notice that the above code differs from the code used in the program. That is because the query tool qualifies all names, even if only one data table is involved (if it does involve multiple tables, this qualification avoids conflicting data columns of the same name from different tables). Since this query requires only one datasheet, consider removing the "people" qualifier from most names, as follows:

SELECT A, last, email from
people.csv people
WHERE (last= ' Eckel ') and 
(e-mail is not Null)
ORDER by F IRST

In addition, we do not want to "Hard-code" this program so that only a specific name can be found. Instead, it should be able to find a name that we provide dynamically on the command line. So make the necessary modifications and convert the SQL statements into a dynamically generated string. As shown below:

"Select a, Last, EMAIL" +
"from people.csv people" +
"WHERE" +
"(last= ' + args[0] +" ') "+
" and (EMAIL is isn't Null) "+ ORDER by a
");

SQL also has a way to insert a name into a query called "program" (Procedures), which is very fast. But for most of our experimental database operations, as well as some of the primary applications, it's good to build query strings in Java.
From this example, it is very simple and intuitive to use the tools currently found-especially the query building tools-that involve SQL and JDBC database programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.