您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 > junit
Junit单元测试简单小例子
作者:帅姐 发布时间:[ 2016/8/15 14:09:32 ] 推荐标签:单元测试 Junit

  一、编写一个web程序
  本文根据工作实际项目,编写一个web程序:实现防伪码查询页面,输入防伪码,点击查询,查询数据库并作出判断,将结果显示在查询页面。

  1、需要添加的buildpath:ojdbc14.jarclasses12.jar

  2、编写searchresult.jsp文件:(刚入门格式不规范)
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2 <!--%
3     String path = request.getContextPath();
4     String basePath = request.getScheme() + "://"
5             + request.getServerName() + ":" + request.getServerPort()
6             + path + "/";
7 %>-->
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <%@ page import="java.sql.*"%>
10 <%@ page import="testsample001.check"%>
11 <html>
12 <head>
13 <!--base href="<!--%=basePath%>">-->
14 <title>查询结果</title>
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--<link rel="stylesheet" type="text/css" href="styles.css"> -->
21 </head>
22 <body>
23     <br>
24     <br>
25     <br>
26     <table>
27         <tr>
28             <td>
29                 <form action="/testsample001/testservlet" method="post">
30                     防伪码
31                     <!--input name="fangweima" id="t1" type="text">-->
32                     <!--input name="searchbtn" type="button" value='查询' onclick="check1()">-->
33                     <!--input name="result" id="t2" type="text"-->
34                     <input type="text" id="t1" name="t1" value="<%=session.getAttribute("t1")%>" />
35                     <!--input type="text" id="t2" name="t2" value="<%=session.getAttribute("t2")%>"/>-->
36                     <input type="submit" value="查询"/>
37                     <br>
38                     查询结果:<%=session.getAttribute("t2")%>;
39                 </form>
40             </td>
41         </tr>
42     </table>
43 </body>
44 </html>
  3、编写check.java文件
1 package testsample001;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 public class check {
10     private Connection con;
11     private String user = "deejuser";
12     // private String user = "sys as sysdba";
13     private String password = "deejuser";
14     private String className = "oracle.jdbc.driver.OracleDriver";
15     // private String url="jdbc:oracle:oci@localhost:1158:orcl";这个url可能无效
16     private String url = "jdbc:oracle:thin:@zs-PC:1521:ytdf";
17
18     public String ConnectOracle(String productno) {
19         try {
20             Class.forName(className);
21             // System.out.println("加载数据库驱动成功!");
22             // System.out.println(productno);
23             String resultInfo = getCon(productno);
24             closed();
25             return resultInfo;
26         } catch (ClassNotFoundException e) {
27             // System.out.println("加载数据库驱动失败!");
28             e.printStackTrace();
29             return "加载数据库驱动失败!";
30         }
31     }
32
33     /** 创建数据库连接 */
34     // public Connection getCon(String productno) {
35     public String getCon(String productno) {
36         try {
37             con = DriverManager.getConnection(url, user, password);
38             // System.out.println("创建数据库连接成功!");
39             Statement stmt = con
40                     .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
41                             ResultSet.CONCUR_UPDATABLE);
42             String sql = "select * from product t where t.packagsn="
43                     + productno;
44             // System.out.print(sql);
45             ResultSet rs = stmt.executeQuery(sql);
46             if (rs.next())
47                 return "防伪码存在!";
48             else
49                 return "防伪码不存在!";
50         } catch (SQLException e) {
51             // System.out.print(con);
52             // System.out.println("创建数据库连接失败!");
53             con = null;
54             e.printStackTrace();
55             return "数据库连接失败!";
56         }
57         // return con;
58     }
59
60     public void closed() {
61         try {
62             if (con != null) {
63                 con.close();
64             }
65         } catch (SQLException e) {
66             System.out.println("关闭con对象失败!");
67             e.printStackTrace();
68         }
69     }
70 }
  4、编写testservlet.java文件
1 package testsample002;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import testsample001.check;
12
13 /**
14  * Servlet implementation class testservlet
15  */
16 @WebServlet("/testservlet")
17 public class testservlet extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19
20     /**
21      * @see HttpServlet#HttpServlet()
22      */
23     public testservlet() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27
28     /**
29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
30      *      response)
31      */
32     protected void doGet(HttpServletRequest request,
33             HttpServletResponse response) throws ServletException, IOException {
34         // TODO Auto-generated method stub
35     }
36
37     /**
38      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
39      *      response)
40      */
41     protected void doPost(HttpServletRequest request,
42             HttpServletResponse response) throws ServletException, IOException {
43         // TODO Auto-generated method stub
44         String value = request.getParameter("t1");// 获取前台页面传递过来的数据
45         // request.getSession(false).setAttribute("t1", value);// 将数据放到session中
46         check connor = new check();
47         String resultInfo = connor.ConnectOracle(value);
48         request.getSession(false).setAttribute("t2", resultInfo);// 将数据放到session中
49         request.getSession(false).setAttribute("t1", value);
50         response.sendRedirect("/testsample001/searchresult.jsp");// 跳转回提交的页面
51
52     }
53
54 }
  5、设置项目的起始页面(tomcat服务配置-web.xml)

  1     <welcome-file-list>
  2         <welcome-file>index.html</welcome-file>
  3         <welcome-file>index.htm</welcome-file>
  4         <welcome-file>searchresult.jsp</welcome-file>
  5     </welcome-file-list>
  6、实现结果:

  7、导出jar包(在要生成jar的项目上右击,选择菜单上的Export(导出)),导出oracleconnect.jar

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd