第三十六节(Java-jdbc高级运用-存储过程调用)

GuestInfoBean.java文件:

package com.tanzhou.date;
/**
 * 数据表实体类 - tb_guestinfo
 * @author Ming
 * @version 1.0
 */
public class GuestInfoBean {

    private Integer id; // '编号',
    private String guestname; //'访客姓名',
    private String guesttel; //'连联电话',
    private String indate; //'来访时间',
    private String outdate; //'离开时间',
    private String remark; // '备注信息',
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getGuestname() {
        return guestname;
    }
    public void setGuestname(String guestname) {
        this.guestname = guestname;
    }
    public String getGuesttel() {
        return guesttel;
    }
    public void setGuesttel(String guesttel) {
        this.guesttel = guesttel;
    }
    public String getIndate() {
        return indate;
    }
    public void setIndate(String indate) {
        this.indate = indate;
    }
    public String getOutdate() {
        return outdate;
    }
    public void setOutdate(String outdate) {
        this.outdate = outdate;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
}


////////////////////////////////////////////////////////////////////////
GuestInfoDao.java文件:

package com.tanzhou.date;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 操作数据的类 dao
 * @author ming
 * @version 1.0

 *
 */
public class GuestInfoDao {

    /**
     * 查询所有的数据
     * @return 查询出来的数据
     */
    public List findAll(){ 
        List list = new ArrayList();
        Connection con = null;          // 声明 Connection 对象
        CallableStatement proc = null; // 声明CallableStatement对象
        // PreparedStatement  执行普通sql的
        ResultSet rs = null; // 声明一个结果集
        
        try {
            con = DBConnection.getConnection(); // 创建数据库连接
            proc =con.prepareCall("{call guestinfofindall()}");
            rs = proc.executeQuery(); // 执行查询的sql 
            while(rs.next()){ // 遍历结果集
                GuestInfoBean gib = new GuestInfoBean();
                gib.setId(rs.getInt("id"));
                gib.setGuestname(rs.getString("guestname"));
                gib.setGuesttel(rs.getString("guesttel"));
                gib.setIndate(rs.getString("indate"));
                gib.setOutdate(rs.getString("outdate"));
                gib.setRemark(rs.getString("remark"));
                list.add(gib);
            }
            // 关闭对象
            rs.close();
            proc.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                con.close(); 
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                con = null;
            }
        }
        
        return list;
    }
    
    /**
     * 删除所有的记录
     */
    public void deleteAll(){
        Connection con = null;
        CallableStatement proc = null;  
        
        try {
            con = DBConnection.getConnection(); //创建数据库连接
            con.setAutoCommit(false); // 关闭自动事物模式
            proc = con.prepareCall("{call guestinfodelete()}"); //调用删除的存储过程
            proc.execute(); // 执行存储过程
            con.commit(); //提交事物
            proc.close(); // 关闭 proc
        } catch (SQLException e) {
            try {
                con.rollback(); // 产生异常回滚事务
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                con = null;
            }
        }
    }
    
}


/////////////////////////////////////////////////////////////////////
DBConnection.java文件里:


package com.tanzhou.date;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 链接数据的核心类
 * @author Ming
 * @version 1.0
 * 2015-2-8
 *
 */
public class DBConnection {

    
    /**
     * 1、加载jdbc驱动
     * 2、提供jdbc链接的url
     * 3、创建数据库连接
     * 4、创建一个statement
     * 5、执行sql语句
     * 6、处理结果
     * 7、关闭jdbc链接
     */
    
    static Connection con = null; // 数据库的链接对象
    
    /**
     * 建立数据库的链接
     * @return 数据库连接
     */
    public static Connection getConnection(){
        
        try {
            Class.forName("com.mysql.jdbc.Driver"); // 加载jdbc驱动
            String url = "jdbc:mysql://localhost:3306/testinfo?useUnicode=true&characterEncoding=gbk";
            String user = "root";
            String password = "root";    
            con = DriverManager.getConnection(url, user, password); // 创建数据库连接
            con.setAutoCommit(false); // 事物
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return con;
    }
    
    
}

//////////////////////////////////////////////////////////////////////

index.jsp文件里:

<%@page import="com.tanzhou.date.GuestInfoBean"%>
<%@page import="com.tanzhou.date.GuestInfoDao"%>
<%@page import="java.util.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>JSP Page</title>
    </head>
    <body>
    <center><a href="deleteAll.jsp">清除所有历史信息</a></center>
        <table width="700" border="1" align="center" cellpadding="1" cellspacing="1">
        <tr align="center">
          <td valign="middle"><span class="STYLE1">编号</span></td>
          <td valign="middle"><span class="STYLE1">客人姓名</span></td>
          <td valign="middle"><span class="STYLE1">联系电话</span></td>
          <td valign="middle"><span class="STYLE1">来访时间</span></td>
          <td valign="middle"><span class="STYLE1">离开时间</span></td>
          <td valign="middle"><span class="STYLE1">来访目的</span></td>
        </tr>
        <%
        List list = new GuestInfoDao().findAll();
        for(int i=0;i<list.size();i++){
            GuestInfoBean gib =(GuestInfoBean)list.get(i);
        %>
         <tr>
             <td><%=gib.getId() %></td>
             <td><%=gib.getGuestname() %></td>
             <td><%=gib.getGuesttel() %></td>
             <td><%=gib.getIndate() %></td>
             <td><%=gib.getOutdate() %></td>
             <td><%=gib.getRemark() %></td>
         </tr>
        <%
        }
        %>
    </table>
        
    </body>
</html>

////////////////////////////////////////////////////////////////////
deleteAll.jsp文件里:

<%@page import="com.tanzhou.date.GuestInfoDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

   GuestInfoDao dao = new GuestInfoDao();
   dao.deleteAll();
   response.sendRedirect("index.jsp");
%>

 

posted @ 2015-02-08 10:35  哥的笑百度不到  阅读(220)  评论(0编辑  收藏  举报