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

📄 dba.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 3 页
字号:
 &quot;jdbc:oracle:thin:username/password@(description=( address_list=(address=(protocol=tcp) (host=developer)(port=1521))) (source_route=yes)(connect_data=(sid=jdcsid)))&quot;;</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><P>The <CODE>actionPerformed</CODE> method calls the <CODE>Class.forName(_driver)</CODE> method to loadthe driver, and the <CODE>DriverManager.getConnection</CODE> methodto establish the connection. The <A HREF="data.html#excep">Exception Handling</A>section in Lesson 6 describes <CODE>try</CODE> and <CODE>catch</CODE>blocks. The only thing different here is that this blockuses two <CODE>catch</CODE> statements because two different errors are possible.  <P>The call to <CODE>Class.forName(_driver);</CODE> throws<CODE>java.lang.ClassNotFoundException</CODE>, and the call to <CODE>c = DriverManager.getConnection(_url);</CODE>throws <CODE>java.sql.SQLException</CODE>. In the caseof either error, the application tells the user what iswrong and exits because the program cannot operate inany meaningful way without a database driver or connection. </FONT><PRE>public void actionPerformed(ActionEvent event){  try{//Load the driver     Class.forName(_driver);//Establish database connection     c = DriverManager.getConnection(_url);   }catch (java.lang.ClassNotFoundException e){     System.out.println(&quot;Cannot find driver class&quot;);     System.exit(1);   }catch (java.sql.SQLException e){     System.out.println(&quot;Cannot get connection&quot;);            System.exit(1);   }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="final"></A><H4>Final and Private Variables</H4>The member variables used to establish the database connection above are declared <CODE>private</CODE>, and two of those variables are also declared <CODE>final</CODE>. <P><STRONG>final</STRONG>:A <CODE>final</CODE> variable containsa constant value that can never change once it is initialized. In the example, the user name, and password are <CODE>final</CODE>variables because you would not want to allow an instanceof this or any other class to change this information.<P><STRONG>private</STRONG>:A <CODE>private</CODE> variable can only beused (accessed) by the class in which it is declared. No other class can read or change <CODE>private</CODE> variables. In the example, the database driver, user name, and password variables are<CODE>private</CODE> to prevent an outside class from accessingthem and jeopardizing the database connection, or compromisingthe secret user name and password information. You can find more information on this in the<A HREF="http://java.sun.com/docs/books/tutorial/java/javaOO/index.html">Objectsand Classs</A> lesson in<A HREF="http://java.sun.com/docs/books/tutorial">The Java Tutorial</A><A NAME="rw"></A><H4>Writing and Reading Data</H4>In the write operation, a <CODE>Statement</CODE> object is created from the <CODE>Connection</CODE>.The <CODE>Statement</CODE> object has methods for executing SQL queries and updates. Next, a <CODE>String</CODE> object that containsthe SQL update for the write operation is constructed andpassed to the <CODE>executeUpdate</CODE> method of the<CODE>Statement</CODE> object. </FONT><PRE>Object source = event.getSource();if(source == button){ JTextArea displayText = new JTextArea(); try{//Code to write to database   String theText = textField.getText();   Statement stmt = c.createStatement();   String updateString = &quot;INSERT INTO dba VALUES 			   ('&quot; + theText + &quot;')&quot;;   int count = stmt.executeUpdate(updateString);</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">SQL commands are <CODE>String</CODE> objects, and therefore, followthe rules of <CODE>String</CODE> construction where the stringis enclosed in double quotes (" ") and variable data is appended with a plus (+). The variable <CODE>theText</CODE> has single and double quotes to tell the database the SQL string has variable rather than literal data. <P>In the read operation, a <CODE>ResultSet</CODE> object is createdfrom the <CODE>executeQuery</CODE> method of the<CODE>Statement</CODE> object. The <CODE>ResultSet</CODE>contains the data returned by the query. To retrieve thedata returned, the code iterates through the <CODE>ResultSet</CODE>,retrieves the data, and appends the data to the text area,<CODE>displayText</CODE>. </FONT><PRE>//Code to read from database    ResultSet results = stmt.executeQuery(	&quot;SELECT TEXT FROM dba &quot;);    while(results.next()){      String s = results.getString(&quot;TEXT&quot;);      displayText.append(s + &quot;\n&quot;);    }    stmt.close();  } catch(java.sql.SQLException e){    System.out.println(e.toString());  }//Display text read from database  panel.removeAll();  panel.add(&quot;North&quot;, clicked);  panel.add(&quot;Center&quot;, displayText);  panel.add(&quot;South&quot;, clickButton);  panel.validate();  panel.repaint();}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="applet"></A><H3>Database Access by Applets</H3>The applet version of the example is like the application code described above except for the standard differences between applications and appletsdescribed in the <A HREF="applet.html#struct">Structure and Elements</A>section of Lesson 3.<P>However, if you run the applet without a policy file, you get a stack trace indicating permission errors.The <A HREF="data.html#sec">Granting Applets Permission</A> sectionin Lesson 6 introduced you to policy files and how to launch anapplet with the permission it needs. The Lesson 6 applet exampleprovided the policy file and told you how to launch the appletwith it. This lesson shows you how to read the stack trace todetermine the permissions you need in a policy file.<P>To keep things interesting, this lesson has two versionsof the database access applet: one uses the  JDBC driver,and the other uses the the JDBC-ODBC bridge with an Open DataBase Connectivity (ODBC) driver.<P>Both applets do the same operations to the same databasetable using different drivers. Each applet has its own policy filewith different permission lists and has differentrequirements for locating the database driver <A NAME="jdbc"></A><H4>JDBC Driver</H4>The JDBC driver is used from a program written exclusivelyin the Java language (Java program). Itconverts JDBC calls directly into the protocolused by the DBMS. This type of driver is available fromthe DBMS vendor and is usually packaged with the DBMSsoftware.<P><STRONG>Starting the Applet:</STRONG>To successfully run, the <A HREF="./Code/DbaAppl.java">DbaAppl.java</A> applet needs an available database driver and a policy file.This section walks through the steps to get everything set up. Here is the <CODE>DbaAppl.html</CODE> file for running the <CODE>DbaAppl</CODE> applet:</FONT><PRE>&#60;HTML&#62;&#60;BODY&#62;&#60;APPLET CODE=DbaAppl.class  WIDTH=200  HEIGHT=100&#62;&#60;/APPLET&#62;&#60;/BODY&#62;&#60;/HTML&#62;</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><P>And here is how to start the applet with appletviewer:</FONT><PRE>  appletviewer DbaAppl.html</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><P><STRONG>Locating the Database Driver:</STRONG>Assuming the driver is not available to the <CODE>DriverManager</CODE>for some reason, the following error generates when you click the <CODE>Click Me</CODE> button.  </FONT><PRE>  cannot find driver</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">This error means the DriverManager looked for the JDBC driver in thedirectory where the applet HTML and class files are and could not find it. To correct this error, copy the driver to the directorywhere the applet files are, and if the driver is bundled in a zip file, unzip the zip file so the applet can access the driver.<P>Once you have the driver in place, launch the applet again. </FONT><PRE>  appletviewer DbaAppl.html</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="perm"></A><STRONG>Reading a Stack Trace:</STRONG>Assuming the driver is locally available to the applet, if the <A HREF="./Code/DbaAppl.java">DbaAppl.java</A> appletis launched without a policy file, the following stack traceis generated when the end user clicks the <CODE>Click Me</CODE>button. </FONT><PRE>java.security.AccessControlException: access denied (java.net.SocketPermission developer resolve)</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The first line in the above stack trace tells you access is denied.This means this stack trace was generated because the applet tried to access a system resource without the proper permission. The second line means to correct this conditionyou need a <CODE>SocketPermission</CODE> that givesthe applet access to the machine (<CODE>developer</CODE>) wherethe database is located. <P>You can use Policy tool to create the policy file you need,or you can create it with an ASCII editor. Here is the policyfile with the permission indicated by the stack trace:</FONT><PRE>grant {  permission java.net.SocketPermission &quot;developer&quot;,     &quot;resolve&quot;;  &quot;accessClassInPackage.sun.jdbc.odbc&quot;;};</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><P>Run the applet again, this time with a policy file named<CODE>DbaApplPol</CODE> that has the above permission in it:</FONT><PRE>appletviewer -J-Djava.security.policy=DbaApplPol                                        DbaAppl.html</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">You get a stack trace again, but this time it is a differenterror condition.</FONT><PRE>  java.security.AccessControlException: access denied   (java.net.SocketPermission  129.144.176.176:1521 connect,resolve)</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Now you need a <CODE>SocketPermission</CODE> that allows access to theInternet Protocol (IP) address and port on the <CODE>developer</CODE>machine where the database is located.<P>Here is the <CODE>DbaApplPol</CODE> policy file with the permission indicated by the stack trace added to it:</FONT><PRE>grant {  permission java.net.SocketPermission &quot;developer&quot;,                        &quot;resolve&quot;;  permission java.net.SocketPermission   "129.144.176.176:1521", &quot;connect,resolve&quot;;};</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Run the applet again. If you use the above policy filewith the Socket permissions indicated, it works just fine.</FONT><PRE>  appletviewer -J-Djava.security.policy=DbaApplPol                                          DbaAppl.html</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="odbc"></A><H4>JDBC-ODBC Bridge with ODBC Driver</H4>Open DataBase Connectivity (ODBC) is Microsoft's programminginterface for accessing a large number of relational databases on numerous platforms. The JDBC-ODBC bridge is built into the Solaris and Windows versions of the Java platform so you can do two things:<OL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Use ODBC from a Java program</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Load ODBC drivers as JDBC drivers.  This example uses the JDBC-ODBC bridge to load an ODBC driverto connect to the database. The applet has no ODBC code, however.</FONT></OL><P>The <CODE>DriverManager</CODE> uses environment settings tolocate and load the database driver. For this example, the driver file does not need to be locally accessible.<P><STRONG>Start the Applet:</STRONG>Here is the <CODE>DbaOdb.html</CODE> file for running the <CODE>DbaOdbAppl</CODE> applet:</FONT>

⌨️ 快捷键说明

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