📄 tij0169.html
字号:
how to set up a database using your database administration software.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course, this process can vary radically from machine to machine, but the
process I used to make it work under 32-bit Windows might give you clues to
help you attack your own situation.
</FONT><P></DIV>
<A NAME="Heading535"></A><H4 ALIGN=LEFT>
Step
1: Find the JDBC Driver
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
program above contains the statement:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
implies a directory structure, which is deceiving. With this particular
installation of JDK 1.1, there was no file called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JdbcOdbcDriver.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so if you looked at this example and went searching for it you’d be
frustrated. Other published examples use a pseudo name, such as
“myDriver.ClassName,” which is less than helpful. In fact, the load
statement above for the jdbc-odbc driver (the only one that actually comes with
JDK 1.1) appears in only a few places in the online documentation (in
particular, a page labeled “JDBC-ODBC Bridge Driver”). If the load
statement above doesn’t work, then the name might have been changed as
part of a Java version change, so you should hunt through the documentation
again.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
the load statement is wrong, you’ll get an exception at this point. To
test whether your driver load statement is working correctly, comment out the
code after the statement and up to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>catch</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
clause; if the program throws no exceptions it means that the driver is loading
properly.
</FONT><P></DIV>
<A NAME="Heading536"></A><H4 ALIGN=LEFT>
Step
2: Configure the database
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Again,
this is specific to 32-bit Windows; you might need to do some research to
figure it out for your own platform.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">First,
open the control panel. You might find two icons that say “ODBC.”
You must use the one that says “32bit ODBC,” since the other one is
for backwards compatibility with 16-bit ODBC software and will produce no
results for JDBC. When you open the “32bit ODBC” icon, you’ll
see a tabbed dialog with a number of tabs, including “User DSN,”
“System DSN,” “File DSN,” etc., in which
“DSN” means “Data Source Name.” It turns out that for
the JDBC-ODBC bridge, the only place where it’s important to set up your
database is “System DSN,” but you’ll also want to test your
configuration and create queries, and for that you’ll also need to set up
your database in “File DSN.” This will allow the Microsoft Query
tool (that comes with Microsoft Office) to find the database. Note that other
query tools are also available from other vendors.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
most interesting database is one that you’re already using. Standard ODBC
supports a number of different file formats including such venerable workhorses
as DBase. However, it also includes the simple “comma-separated
ASCII” format, which virtually every data tool has the ability to write.
In my case, I just took my “people” database that I’ve been
maintaining for years using various contact-management tools and exported it as
a comma-separated ASCII file (these typically have an extension of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.csv</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
In the “File DSN” section I chose “Add,” chose the text
driver to handle my comma-separated ASCII file, and then un-checked “use
current directory” to allow me to specify the directory where I exported
the data file.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You’ll
notice when you do this that you don’t actually specify a file, only a
directory. That’s because a database is typically represented as a
collection of files under a single directory (although it could be represented
in other forms as well). Each file usually contains a single table, and the SQL
statements can produce results that are culled from multiple tables in the
database (this is called a <A NAME="Index2851"></A><A NAME="Index2852"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>join</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
A database that contains only a single table (like this one) is usually called a <A NAME="Index2853"></A><A NAME="Index2854"></A><A NAME="Index2855"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>flat-file
database
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Most problems that go beyond the simple storage and retrieval of data generally
require multiple tables that must be related by joins to produce the desired
results, and these are called <A NAME="Index2856"></A><A NAME="Index2857"></A><A NAME="Index2858"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>relational</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
databases.
</FONT><P></DIV>
<A NAME="Heading537"></A><H4 ALIGN=LEFT>
Step
3: Test the configuration
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
test the configuration you’ll need a way to discover whether the database
is visible from a program that queries it. Of course, you can simply run the
JDBC program example above up to and including the statement:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Connection
c = DriverManager.getConnection(
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
dbUrl, user, password);
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
an exception is thrown, your configuration was incorrect.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">However,
it’s useful to get a query-generation tool involved at this point. I used
Microsoft Query that came with Microsoft Office, but you might prefer something
else. The query tool must know where the database is, and Microsoft Query
required that I go to the ODBC Administrator’s “File DSN” tab
and add a new entry there, again specifying the text driver and the directory
where my database lives. You can name the entry anything you want, but
it’s helpful to use the same name you used in “System DSN.”
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Once
you’ve done this, you will see that your database is available when you
create a new query using your query tool.
</FONT><P></DIV>
<A NAME="Heading538"></A><H4 ALIGN=LEFT>
Step
4: Generate your SQL query
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
query that I created using Microsoft Query not only showed me that my database
was there and in good order, but it also automatically created the SQL code
that I needed to insert into my Java program. I wanted a query that would
search for records that had the last name that was typed on the command line
when starting the Java program. So as a starting point, I searched for a
specific last name, ‘Eckel’. I also wanted to display only those
names that had email addresses associated with them. The steps I took to create
this query were:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Start
a new query and use the Query Wizard. Select the “people” database.
(This is the equivalent of opening the database connection using the
appropriate database URL.)
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Select
the “people” table within the database. From within the table,
choose the columns FIRST, LAST, and EMAIL.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Under
“Filter Data,” choose LAST and select “equals” with an
argument of Eckel. Click the “And” radio button.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Choose
EMAIL and select “Is not Null.”
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Under
“Sort By,” choose FIRST.
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
result of this query will show you whether you’re getting what you want.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now
you can press the SQL button and without any research on your part, up will pop
the correct SQL code, ready for you to cut and paste. For this query, it looked
like this:
</FONT><P></DIV>
<font color="#990000"><PRE>SELECT people.FIRST, people.LAST, people.EMAIL
FROM people.csv people
WHERE (people.LAST='Eckel') AND
(people.EMAIL Is Not Null)
ORDER BY people.FIRST </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">With
more complicated queries it’s easy to get things wrong, but with a query
tool you can interactively test your queries and automatically generate the
correct code. It’s hard to argue the case for doing this by hand.
</FONT><P></DIV>
<A NAME="Heading539"></A><H4 ALIGN=LEFT>
Step
5: Modify and paste in your query
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You’ll
notice that the code above looks different from what’s used in the
program. That’s because the query tool uses full qualification for all of
the names, even when there’s only one table involved. (When more than one
table is involved, the qualification prevents collisions between columns from
different tables that have the same names.) Since this query involves only one
table, you can optionally remove the “people” qualifier from most
of the names, like this:
</FONT><P></DIV>
<font color="#990000"><PRE>SELECT FIRST, LAST, EMAIL
FROM people.csv people
WHERE (LAST='Eckel') AND
(EMAIL Is Not Null)
ORDER BY FIRST </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
addition, you don’t want this program to be hard coded to look for only
one name. Instead, it should hunt for the name given as the command-line
argument. Making these changes and turning the SQL statement into a
dynamically-created
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
produces:
</FONT><P></DIV>
<font color="#990000"><PRE>"SELECT FIRST, LAST, EMAIL " +
"FROM people.csv people " +
"WHERE " +
"(LAST='" + args[0] + "') " +
" AND (EMAIL Is Not Null) " +
"ORDER BY FIRST"); </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">SQL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -