国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

JavaBean操作Oracle數據庫

2024-08-29 13:31:38
字體:
來源:轉載
供稿:網友
中國最大的web開發資源網站及技術社區,

本文以一個完整的javabean數據庫訪問程序簡要說明jsp操作數據庫。
  本程序由3個bean組成,其中webconstants中定義全局變量,connectionmanager管理數據庫連接,mainbean利用webconstants和connectionmanager操作數據庫。

首先定義全局變量,如下:

package webrelease;

public interface webconstants
{
  public static final string driverclass ="driverclass";
  const userid is the user id to connect to database
  public static final string userid ="comm";
  const passwd is the user password to connect to database
  public static final string passwd ="comm123";
  const url is the url to connect to database
  public static final string url="jdbc:oracle:thin:@10.2.0.1:1521:orcl";
  public static final string selecttype ="select";
  public static final string connection ="connection";
  public static final string connerror ="conerror";
}

接著創建一個數據庫連接管理bean,如下:

package webrelease;

import java.io.*;
import java.beans.*;
import java.util.*;
import java.sql.*;
import webrelease.webconstants;
import oracle.jdbc.driver.*;

public class connectionmanager implements webconstants
{
  private boolean debug = true;
  protected connection con;
  protected debugwriter writer;
  propertychangesupport pcs;
////////////////////////////////////////////////////////////////////////////////
  public connectionmanager()
  {
    pcs = new propertychangesupport(this);
    writer = new debugwriter();
  }
////////////////////////////////////////////////////////////////////////////////
  public void setdebug(string b)
  {
    debug = b.equals("true");
  }
////////////////////////////////////////////////////////////////////////////////
  public void addpropertychangelistener( propertychangelistener l)
  {
    pcs.addpropertychangelistener(l);
  }
////////////////////////////////////////////////////////////////////////////////
  public void removepropertychangelistener(propertychangelistener l)
  {
    pcs.removepropertychangelistener(l);
  }
////////////////////////////////////////////////////////////////////////////////
  public void login()
  {
    try
    {
      drivermanager.registerdriver(new oracle.jdbc.driver.oracledriver());
      class.forname("oracle.jdbc.driver.oracledriver");
    }
    catch(exception e)
    {
      if(debug) {  writer.writedebug("error setting driver:"+e.getmessage());}
    }
    try
    {
      con = drivermanager.getconnection(url,userid,passwd);
      pcs.firepropertychange(connection,null,con);
      if(debug)
      {
        writer.writedebug("connection succeded ! url:"+url+"user:"+userid+
                          "pwd:"+passwd);
      }
    }
    catch(exception e)
    {
      pcs.firepropertychange(connerror,null,e);
      if(debug)
      {
        writer.writedebug("connection failed ! url:"+url+"user:"+userid+
                          "pwd:"+passwd);
      }
    }

  }
}

在mainbean中,偵聽數據庫是否連接,如果連接則對數據庫進行操作:

package webrelease;

import java.io.*;
import java.beans.*;
import java.util.*;
import java.sql.*;
import webrelease.*;

public class mainbean implements propertychangelistener ,webconstants
{
  private boolean debug = true;
  protected connection con;
  protected debugwriter writer;
////////////////////////////////////////////////////////////////////////////////
  public mainbean()
  {
  writer = new debugwriter();
  }
////////////////////////////////////////////////////////////////////////////////
  public void propertychange(propertychangeevent evt)
  {
  string prop = evt.getpropertyname();
 //see if we got a connection
  if(prop.equals(connection))
    {//ok so update the local connection
      try
      {
        //make sure it really is a connection
        con = (connection)evt.getnewvalue();
      }
      catch(exception e)
      {
        writer.writedebug("error connecting to database"+e.getmessage());
      }
    }
  }
////////////////////////////////////////////////////////////////////////////////
  public void setconnectionmanager(connectionmanager cm)
  {
    if(cm != null)
    {
      // to remove the old listener from cm
      // to avoid confusion in the mainbean
      cm.removepropertychangelistener(this);
      cm.addpropertychangelistener(this);
      if(debug)
      {
        writer.writedebug("mainbean;set connection manager"+ cm.tostring());
      }
    }
    else
    {
     if(debug)
      {
        writer.writedebug("mainbean;tried to set a null connection manager");
      }
    }
  }
////////////////////////////////////////////////////////////////////////////////
  public boolean isconnected()
  {
    //see if datatabe is connected
    return (con != null);
  }
////////////////////////////////////////////////////////////////////////////////
 public void processquery()
 {
 try
 {
  statement stmt=con.createstatement();
resultset rs=stmt.executequery("select * from hello order by id");
 writer.writedebug("select * from hello order by id");
while(rs.next())
{
writer.writedebug("next");
string str=rs.getstring("name");
writer.writedebug(str+"  &nbsp   ");
}
rs.close();
}
catch(exception e)
{
}


 }
}

 

ok,現在我們來測試一下這個程序,創建一個測試頁面:

<%@ page contenttype="text/html; charset=gb2312" %>


<html>
<head>
<title>
hello
</title>
</head>
<body bgcolor="#ffffc0">
<h1>
<jsp:usebean id="main" class="webrelease.mainbean" scope ="session"/>
<jsp:usebean id="con" class="webrelease.connectionmanager" scope ="session"/>
<%
main.setconnectionmanager(con);
con.login();
%>
<%
if(main.isconnected())
out.print("mainbean connect success!!!");
main.processquery();

%>

</h1>


</body>

</html>

在tomcat中瀏覽這個頁面,我們應該可以看到輸出“mainbean connect sucess!!!”

注釋不是太多,希望你能看明白。


 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐山县| 怀宁县| 泰和县| 呈贡县| 舟曲县| 兴宁市| 会东县| 奉新县| 城市| 清新县| 三门县| 普安县| 手游| 司法| 高要市| 远安县| 连平县| 海伦市| 鄯善县| 山西省| 邹平县| 英吉沙县| 新兴县| 兴城市| 中超| 枣阳市| 汕尾市| 苍溪县| 定边县| 五原县| 五寨县| 同仁县| 奇台县| 宿迁市| 岚皋县| 大连市| 山东省| 庄河市| 陇西县| 滕州市| 古交市|