• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

通用数据库操作BEAN

发布: 2007-7-14 21:19 | 作者: 佚名    | 来源: 网络转载     | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网 通用数据库操作BEAN

package pkgCDB;

import java.text.*;
import java.util.*;
import java.sql.*;

public class clsCDB
{
String sDBDriver = "";
   String sConnStr = "";
   String sHint = "";
   ResultSet rs = null;
   Connection conn = null;
   Statement stmt = null;

   public String getHint()
   {
return sHint;
   }

   public void setDBDriver(String s)
   {
     sDBDriver = "" + s;
   }

   public String getDBDriver()
   {
     return sDBDriver;
   }

   public String getConnStr()
   {
     return sConnStr;
   }

   public void setConnStr(String s)
   {
     sConnStr = "" + s;
   }

   public ResultSet getResultSet()
   {
     return rs;
   }

   //Initialize the database
   public boolean Initialization(String dbDrive,String dbConn)
   {
     sDBDriver = dbDrive;
     sConnStr = dbConn;
return Initialization();
   }

   public boolean Initialization()
   {
try
     {
       Class.forName(sDBDriver);
       sHint = "Initialization sucessfully";      
       return true;
}
catch(java.lang.ClassNotFoundException e) {
sHint = "Initialization : " + e.getMessage();
       return false;
}
   }

//A common function perhaps usable
   public String chkStr(String inStr)
   {
     StringBuffer result = new StringBuffer("");
     char cc;

     if (inStr != null)
     {
     for (int i = 0; i < inStr.length(); i++)
     {
       cc = inStr.charAt(i);
       if (cc == ´\´´)
         {
         result.append(´\´´);
       }
       result.append(cc);
     }
}
return result.toString();
   }

//execute a SQL statement with recordset returned
   public ResultSet executeQuery(String sql)
   {
     rs = null;
     try
     {
     conn = DriverManager.getConnection(sConnStr);
       stmt = conn.createStatement();
       rs = stmt.executeQuery(sql);
     }
     catch(SQLException ex)
     {
     sHint = "executeQuery: " + ex.getMessage();
     }
     return rs;
   }

//execute a SQL statement without returning recordset
   public boolean executeUpdate(String sql)
   {
     try
     {
     conn = DriverManager.getConnection(sConnStr);
       stmt = conn.createStatement();
       stmt.executeUpdate(sql);
       return true;
     }
     catch(SQLException ex)
     {
     sHint = "executeUpdate: " + ex.getMessage();
       return false;
     }
   }

//Close connections
   public boolean closeConn()
   {

  try
     {
  if (rs!=null)
       rs.close();
  if (stmt!=null)
       stmt.close();
   if (conn!=null)
       conn.close();
       return true;
   }
     catch(SQLException ex)
     {
sHint = "closeConn: " + ex.getMessage();
       return false;
     }
   }
}



测试用JSP文件

<%@ page language="java" import="java.sql.*" %>
<%@ page import = "pkgCDB.clsCDB"%>
<jsp:useBean id="CommonDBBean" class="pkgCDB.clsCDB" scope="session"/>

<html>
<head><title>Common Database Bean Test</title></head>
<body>
<font size=4>
<%
if ( (request.getParameter("DBDriver") == null)
   // (request.getParameter("ConnStr") == null)
     // (request.getParameter("SQLStr") == null) )
   { %>
  <form method=get>
  Input DBDriver: <input type=text name=DBDriver value="sun.jdbc.odbc.JdbcOdbcDriver">
     <BR>
  Input Connection String: <input type=text name=ConnStr value="jdbc:odbc:testBean">


  Input SQL Query: <input type=text name=SQLStr value="select * from jobs">
     <BR>
  <input type=submit value="Submit">
  </form>

<% }
else
   { %>
<% if (!CommonDBBean.Initialization(request.getParameter("DBDriver"),request.getParameter("ConnStr")))
   { %>
   <%=CommonDBBean.getHint() + "<p></p>" %>
<% }
else
   { %>
   <% ResultSet rs; %>
     <% rs = CommonDBBean.executeQuery(request.getParameter("SQLStr")); %>
     <%int j = rs.getMetaData().getColumnCount(); %>
     <table BORDER = "1">
<tr>
     <%for (int i = 1; i <= j; ++i)
     { %>
     <td>
       <%=rs.getMetaData().getColumnName(i) %>
       </td>
<% } %>
</tr>

     <%while(rs.next())
     { %>
     <tr>
     <%for (int i = 1;i <= j; ++i)
       { %>
<td>
         <%=rs.getString(i)%>
         </td>
<% } %>
</tr>
<% } %>
</table>
<% }
}
   if (!CommonDBBean.closeConn())
   { %>
   <%=CommonDBBean.getHint() + "<p></p>" %>
<% } %>
</font>
</body>
</html>

作者:zergman
email: zergman@chinaasp.com

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网