⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dba.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD>  <TITLE>Java(TM) Language Basics, Part 1, Lesson 7: Database Access and Permissions</TITLE>  <META NAME="AUTHOR" CONTENT="Monica Pawlan">  <META NAME="KEYWORDS" CONTENT="programming, basics, Java 2">  <META NAME="OWNER" CONTENT="Editorial/JDC">  <META NAME="revision" CONTENT="@(#)dba.src	1.27 03/09/00  JDC">    <STYLE TYPE="text/css">    <!--     CODE {font-family: Courier, Monospace;          font-size: 12pt}    PRE {font-family: Courier, Monospace;         font-size: 11pt}      -->  </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066"><!-- 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" WIDTH="55" HEIGHT="14" ALT="Search" value="search" BORDER="0"></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" WIDTH="165" HEIGHT="22" 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 &amp; 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>    <DIV ALIGN="RIGHT">    <A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>    </DIV></TD></TR><TR><TD>&nbsp;</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><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- 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>Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Programming Language Basics, Part 1<BR>  <FONT SIZE="3">Lesson 7: Database Access and Permissions</FONT></H2>    <FONT SIZE="-1">[<A HREF="data.html">&lt;&lt;BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="rmi.html">NEXT&gt;&gt;</A>]</FONT></DIV><P>This lesson converts the application, applet, and servletexamples from Lesson 6 to write to and read from a databaseusing JDBC<FONT SIZE="-2"><SUP>TM</SUP></FONT>. JDBC isthe Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> database connectivity application programming interface (API) available in theJava&#174; 2 Platform software. <P>The code for this lesson is very similar to the code you saw in Lesson 6, but additional steps (beyond converting the file access code to database access code) include setting up the environment, creating a database table, and connecting to the database. Creating a database table is a database administration task that is not part of your program code. However, establishing a database connection and the resulting database access are. <P>As in Lesson 6, the applet needs appropriate permissions toconnect to the database. Which permissions it needs varieswith the type of driver used to make the database connection. <UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#setup">Database Setup</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#db">Create Database Table</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#app">Database Access by Applications</A></FONT>	<UL>	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">	<A HREF="#estab">Establishing a Connection</A></FONT>	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">	<A HREF="#final">Final and Private Variables</A></FONT>	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">	<A HREF="#rw">Writing and Reading Data</A></FONT>	</UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#applet">Database Access by Applets</A></FONT>	<UL>	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">	<A HREF="#jdbc">JDBC Driver</A></FONT>	<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">	<A HREF="#odbc">JDBC-ODBC Bridge with ODBC Driver</A></FONT>	</UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#serv">Database Access by Servlets</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#more">More Information</A></FONT></UL><HR><A NAME="setup"></A><H3>Database Setup</H3>You need access to a database if you want to run the examplesin this lesson. You can install a database on your machine or perhaps you have access to a database at work.  Either way, you need adatabase driver and any relevant environment settings so your program can load the driver and locate the database. The program willalso need database login information in the form of a user nameand password. <P>A database driver is software that lets a program establish aconnection with a database. If you do not have the right driver for the database to which you want to connect, your program will be unable to establish the connection.<P>Drivers either come with the database or areavailable from the Web. If you install your own database, consultthe documentation for the driver for information on installationand any other environment settings you need for your platform. If you are using a database at work,consult your database administrator for this information.<P>To show you two ways to do it, the application example usesthe <CODE>jdbc</CODE> driver, the applet examples  use the  <CODE>jdbc</CODE> and <CODE>jdbc.odbc</CODE> drivers, and the servlet example uses the <CODE>jdbc.odbc</CODE> driver. All examples connect to an <CODE>OracleOCI7.3.4</CODE> database. <P>Connections to other databases will involve similar steps andcode. Be sure to consult your documentation or systemadministrator if you need help connecting to the database. <A NAME="db"></A><H3>Create Database Table</H3>Once you have access to a database, create a table in itfor the examples in this lesson. You need a table with onetext field for storing character data. </FONT><PRE>TABLE DBA (        TEXT            varchar2(100),        primary key (TEXT))</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="app"></A><H3>Database Access by Applications</H3>This example converts the <A HREF="./Code/FileIO.java">FileIO</A>program from Lesson 6 to write data to and read data froma database.  The top window below appears when you startthe <A HREF="./Code/Dba.java">Dba</A> application, and the windowbeneath it appears when you click the <CODE>Click Me</CODE> button. <P>When you click the <CODE>Click Me</CODE> button, whatever is entered into the text field is saved tothe database. After that, the data is retrieved from thedatabase and displayed in the window shown on the bottom. If you write data to the table more than once, everything written is read and displayedin the window shown on the bottom, so you might have to enlarge the window to see the entire list of table items.<P><CENTER><IMG SRC="./Art/dba1.gif" WIDTH="252" HEIGHT="129" ALT=""><P>When Application Starts<P><IMG SRC="./Art/dba2.gif" WIDTH="247" HEIGHT="157" ALT=""><P>After Writing Orange and Apple to Database</CENTER><P>The database access application needs code to establish the database connection and do the database read and write operations.<A NAME="estab"></A><H4>Establishing a Database Connection</H4>The JDBC <CODE>DriverManager</CODE> class can handle multiple database drivers, and initiates all database communication. To load the driver and connect to the database, the application needs a <CODE>Connection</CODE> object and <CODE>Strings</CODE> thatrepresent the <CODE>_driver</CODE> and <CODE>_url</CODE>. <P>The <CODE>_url</CODE> string is in the form of a Uniform Resource Locator (URL). It consists of the URL, Oracle subprotcol,and Oracle data source in the form <CODE>jdbc:oracle:thin</CODE>, the database login <CODE>username</CODE>, the <CODE>password</CODE>, plus machine, port, and protocol information.   </FONT><PRE>private Connection c;final static private String _driver =   &quot;oracle.jdbc.driver.OracleDriver&quot;;final static private String _url = 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -