Java Web MVC实例

开发环境

Eclipse Java EE IDE for Web Developers.

Version: Luna Service Release 2 (4.4.2)

tomcat:7.0

数据库:oracle

 

需求

利用jsp MVC 模式读取oracle数据库中的EMP表的数据

 

第一步:建立web工程

1)在Eclipse工程领域的空白处鼠标右键,New --> Dynaminc Web Project

2)Project Name:web01

    点击下一步

3)下一步

4)打钩后下一步

 5)web工程建立完成

 

第二步:建立测试页并发布到tomcat

1)建立测试页面

新建jsp页面,在jsp页面中写上index jsp

2)发布web工程

3)启动tomcat

4)启动完成

5)访问jsp页面

我的端口是8081

 

第三步:oracle连接配置及测试

1)oracle lib文件拷贝(本人oracle安装在d盘下,D:\oracle\product\10.2.0\db_1\jdbc\lib)

 2)粘贴到WEB-INF/lib下面

3)写一个测试类测试一下是不是数据库连接成功

在src下面建立DBConnection.java

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 
 6 public class DBConnection {
 7     // 连接Oracle数据库
 8     public void OracleConnection() {
 9         Connection con = null;
10         PreparedStatement pre = null;
11         ResultSet rs = null;
12 
13         try {
14             // 1. 加载Oracle驱动程序
15             Class.forName("oracle.jdbc.driver.OracleDriver");
16 
17             // 2. 设置Oracle数据库基本信息
18             String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
19             String user = "scott";
20             String password = "goodluck";
21 
22             // 2. 获取连接
23             con = DriverManager.getConnection(url, user, password);
24             System.out.println("----> Connection Success!");
25 
26             // 3. 执行SQL语句
27             String sql = "SELECT * FROM EMP";
28             pre = con.prepareStatement(sql);
29 
30             // 4. 获取结果集
31             rs = pre.executeQuery();
32             while (rs.next()) {
33                 System.out.println("编号:" + rs.getString("empno") + ";姓名:"
34                         + rs.getString("ename") + "; 工作:" + rs.getString("job")
35                         + "; 领导:" + rs.getString("mgr") + "; 雇佣日期:"
36                         + rs.getString("hiredate") + "; 工资:"
37                         + rs.getString("sal") + "; 奖金:" + rs.getString("comm")
38                         + "; 部门:" + rs.getString("deptno"));
39             }
40         } catch (Exception e) {
41             e.printStackTrace();
42         } finally {
43             try {
44                 if (rs != null)
45                     rs.close();
46                 if (pre != null)
47                     pre.close();
48                 if (con != null)
49                     con.close();
50                 System.out.println("----> Connection End <-----");
51             } catch (Exception e) {
52                 e.printStackTrace();
53             }
54         }
55     }
56 
57     public static void main(String[] args) {
58         DBConnection db = new DBConnection();
59         db.OracleConnection();
60     }
61 }

右键Run As --> Java Application

控制台打出下面信息说明连接成功!

 

第四步:共同程序

1)连接数据库程序

 1 package com.test.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class BaseDao {
10     Connection conn = null;
11     PreparedStatement pre = null;
12     ResultSet rs = null;
13 
14     public BaseDao() {
15         try {
16             // 1. 加载Oracle驱动程序
17             Class.forName("oracle.jdbc.driver.OracleDriver");
18         } catch (ClassNotFoundException e) {
19             e.printStackTrace();
20         }
21     }
22     
23     public Connection dbConnection(){
24         // 2. 设置Oracle数据库基本信息
25         String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
26         String user = "scott";
27         String password = "goodluck";
28 
29         // 3. 获取连接
30         try {
31             conn = DriverManager.getConnection(url, user, password);
32             System.out.println("----> Connection Success!");
33         } catch (SQLException e) {
34             e.printStackTrace();
35         }
36         return conn;
37     }
38 
39     public void dbDisconnection(){
40         try {
41             if (rs != null)
42                 rs.close();
43             if (pre != null)
44                 pre.close();
45             if (conn != null)
46                 conn.close();
47             System.out.println("----> Connection End <-----");            
48         } catch (Exception e) {
49             e.printStackTrace();
50         }
51     }
52 }

 

第五步:编写业务程序

1)controller

 1 package com.test.controller;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import com.test.bean.EmpBean;
