点击这里给我发消息 点击这里给我发消息

用CashFilter实现页面级缓存实践

添加时间:2013-12-7
    相关阅读: 页面 数据库

1.mysql在本地localhost的test数据库 建person表,暂以字段id,name,password ,表中可输入一行值.

2.建类 DAOUtil

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

public class DAOUtil {
    public DAOUtil() {
    }

   public static Connection getConn() {
        Connection conn = null;
        String db_url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gb2312";
        String db_user = "root";
        String db_password= "admin";
        String db_driver = "com.mysql.jdbc.Driver";

        try {
            Class.forName(db_driver);
            conn = DriverManager.getConnection(db_url, db_user, db_password);
             } catch (Exception ex) {
             ex.printStackTrace();
        }
        return conn;
    }

  /**
  * dbclose
  * 关闭Connection,Statement
  * @param conn Connection
  * @param stmt Statement
  */
 public static void dbclose(Connection conn, Statement stmt) {
       try {
           if (stmt != null)
               stmt.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
       try {
           if (conn != null)
               conn.close();
       } catch (Exception e) {
          e.printStackTrace();
       }
   }

}

3.建类 PersonDAO

import java.util.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;


public class PersonDAO {
    static PersonDAO pdao;
    public static PersonDAO getInstance() {
        if (pdao == null) {
            synchronized (PersonDAO.class) {
                pdao = new PersonDAO();
            }
        }
        return pdao;
    }

    public PersonDAO() {
        super();    }

    public static void main(String[] args) {
          System.out.println("person==="+PersonDAO.getInstance().getPersonInfo());
    }
  
       private static List getPersonInfo() {
          String name  = "";
          String password = "";
          Connection conn = null;
          List list = new java.util.ArrayList();
          Statement stmt = null;
          String sql =
                  "select name,password from person";
          try {
              conn = DAOUtil.getConn();
              stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery(sql);
              while (rs.next()) {
                  name = (String)rs.getString("name");
                  password = (String)rs.getString("password");
                  list.add(name+","+password);
                 }
          } catch (Exception ex) {
              ex.printStackTrace();
               } finally {
              DAOUtil.dbclose(conn, stmt);
          }
          return list;
      } 
}

 

[1] [2] 下一页

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册