转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> ORACLE >> 正文
JavaBean操作Oracle数据库         ★★★★

JavaBean操作Oracle数据库

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1124 更新时间:2009/4/22 22:10:42

  本文以一个完整的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!!!”

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


 


[电脑应用]用C#动态创建Access数据库  [Web开发]asp 在线备份与恢复sqlserver数据库代码详解
[电脑应用]教你如何远程管理MSSQL数据库  [其他]关于数据库优化查询计划的方法总结
[电脑应用]Linux数据库大比拚  [JAVA开发]Java连接各种数据库的实例
[聊天工具]QQ IP数据库Build 0825 纯真版__天极Yesky  [系统软件]EXP-00008: ORACLE error 904 encountered的解决方…
[系统软件]利用crontab系统每天定时备份MySQL数据库  [系统软件]备份与恢复Windows2003的AD数据库
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台