There are two ways of database straight and data pool in the code.
Package my_j2ee;
Import java.awt. *; import java.awt.event. *; import javax.swing. *; import java.sql. *; import javax.naming. *; import javax.sql.datasource; import java.util. * ; / ** *
Title: p> *
Description: p> *
Copyright: Copyright (c) 2004 p> *
Company: p> * @Author NOT Attributable * @version 1.0 * /
public class Frame1 extends JFrame {JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout (); JScrollPane jScrollPane1 = new JScrollPane (); JTextArea jTextArea1 = new JTextArea (); JButton jButton1 = new JButton (); JButton jButton2 = new JButton (); JButton JButton3 = new jbutton ();
// construct the frame public frame1 () {enableevents (awtevent.window_event_mask; try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}}
// Component initialization private void jbInit () throws Exception {contentPane = (JPanel) this.getContentPane (); contentPane.setLayout (borderLayout1); this.setSize (new Dimension (421, 341)); this.setTitle ( "Frame Title "); jTextArea1.setText (" jTextArea1 "); jButton1.setText (" jButton1 "); jButton1.addActionListener (new Frame1_jButton1_actionAdapter (this)); jButton2.setText (" jButton2 "); jButton2.addActionListener (new Frame1_jButton2_actionAdapter (this) ); jButton3.setText ( "jButton3"); contentPane.add (jTextArea1, BorderLayout.NORTH); contentPane.add (jScrollPane1, BorderLayout.WEST); contentPane.add (jButton2, BorderLayout.EAST); contentPane.add (jButton3, BorderLayout.center; jscrollpane1.getViewPort (). Add (jbutton1, null);}
// Overridden so we can exit when window is closed protected void processWindowEvent (WindowEvent e) {super.processWindowEvent (e); if (e.getID () == WindowEvent.WINDOW_CLOSING) {System.exit (0);}} void JButton1_ActionPerformed (ActionEvent E) {Try
{Class.Forname ("WebLogic.jdbc.mssqlserVer4.driver"); Connection Con = DriverManager.getConnection ("JDBC: WebLogic: MSSQLServer4: J2EE @ localhost: 1433", "SA", "Jovial"); // Depending on your SQL Server account. Statement St = Con.CreateStatement (); ResultSet Res = St.executeQuery ("Select * from admin"); string line = ""; while (res.next ()) line = line res. maxString ("password") "/ n"; jtextarea1.settext (line); con.close ();} catch (exception ex) {jtextarea1.settext ("error:" ex.getMessage ());}}
void jButton2_actionPerformed (ActionEvent e) {Hashtable ht = new Hashtable (); ht.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); ht.put (Context.PROVIDER_URL, "t3: // localhost: 7001") ; try {Context ctx = new InitialContext (ht); DataSource ds = (DataSource) ctx.lookup ( "J2EEMSSQL"); Connection con = ds.getConnection ( "wormbug", "jovial521"); Statement st = con.createStatement ( ); Resultset res = st.executeQuery ("SELECT * from admin"); string line = ""; while (res.next ()) line = line res. maxString ("name") "/ n"; jtextarea1 .setText (line); con.close ();} catch (Exception ex) {jTextArea1.setText ( "error:" ex.getMessage ());}}} class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {Frame1 Adaptee;
Frame1_jbutton1_actionadapter (frame1 adaptee) {this.adaptee = adaptee;} public void actionPerformed (ActionEvent E) {adaptee.jbutton1_actionperformed (e);}}
Class frame1_jbutton2_actionAdapter Implements java.awt.event.ActionListener {frame1 adaptee
Frame1_jButton2_ActionAdapter (frame1 adaptee) {this.adaptee = adaptee;} public void actionPerformed (ActionEvent E) {adaptee.jbutton2_actionperformed (e);}}