Use JDBC access database in Applet

xiaoxiao2021-03-06  52

A little summary about the author http://www.chinaunix.net access the database using JDBC applet in the: wolfg Posted: 2004-02-10 11:04:50

Recently, I saw a few posts about the use of JDBC accessing the database in Applet, I feel that the heroes have a response very absolutely, and the applet cannot access the database, and don't dare to share, so refer to Sun's online tutorial [URL = HTTP : //cn.sun.com/developrs/onlineTraining/deramming/basicjava1/data.html] Java programming language foundation: Practice Guide, Part 1 [/ URL] ", have made some experiments, summed up to share with you, Please also advise.

Experimental Environment DB Server # 1: Solairs 8 Oracle 8.1.7 Apache DB Server # 2: Solairs 8 Oracle 8.1.7 Client: Windows2000 SP4 J2SE1.4.1 Oracle JDBC Driver

The structure of the data sheet used in the experiment [CODE: 1: 8c94ca30e9] Table DBA (Text Varchar2 (100), Primary Key (Text)) [/ Code: 1: 8c94ca30e9]

Applet Source Code [CODE: 1: 8C94CA30E9] Import java.awt.color; import java.awt.BorderLayout; import java.awt.event. *; import java.applet.applet; import javax.swing. *; import java . SQL. *; Import java.net. *; import java.io. *;

Public Class DBAAPPL EXTENDS Applet Implements ActionListener {

JButton Button, ClickButton; JtextField Textfield; Private Boolean _ClickMemode = true; private connection c;

final static private String _driver = "oracle.jdbc.driver.OracleDriver"; final static private String _url = "jdbc: oracle: thin: username / password @ (description = (address_list = (address = (protocol = tcp) (host = Developer))))) (connection_route = yes) ";

Public void init () {setBackground (color.white); text = new jlabel ("text retrieved from file:"); button = new jbutton ("Click ME "); button.addActionListener (this); clickButton = new JButton (" Click Again "); clickButton.addActionListener (this); textField = new JTextField (20); setLayout (new BorderLayout ()); setBackground (Color.white) Add (borderLayout.North, Text); add (borderLayout.center, TextField); add (borderLayout.South, Button);} public void start () {system.out.println ("applet starting.");}

Public void stop () {system.out.println ("applet stopping.");

Public void destroy () {system.out.println ("Destroy Method Called.");

public void actionPerformed (ActionEvent event) {try {Class.forName (_driver); c = DriverManager.getConnection (_url);} catch (java.lang.ClassNotFoundException e) {System.out.println ( "Can not find driver"); System.out.println (E.GetMessage ());} catch (java.sql.sqlexception e) {system.out.println ("cannot get connection"); system.out.println (E.getMessage ()); }

Object source = event.getSource (); if (source == button) {if (_clickMeMode) {JTextArea displayText = new JTextArea (); try {// Write to database String theText = textField.getText (); Statement stmt = c .createStatement (); String updateString = "INSERT INTO dba VALUES ( '" theText "')"; int count = stmt.executeUpdate (updateString); // Read from database ResultSet results = stmt.executeQuery ( "SELECT TEXT fROM DBA "); while (results.next ()) {string s = results.getstring (" text "); DisplayText.Append (S " / n ");} stmt.close ();} catch (java.sql .Sqlexception e) {System.out.println ("cannot create SQL Statement"); System.out.Println (E.GetMessage ());} // Display Text Read from Database Text.Settext ("Text Retrieved from File: "); Button.Settext (" Click Again "); _ClickMemode = false; // Display Text Read fr om Database} else {text.setText ("Text to save to file:"); Textfield.Settext (""); button.Settext ("Click Me"); _ClickMemode = true;}}}} [/ code: 1 : 8C94CA30E9]

HTML file running this applet DBAAPPL.HTML [Code: 1: 8c94ca30e9]

[/ code: 1: 8c94ca30e9]

First, DBAAPPL.JAVA is specified in the _url string is the Oracle with Web Server (Server1), and then put DBAAPPL.CLASS and DBAAPPL.html in the same directory of Web Server, in the client Access DBAAPPL.html in the browser, view the Java console, get the following information [CANNOT FIND DRIER ORA30E9] Cannot Find Driver Oracle.jdbc.driver.Oracledriver java.lang.nullpointerException [/ code: 1: 8c94ca30e9] This error message It means that DriverManager has not found a JDBC driver in the directory where applet html and class files are located and the current JRE used locally in the local ClassPath. Solution, put the JDBC driver Oracle-jdbc-driver.jar file in the $ {jre_home} / lib / ext directory of the client, or use the JAR tool to extract oracle-jdbc-driver.jar, The Oracle directory obtained after decompression is also placed in the directory where the applet is located on the web server. After correcting, re-accessing DBAAPPL.html, check the Java console, get the following information [CODE: 1: 8c94ca30e9] java.security.accessControlException: Access Denied (java.net.socketpermission Server1 resolve) [/ code: 1: 8c94ca30e9] Wrong informs you to access. That is to say, because the applet program is trying to access system resources without obtaining correct permissions, the code in parentheses indicates that if this is correct, you need a computer (host name Server1) to the database (host name Server1) to applet access SocketPermission.

Solution, use the Policy tool to generate the policy file you need, or generate the policy file with an ASCII editor. The following code is the content named DBAApplpol, the content of the policy file obtained by the above permissions [Code: 1: 8c94ca30e9] Grant {Permission Java.Net.SocketPermission "Server1", "resolve";}; [/ code: 1: 8c94ca30e9] Once you have created a policy file, you have to modify $ {jre_home} /lib/security/java.security file in [Code: 1: 8c94ca30e9] policy.url.1 = file: $ {java.home} / lib / security / java .policy policy.url.2 = file: $ {user.home} /. java.policy [/ code: 1: 8c94ca30e9] Add a line [Code: 1: 8c94ca30e9] policy.url.3 = file: / path / To / DBaApplpol [/ Code: 1: 8c94ca30e9]

Access DBAAPL.html again and run successfully.

Then change the _URL string in DBAAPPL.JAVA to the specified Oracle, compile, upload, and access to the following error message [Code: 1: 8c94ca30e9] java.security.accessControlException: Access Denied (Java. Net. Socketpermission 10.6.1.16:1521 Connect, Resolve) [/ code: 1: 8c94ca30e9] This means that a SocketPermission that allows access to IP addresses and ports on Server2 in the database. Add [CODE: 1: 8C94CA30E9] permission java.net.SocketPermission java.net.socketpermission "10.6.1.16:1521", "Connect, Resolve"; [/ code: 1: 8c94ca30e9] is successfully run. Summarize, access the database through the applet is not not possible, but it is really less convenient, and some special environments may be used. [List = 1: 8c94ca30e9] [*: 8C94CA30E9] The client has JRE support, you can access the applet in the client to be prompted to install Java Plug-in, such a page can generate [*: 8C94CA30E9] JDBC driver via HTMLCONVERT tool Either placed under the client JRE ClassPath, or put together with Applet, pay attention to if the driver is a JAR file, to unwinder [*: 8c94ca30e9] By setting the correct policy file, Applet can also access the same machine in the same machine. The database can also access other remote databases. However, you need a client that you want to successfully run the applet to modify its local JRE's Java.security file [/ list: o: 8c94ca30e9]

转载请注明原文地址:https://www.9cbs.com/read-117904.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.057, SQL: 9