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

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

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);
  }
}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

wormbug

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值