码迷,mamicode.com
首页 > 数据库 > 详细

(待解决):servlet+javaBean+jsp 读取数据库列表 页面无结果

时间:2015-10-06 15:15:57      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

文件如下:

技术分享

DepartBean.java:

技术分享
package beans;

public class DepartBean {
  private int did;
  private String dname;
  private String dremark;


public int getDid() {
    return did;
}
public void setDid(int did) {
    this.did = did;
}
public String getDname() {
    return dname;
}
public void setDname(String dname) {
    this.dname = dname;
}
public String getDremark() {
    return dremark;
}
public void setDremark(String dremark) {
    this.dremark = dremark;
}
  
  
}
View Code

 DepartControl.java:

技术分享
package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

import beans.DepartBean;
import utils.DBConn;

public class DepartControl {
      public DepartControl(){}
      @Test
      public List<DepartBean> DepartControl1(){
            ArrayList<DepartBean> DepartList=new ArrayList<DepartBean>();
            //List<Map<String, String>> list = new ArrayList<Map<String, String>>  ();
            
            ResultSet rs=null;
            DBConn db=new DBConn();
            rs=db.doSelect("select * from department order by did asc");
           
                
            try {
                while(rs.next()){
                    DepartBean dptb=new DepartBean();
                    dptb.setDid(Integer.parseInt(rs.getString("did")));
                    dptb.setDname(rs.getString("dname"));
                    dptb.setDremark(rs.getString("dremark"));
                    DepartList.add(dptb);
                }
            
            
            
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    db.close(rs);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
            }
      return DepartList;
        }
     
}
View Code

DepartmentServlet.java

技术分享
package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DepartBean;
import model.DepartControl;

/**
 * Servlet implementation class DepartmentServlet
 */
@WebServlet("/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DepartmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //doGet(request, response);
         response.setCharacterEncoding("utf-8");  
           
          
            DepartControl dptc=new DepartControl();     
             List list=dptc.DepartControl1();     
             System.out.println(list);
             String str="hello";
         
            
             HttpSession session =request.getSession();
             session.setAttribute("str", str);
             
             response.sendRedirect("Department/NewFile.jsp");
             
            // session.setAttribute("list", list);
          //  request.getRequestDispatcher("Department/NewFile.jsp").forward(request, response);
        
        
    }

}
View Code

DBConn.java:

技术分享
package utils;

import java.io.*;   
import java.sql.*;



public class DBConn {   
    public static String driver;//定义驱动   
    public static String url;//定义URL   
    public static String user;//定义用户名   
    public static String password;//定义密码   
    public static Connection conn;//定义连接   
    public static Statement stmt;//定义STMT   
    public ResultSet rs;//定义结果集   
    //设置CONN   
    static{   
        try { 
            driver="oracle.jdbc.driver.OracleDriver";
            url="jdbc:oracle:thin:@localhost:1521:orcl";
            //URL=协议名+IP地址(域名)+端口+数据库名称;
            user="c##zj";
            password="1234";
            Class.forName(driver);   
            conn = DriverManager.getConnection(url,user,password);
            System.out.println("-------连接成功------");
        } catch(ClassNotFoundException classnotfoundexception) {   
              classnotfoundexception.printStackTrace();   
            System.err.println("db: " + classnotfoundexception.getMessage());   
        } catch(SQLException sqlexception) {   
            System.err.println("db.getconn(): " + sqlexception.getMessage());   
        }   
    }   
    //构造函数,默认加裁配置文件为jdbc.driver   
    public DBConn(){   
        this.conn=this.getConn();
    }   
    //返回Conn   
    public Connection getConn(){   
        return this.conn;   
    }   
    //执行插入   
       public void doInsert(String sql) {   
        try {   
            stmt = conn.createStatement();   
            int i = stmt.executeUpdate(sql);   
        } catch(SQLException sqlexception) {   
            System.err.println("db.executeInset:" + sqlexception.getMessage());   
        }finally{   
               
        }   
    }   
    //执行删除   
    public void doDelete(String sql) {   
        try {   
            stmt = conn.createStatement();   
            int i = stmt.executeUpdate(sql);   
        } catch(SQLException sqlexception) {   
            System.err.println("db.executeDelete:" + sqlexception.getMessage());   
        }   
    }   
    //执行更新   
    public void doUpdate(String sql) {   
        try {   
            stmt = conn.createStatement();   
            int i = stmt.executeUpdate(sql);   
        } catch(SQLException sqlexception) {   
            System.err.println("db.executeUpdate:" + sqlexception.getMessage());   
        }   
    }   
    //查询结果集   
    public ResultSet doSelect(String sql) {   
        try {
            conn=DriverManager.getConnection(url,user,password);
            stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);     
            rs = stmt.executeQuery(sql); 
            System.out.println("取得结果集");
        } catch(SQLException sqlexception) {   
            System.err.println("db.executeQuery: " + sqlexception.getMessage());   
        }   
        return rs;   
    }   
    /**  
     *关闭数据库结果集,数据库操作对象,数据库链接  
       @Function: Close all the statement and conn int this instance and close the parameter ResultSet  
       @Param: ResultSet  
       @Exception: SQLException,Exception  
      **/  
     public void close(ResultSet rs) throws SQLException, Exception {   
  
       if (rs != null) {   
         rs.close();   
         rs = null;   
       }   
  
       if (stmt != null) {   
         stmt.close();   
         stmt = null;   
       }   
  
       if (conn != null) {   
         conn.close();   
         conn = null;   
       }   
     }   
  
     /**  
      *关闭数据库操作对象,数据库连接对象  
      * Close all the statement and conn int this instance  
      * @throws SQLException  
      * @throws Exception  
      */  
     public void close() throws SQLException, Exception {   
       if (stmt != null) {   
         stmt.close();   
         stmt = null;   
       }   
  
       if (conn != null) {   
         conn.close();   
         conn = null;   
       }   
     }   


     
}  
  DepartList.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<table border="1">
        <tr>
             <td>组织编号</td>><td> 组织名称</td><td>描述</td> <td></td>
        </tr>   
        <c:forEach var="dpart" items="${list} ">
             <tr>
                <td>${depart.did }</td>
                  <td>${dpart.dname}</td>
                <td>${dpart.dremark}</td>
                <td></td>
              </tr>      
        </c:forEach>
               
</table>





</body>
</html>
View Code

 LookDepartBean.jsp:

技术分享
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<table border="1">
        <tr>
             <td>组织编号</td>><td> 组织名称</td><td>描述</td> <td></td>
        </tr>   
        <c:forEach var="dpart" items="${list} ">
             <tr>
                <td>${depart.did }</td>
                  <td>${dpart.dname}</td>
                <td>${dpart.dremark}</td>
                <td></td>
              </tr>      
        </c:forEach>
               
</table>





</body>
</html>
View Code

报错: 

       页面无结果

分析:

警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}

                 Setting property ‘source‘ to ‘org.eclipse.jst.jee.server:PermissionSystem‘ did not find a matching property.

 

 

(待解决):servlet+javaBean+jsp 读取数据库列表 页面无结果

标签:

原文地址:http://www.cnblogs.com/gugibv/p/4857115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!