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

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

jsp+beans实现读取数据库的例子

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 25次 | 进入软件测试论坛讨论

领测软件测试网 jsp+beans实现读取数据库的例子
Select.jsp

<html>
<head>
<title>Select everything from a database</title>
</head>
<body>

<jsp:useBean id="select" class="DataBaseSelect" scope="request">
</bean>

<% out.print(select.connect()); %>
<br>
<% out.print(select.select()); %>

<p>Format results

<br>
<%@ page import="java.util.Vector" %>
<% Vector aResult = select.getResult(); %>

<table>
<% for (int i=0; i < aResult.size(); i++) { %>
   <tr>
       <td>
           <% out.print(aResult.elementAt(i)); %>
       </td>
   </tr>
<% } %>
</table>

</body>
</html>

DataBaseSelect.java
import java.sql.*;
import java.util.Vector;

/**
* DataBaseSelect.java
* Written by Morgan Catlin
*   August 19, 1999
*
* Variables:
*   Vector result = where I put the results of a select query
*
* Methods:
*   Vector getResult() = returns result
*   void setResult() = sets result
*   String connect() = connects to a database
*   String select() = selects information from a database
*/

public class DataBaseSelect {
   
   private Vector result;
     
   public DataBaseSelect() {
      result = new Vector();
   } // constructor DataBaseSelect
   
   public String connect() {
      try {
     Class.forName("a jdbc driver").newInstance();
     return "Driver Loaded!";
      } catch (Exception E) {
     return "Unable to load driver.";
      }
   }
   
   public String select() {
      try {
     Connection C = DriverManager.getConnection("database connection string");
     
     Statement Stmt = C.createStatement();
     
     ResultSet myResult = Stmt.executeQuery("SELECT acolumn from atable");
     
     while (myResult.next()) {
        result.addElement(myResult.getString(1));
     }
     
         // Clean up
         myResult.close();
         Stmt.close();
         C.close();
     return "Connection Success!";
      } catch (SQLException E) {
     return "SQLException: " + E.getMessage();
      }
   }
      
   /**
    * Accessor for result
    **/
   public Vector getResult() {
      return result;
   }
   
   /**
    * Mutator for result
    **/
   public void setResult(Vector avector) {
     result = avector;
   }  
   
} // class DataBaseSelect

延伸阅读

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


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

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网