JAVAWeb项目 微型商城项目-------(三)导包及工具类的准备

1.导包

数据库驱动包

c3p0数据库池包 --CRDU

Servlet核心包

日志记录包

2.工具类的准备

JDBCUtil 数据库连接类

public class JDBCUtil {

  static ComboPooledDataSource dataSource = null;
  static {
    dataSource = new ComboPooledDataSource();
  }

  /**
   * 获取连接对象
   * @return
   * @throws SQLException 
   */
  public static Connection getConn() throws SQLException {
    System.out.println("数据库连接成功");
    Connection conn = dataSource.getConnection();
    System.out.println("取得conn对象");
    return conn;
  }

  /**
   * 释放资源
   * @param conn
   * @param st
   * @param rs
   */
  public static void release(Connection conn, Statement st, ResultSet rs) {
    closeRs(rs);
    closeSt(st);
    closeConn(conn);
  }

  public static void release(Connection conn, Statement st) {
    closeSt(st);
    closeConn(conn);
  }


  private static void closeRs(ResultSet rs) {
    try {
      if (rs != null) {
        rs.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      rs = null;
    }
  }

  private static void closeSt(Statement st) {
    try {
      if (st != null) {
        st.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      st = null;
    }
  }

  private static void closeConn(Connection conn) {
    try {
      if (conn != null) {
        conn.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      conn = null;
    }
  }
}

网址生成类

/**
 * 
 * @ClassName:  BasePath   
 * @Description:TODO(返回输入的网址)   
 * @author: kiko 
 * @date:   2018年6月25日 上午9:36:55   
 *     
 * @Copyright: 2018
 */
public class BasePath {
  /**
   * 
   * @Title: getBasePath   
   * @Description: TODO(这里用一句话描述这个方法的作用)   
   * @param: @param request
   * @param: @return      
   * @return: String     返回类容 http://xxx/项目名称/
   * @throws
   */
  public static String getBasePath(HttpServletRequest request) {
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
        + request.getServerPort() + path;
    return basePath;
  }
}

利用过滤器解决中文乱码

/**
 * 
 * @ClassName: EncodingFilter
 * @Description:TODO(处理中文错误过滤器)
 * @author: kiko
 * @date: 2018年6月23日 下午10:45:15
 * 
 * @Copyright: 2018
 */
/**  
 * 使用注解标注过滤器  (web.xml就无需配置)
 * @WebFilter将一个实现了javax.servlet.Filte接口的类定义为过滤器  
 * 属性filterName声明过滤器的名称,可选  
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)  
 */
@WebFilter(filterName = "EncodingFilter", urlPatterns = "/*") // 过滤设置
public class EncodingFilter implements Filter {


  @Override
  public void init(FilterConfig FilterConfig) throws ServletException {
    System.out.println("过滤器开始工作");

  }

  @Override
  public void doFilter(ServletRequest Request, ServletResponse Response, FilterChain Chain)
      throws IOException, ServletException {
    System.out.println("过滤器正在过滤");
    Request.setCharacterEncoding("utf-8");
    Chain.doFilter(Request, Response);
    Response.setCharacterEncoding("utf-8");

  }


  @Override
  public void destroy() {
    System.out.println("过滤器结束工作");

  }

}
验证工具类

/**
 * 
 * @ClassName:  VakidateUtil   
 * @Description:TODO(验证工具类:数据是否为空,数据正则判断,两个数据是否相同)   
 * @author: kiko 
 * @date:   2018年6月24日 下午1:19:45   
 *     
 * @Copyright: 2018
 */
public class VakidateUtil {
  /**
   * 
   * @Title: VakidateEmpty   
   * @Description: TODO(验证输入数据是否为空)   
   * @param: @param data 字符串
   * @param: @return      
   * @return: boolean    如果不是null 返回true  
   * @throws
   */
  public static boolean VakidateNoEmpty(String data) {
    if (data == null || "".equals(data)) {
      return false;
    }
    return true;

  }

  /**
   * 
   * @Title: VakidateRegex   
   * @Description: TODO(验证正则)   
   * @param: @param data    字符串
   * @param: @param regex 正则表达式
   * @param: @return      
   * @return: boolean      符合正则返回true
   * @throws
   */
  public static boolean VakidateRegex(String data, String regex) {

    if (VakidateNoEmpty(data)) {
      return data.matches(regex);
    }
    return false;
  }

  /**
   * 
   * @Title: Vakidatesame   
   * @Description: TODO(验证数据是否相同,不区分大小写)   
   * @param: @param data1
   * @param: @param data2
   * @param: @return      
   * @return: boolean  相同返回true 
   * @throws
   */
  public static boolean Vakidatesame(String data1, String data2) {
    if (VakidateNoEmpty(data1) && VakidateNoEmpty(data2)) {
      return data1.equalsIgnoreCase(data2);
    }
    return false;

  }
}
 


MD5生成工具类
/**
 * 
 * @ClassName:  MD5Code   
 * @Description:TODO(MD5生成工具类)   
 * @author: kiko 
 * @date:   2018年6月23日 下午4:44:42   
 *     
 * @Copyright: 2018
 * apache commones Codec包中有常用的一些加密算法实现
 */
public class MD5Code {

  /** 16进制的字符数组 */
  private final static String[] hexDigits =
      {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

  /**
   * 
   * 
   * @param source
   *            需要加密的字符串
   * @return  result MD5加密后的字符串
   */
  public static String MD5Encode(String source) {
    String result = null;
    try {
      result = source;
      // 获得MD5对象
      MessageDigest messageDigest = MessageDigest.getInstance("MD5");
      // 使用指定的字节数组更新摘要信息
      messageDigest.update(result.getBytes("UTF-8"));
      // messageDigest.digest()获得16位长度
      result = byteArrayToHexString(messageDigest.digest());

    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

  /**
   * 转换字节数组为16进制字符串
   * 
   * @param bytes  字节数组
   *            
   * @return  16进制的字符串
   */
  private static String byteArrayToHexString(byte[] bytes) {
    StringBuilder stringBuilder = new StringBuilder();
    for (byte tem : bytes) {
      stringBuilder.append(byteToHexString(tem));
    }
    return stringBuilder.toString();
  }

  /**
   * 将一个字节转换为16进制的字符串
   * 
   * @param b  要转换的byte 
   *           
   * @return 16进制对应的字符
   */
  private static String byteToHexString(byte b) {
    int n = b;
    if (n < 0) {
      n = 256 + n;
    }
    int d1 = n / 16;
    int d2 = n % 16;
    return hexDigits[d1] + hexDigits[d2];
  }



猜你喜欢

转载自blog.csdn.net/kikock/article/details/80803419
今日推荐