📄 jdbc.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 4: Distributed Computing</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)jdbc.src 1.3 07/20/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 4: JDBC<FONT SIZE="-2"><SUP>TM</SUP></FONT> Technology</FONT></H2>
<FONT SIZE="-1">[<A HREF="corba.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="aucserv.html">NEXT>></A>]</FONT></DIV>
<P>
The Bean-managed Enterprise JavaBeans<FONT SIZE="-2"><SUP>TM</SUP></FONT> auction application
with its Remote Method Invocation (RMI) and Common Object Request
Broker (CORBA) variants have used simple JDBC<FONT SIZE="-2"><SUP>TM</SUP></FONT> calls to retrieve
and update information from a database using a JDBC connection pool.
By default, JDBC database access involves opening a database connection,
running SQL commands in a statement, processing the returned
results, and closing the database connection.
<P>
Overall, the default approach works well for low volume database
access, but how do you manage a large number of requests that update
many related tables at once and still ensure data integrity?
This section explains how with the following topics.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#driver">JDBC Drivers</A> </FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#connect">Database Connections</A> </FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#statement">Statements</A></FONT>
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#call">Callable Statements</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#ment">Statements</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#prepare">Prepared Statements</A></FONT>
</UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#cache">Caching Database Results</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#result">Result Sets</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#scroll">Scrolling Result Sets</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#trans">Controlling Transactions</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#esc">Escaping Characters</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#db">Mapping Database Types</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#date">Mapping Date types</A></FONT>
</UL>
<HR>
<A NAME="driver"></A>
<H3>JDBC Drivers</H3>
The connection to the database is handled by the JDBC Driver class. The
Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> SDK contains only one JDBC driver, a <CODE>jdbc-odbc</CODE>
bridge that can communicate with an existing Open DataBase
Conectivity (ODBC) driver. Other databases need a JDBC driver specific
to that database.
<P>
To get a general idea of what the JDBC driver does, you can examine
the <CODE>JDCConnectionDriver.java</CODE> file. The JDCConnectionDriver
class implements the <CODE>java.sql.Driver</CODE> class and acts as a
pass-through driver by forwarding JDBC requests to the real database
JDBC Driver. The JDBC driver class is loaded with a call to
<CODE>Class.forName(drivername)</CODE>.
<P>
These next lines of code show how to
load three different JDBC driver classes:
<PRE>
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("postgresql.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
</PRE>
Each JDBC driver is configured to understand a specific URL so multiple
JDBC drivers can be loaded at any one time. When you specify a URL at
connect time, the first matching JDBC driver is selected.
<P>
The jdbc-odbc bridge accepts Uniform Resource Locators (URLs) starting
with <CODE>jdbc:odbc:</CODE> and uses the next field in that URL to
specify the data source name. The data source name identifies the
particular database scheme you wish to access. The URL can also include
more details on how to contact the database and enter the account.
<PRE>
//access the ejbdemo tables
String url = "jdbc:odbc:ejbdemo";
</PRE>
This next example contains the Oracle SQL*net information on the particular
database called ejbdemo on machine dbmachine
<PRE>
String url = "jdbc:oracle:thin:user/password@(
description=(address_list=(
address=(protocol=tcp)
(host=dbmachine)(port=1521)))(source_route=yes)
(connect_data=(sid=ejbdemo)))";
</PRE>
This next examples uses <CODE>mysql</CODE> to connect to the
ejbdemo database on the local machine. The login user name and
password details are also included.
<PRE>
String url =
"jdbc:mysql://localhost/ejbdemo?user=user;
password=pass";
</PRE>
JDBC drivers are divided into four types. Drivers may also be
categorized as pure Java or thin drivers to indicate if they
are used for client applications (pure Java drivers) or applets
(thin drivers). Newer drivers are usually Type 3 or 4.
The four types are as follows:
<H4>Type 1 Drivers</H4>
Type 1 JDBC drivers are the bridge drivers such as the jdbc-odbc bridge.
These drivers rely on an intermediary such as ODBC to transfer the SQL
calls to the database. Bridge drivers often rely on native code,
although the jdbc-odbc library native code is part of the Java<A HREF="#TJVM"><SUP>1</SUP></A> 2
virtual machine.
<H4> Type 2 Drivers</H4>
Type 2 Drivers use the existing database API to communicate with
the database on the client. Although Type 2 drivers are faster than
Type 1 drivers, Type 2 drivers use native code and require additional
permissions to work in an applet.
<P>
A Type 2 driver might need client-side database code to connect over
the network.
<H4>Type 3 Drivers</H4>
Type 3 Drivers call the database API on the server. JDBC requests from
the client are first proxied to the JDBC Driver on the server to run.
Type 3 and 4 drivers can be used by thin clients as they need no native code.
<H4>Type 4 Drivers</H4>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -