A summary of the use of JDBC access databases in Applet

xiaoxiao2021-03-06  39

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 Experience Data Table Structure [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.aw . 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 {JLabel text, clicked; 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 = (protocol = tcp)) (Source_Route = YES) (connection_data = (SID = ANSID) ) "; 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 design () {system.out.println ("Destroy Method Called.");} Public void actionPerformed Try {class.forname (_driver); c = drivermanager.getConnection (_URL);} catch (java.lang.classnotfoundexception e) {system.out.println ("cannot 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"; 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 statin "); 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 from Database} else {text: ("text to save to file:"); Textfield.Settext (""); button.Settext ("Click Me"); _ClickMemode = true;}}}} [/ code: 1: 8c94ca30e9] HTML file DBAAPPL.HTML [Code: 1: 8c94ca30e9] [/ code: 1: 8c94ca30e9] First, DBAAPPL.JAVA is specified in the _url string is Oracle with Web Server on the same host (Server1), compiles DBAAPPL.CLASS and DBAAPPL.html In the same directory of Web Server, visit DBAAPPL.html in the client's browser, see the Java console, get the following information [code: 1: 8c94ca30e9] Cannot Find Driver Oracle.jdbc.driver.Oracledriver java.lang This error message means that DRIVERMANAGER has not found a JDBC driver in the local classpath where the applet html and class files are located and the current JRE used. 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 map accesses system resources without obtaining correct permissions, the code in parentheses, to correct this, you need a computer (host name server1) to the database (host name Server1) to access Applet Access SocketPermission for permissions. 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] The DBAAPPL.html again is successfully run. 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, there is a need to successfully run the applet client to modify its local JRE's Java.Security file [/ list: o: 8c94ca30e9] [Post Reply] [View Forum original] [Add to Favorites] [Close] eudemon Reply to: 2004-02-10 13:36:47 What you said, there are many books, this all revised local policy files involving Socket, files, NET, but with the modification Some security things in the policy file have also been followed, who can guarantee whether the procedure you run is safe, and others will not take advantage of it. Even if there is a digital signature, it is not necessarily insurance.

Wolfg Reply to: 2004-02-10 14:33:17 [quote: 9C7806ABC8 = "eudemon"] You said these things, there are many books, this kind of operation involving socket, file, NET, etc. Modify local policy files, but with some security of the modified policy file, anyone can guarantee whether the program you run is safe, and others will not take advantage of it. Even if there is a digital signature, it is not necessarily insurance. [/ quote: 9C7806ABC8] Oh, of course, many books have :), I just help everyone summarize a little bit, let alone, I don't want to use Applet to connect Database, each takes the needs. The technology is to serve people, what is suitable for you, even if Sun Na is reply to: 2004-02-10 21:20:58 Thank you for the summary of the landlord. There is a problem, you need to ask, there is such a request, that is, a JSP page needs to display 6-8 Applet windows. Each applet is to query a stock price from the database and based on the price. Draw an image in the applet, and you cannot get the new image of the applet by refreshing the JSP page, requiring the timing refresh of the applet (such as 30 seconds to brush once, wait until a new price), the most difficult requirement is not in the client anything. Is this required to use Applets that cannot be implemented? If so, can there be an instead of the scheme?

Wolfg Reply to: 2004-02-10 22:28:50 You can do a servlet to get stock information, generate XML data, access this servlet when the applet is fixed, parsing the obtained XML data, plotting the image; or uses WebService

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

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