13 import com.test.service.EmpService;
14 
15 /**
16  * Servlet implementation class EmpController
17  */
18 @WebServlet("/EmpController")
19 public class EmpController extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     /**
23      * @see HttpServlet#HttpServlet()
24      */
25     public EmpController() {
26         super();
27     }
28 
29     /**
30      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
31      */
32     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33         EmpService es = new EmpService();
34         EmpBean eb = new EmpBean();
35         
36         eb.setEname(request.getParameter("searchTxt"));
37         ArrayList<EmpBean> empBean = es.getEmpList(eb);
38         
39         request.setAttribute("empBean", empBean);
40         request.getRequestDispatcher("/test/empList.jsp").forward(request, response);
41     }
42 
43     /**
44      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
45      */
46     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47         this.doGet(request, response);
48     }
49 
50 }

2)bean

 1 package com.test.bean;
 2 
 3 public class EmpBean {
 4     private int empNo;
 5     private String ename;
 6     private String job;
 7     private int mgr;
 8     private String hiredate;
 9     private double sal;
10     private double comm;
11     private int deptno;
12     
13     public int getEmpNo() {
14         return empNo;
15     }
16 
17     public void setEmpNo(int empNo) {
18         this.empNo = empNo;
19     }
20 
21     public String getEname() {
22         return ename;
23     }
24     
25     public void setEname(String ename) {
26         this.ename = ename;
27     }
28     
29     public String getJob() {
30         return job;
31     }
32     
33     public void setJob(String job) {
34         this.job = job;
35     }
36     
37     public int getMgr() {
38         return mgr;
39     }
40     
41     public void setMgr(int mgr) {
42         this.mgr = mgr;
43     }
44     
45     public String getHiredate() {
46         return hiredate;
47     }
48     
49     public void setHiredate(String hiredate) {
50         this.hiredate = hiredate;
51     }
52     
53     public double getSal() {
54         return sal;
55     }
56     
57     public void setSal(double sal) {
58         this.sal = sal;
59     }
60 
61     public double getComm() {
62         return comm;
63     }
64 
65     public void setComm(double comm) {
66         this.comm = comm;
67     }
68 
69     public int getDeptno() {
70         return deptno;
71     }
72 
73     public void setDeptno(int deptno) {
74         this.deptno = deptno;
75     }
76 }

3)service

 1 package com.test.service;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.util.ArrayList;
 8 
 9 import com.test.bean.EmpBean;
10 import com.test.dao.BaseDao;
11 
12 public class EmpService {
13     int idx = 1;
14         
15     public ArrayList<EmpBean> getEmpList(EmpBean eb){
16         
17         Connection conn = null;
18         PreparedStatement pstmt = null;
19         ResultSet rs = null;
20         
21         ArrayList<EmpBean> empList = new ArrayList<EmpBean>();
22 
23         BaseDao commonDao = new BaseDao();
24         conn = commonDao.dbConnection();
25 
26         // 3. 执行SQL语句
27         StringBuffer sqlBf = new StringBuffer();
28         sqlBf.setLength(0);
29         
30         sqlBf.append("SELECT  EMPNO                                             \n");
31         sqlBf.append("      , ENAME                                             \n");
32         sqlBf.append("      , JOB                                               \n");
33         sqlBf.append("      , MGR                                               \n");
34         sqlBf.append("      , TO_CHAR(HIREDATE, 'YYYYMMDD')  HIREDATE           \n");
35         sqlBf.append("      , SAL                                               \n");
36         sqlBf.append("      , COMM                                              \n");
37         sqlBf.append("      , DEPTNO                                            \n");
38         sqlBf.append("FROM    EMP                                               \n");
39         sqlBf.append("WHERE   ENAME LIKE UPPER(?) || '%'                        \n");
40 
41         
42         System.out.println(sqlBf.toString());
43         
44         try {
45             pstmt = conn.prepareStatement(sqlBf.toString());
46             idx = 1;
47             pstmt.setString(idx++, eb.getEname());
48             
49             // 4. 获取结果集
50             rs = pstmt.executeQuery();
51             while (rs.next()) {
52                 EmpBean emp = new EmpBean();
53                 
54                 emp.setEmpNo(rs.getInt("empno"));
55                 emp.setEname(rs.getString("ename"));
56                 emp.setJob(rs.getString("job"));
57                 emp.setMgr(rs.getInt("mgr"));
58                 emp.setHiredate(rs.getString("hiredate"));
59                 emp.setSal(rs.getDouble("sal"));
60                 emp.setComm(rs.getDouble("comm"));
61                 emp.setDeptno(rs.getInt("deptno"));
62                 
63                 empList.add(emp);
64             }            
65         } catch (SQLException e) {
66             e.printStackTrace();
67         }
68         
69         commonDao.dbDisconnection();
70         
71         return empList;
72     }
73 }

 

第六步:页面测试

posted @ 2015-09-14 08:51  bada130  阅读(1028)  评论(0编辑  收藏  举报