JBUILDER X+Weblogic8.1+SQL Server2000两种数据库连接方法

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

代码中有数据库直连 和 数据池两种方法

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.*;

/**

* <p>Title: </p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2004</p>

* <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");//此处根据你的SQLServer帐户而定。

Statement st = con.createStatement();

ResultSet res = st.executeQuery("select * from admin");

String line = "";

while (res.next())

line = line + res.getString("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.getString("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);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航