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]