⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.java

📁 java支持的短信平台
💻 JAVA
字号:
package com.khan.util;

import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.sql.*;
import com.khan.db.*;
import java.util.Vector;

public class common {

  public static boolean debug = true;

  /**构造器*/
  public common() {}

  /**根据传入的字段名和条件生成模糊条件查询语句
   * @param UrlArgs 包含字段名和条件的hash表
   * @return String 查询条件语句 */
  public static String AddQuery(HashMap UrlArgs) {
    String str = "";
    Iterator e = UrlArgs.keySet().iterator();
    while (e.hasNext()) {
      Object ob = e.next();
      if (!((String) UrlArgs.get(ob)).equals(new String())) {
        str += " and " + (String) ob + " like '" + (String) UrlArgs.get(ob) + "%'";
      }
    }
    return str;
  }


  /**
   * 取得所有得参数列表
   * @param req HttpServletRequest
   * @return HashMap 返回包含参数列表的HashMap
   */
  public static HashMap getParas(HttpServletRequest req) {
    HashMap hm = new HashMap();
    Enumeration ea = req.getParameterNames();
    while (ea.hasMoreElements()) {
      String s = (String) ea.nextElement();
      hm.put(s, req.getParameter(s));
    }
    return hm;
  }

  /**
   *取得http head
   * @param req HttpServletRequest
   * @return HashMap
   */
  public static HashMap gethead(HttpServletRequest req) {
    HashMap hm = new HashMap();
    Enumeration ea = req.getHeaderNames();
    while (ea.hasMoreElements()) {
      String s = (String) ea.nextElement();
      hm.put(s, req.getHeader(s));
    }
    return hm;
  }

  /**检测参数列表中是否存在某个参数
   * @param req HttpServletRequest
   * @param para String 参数名
   * @return boolean 是否存在这个参数
   */
  public static boolean existPara(HttpServletRequest req, String para) {
    HashMap hm = getParas(req);
    return hm.containsKey(para);
  }

  /**
   * 取得手机号码及浏览器信息
   * @param req HttpServletRequest
   * @return String 手机类型
   */
  private static String getMobType2(HttpServletRequest req) {
    String mobtype = (String) req.getHeader("User-Agent");

    if (mobtype.indexOf("UP.Browser") != -1) {
      mobtype = mobtype.substring(0, mobtype.indexOf("UP.Browser"));
      mobtype = mobtype.trim();
    }
    return mobtype;
  }


  public static boolean isNumber(String number) {
    try {
      Integer.parseInt(number);
    } catch (NumberFormatException e) {
      return false;
    }
    return true;
  }

  private static String getMobType1(HttpServletRequest req) {
    String mobtype = (String) req.getHeader("User-Agent");
    return mobtype;
  }

  private static String getMobType(HttpServletRequest req) {
    String mobtype = (String) req.getHeader("User-Agent");
    mobtype = mobtype.substring(0, mobtype.indexOf("/"));
    return mobtype;
  }

  /**
   * 取得手机号
   * @param req HttpServletRequest
   * @return String 手机号码
   */
  public static String getMobID(HttpServletRequest req) {
    /*
      String mobtype = (String) req.getHeader("User-Agent");
      String mobid = (String) req.getHeader("X-Up-Calling-Line-ID");

      if (mobid == null)
        mobid = (String) req.getHeader("X-Nokia-msisdn");

      if (mobid == null)
        mobid = "8612345678912";
      if (!mobid.startsWith("86"))
        mobid = "86" + mobid;
     */

    String mobid = (String) req.getParameter("MISC_MSISDN");
    if (mobid == null) {
      mobid = "8612345678912";
    }
    if (!mobid.startsWith("86")) {
      mobid = "86" + mobid;

    }
    return mobid;
  }

  /**
   * 取得登录wap用户的手机号
   * @param req HttpServletRequest
   * @return String
   */
  public static String getMid(HttpServletRequest req) {
    return (String) req.getParameter("MISC_MID");
  }

  public static String getUA(HttpServletRequest req) {
    String mobtype = (String) req.getHeader("User-Agent");
    if (mobtype == null) {
      return "";
    }
    if (mobtype.indexOf('/') != -1) { //普通处理
      mobtype = mobtype.substring(0, mobtype.indexOf('/'));
      return mobtype;
    }
    if (mobtype.indexOf('*') != -1) { //Samsung某些机器特别处理
      mobtype = mobtype.substring(0, mobtype.indexOf('/'));
      return mobtype;
    }
    return mobtype;

  }


  /**返回分页数
   * @param count 纪录总数
   * @param No 每页的纪录数
   * @return int 页数
   * */
  static public int getPageCount(int count, int No) {
    // System.out.println( count + " "+ No);
    return (count % No) > 0 ? count / No + 1 : count / No;
  }


  static public int excuteSql(String sqlstr, String JndiName) {
    Connection conn = DBMng.getConnection(JndiName);
    Statement stmt = null;
    int result = 0;
    try {
      if (conn.isClosed()) {
        conn = DBMng.getConnection(JndiName);
      }
      stmt = conn.createStatement();
      result = stmt.executeUpdate(sqlstr);
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return result;
  }


  /**
   * 执行sql语句
   * @param sqlstr String  sql语句
   * @return int 影响的行数
   */
  static public int excuteSql(String sqlstr) {
    Connection conn = DBMng.getConnection();
    Statement stmt = null;
    int result = 0;
    try {
      if (conn.isClosed()) {
        conn = DBMng.getConnection();
      }
      stmt = conn.createStatement();
      result = stmt.executeUpdate(sqlstr);
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return result;
  }


  /**
   * 执行带结果集的sql语句
   * @param sqlstr String sql语句
   * @return String[][] 结果集
   */
  static public String[][] getRecord(String sqlstr, String JndiName) {
    int fieldCount = 0;
    Vector dataSource = new Vector();
    Connection conn = DBMng.getConnection(JndiName);
    Statement stmt = null;
    ResultSet rs = null;
    try {
      if (conn.isClosed()) {
        conn = DBMng.getConnection(JndiName);
      }
      stmt = conn.createStatement();
      rs = stmt.executeQuery(sqlstr);
      while (rs.next()) {
        Vector record = new Vector();
        fieldCount = rs.getMetaData().getColumnCount();
        //System.out.println(fieldCount);
        for (int j = 1; j <= fieldCount; j++) {
          //System.out.println(rs.getString(j));
          record.add(rs.getString(j));
        }
        dataSource.add(record);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    String[][] strs = new String[dataSource.size()][fieldCount];
    for (int i = 0; i < dataSource.size(); i++) {
      Vector vec = (Vector) (dataSource.elementAt(i));
      for (int j = 0; j < fieldCount; j++) {
        strs[i][j] = (String) (vec.elementAt(j));
      }
    }
    return strs;
  }


  /**
   * 执行带结果集的sql语句
   * @param sqlstr String sql语句
   * @return String[][] 结果集
   */
  static public String[][] getRecord(String sqlstr) {
    int fieldCount = 0;
    Vector dataSource = new Vector();
    Connection conn = DBMng.getConnection();
    Statement stmt = null;
    ResultSet rs = null;
    try {
      if (conn.isClosed()) {
        conn = DBMng.getConnection();
      }
      stmt = conn.createStatement();
      rs = stmt.executeQuery(sqlstr);
      while (rs.next()) {
        Vector record = new Vector();
        fieldCount = rs.getMetaData().getColumnCount();
        //System.out.println(fieldCount);
        for (int j = 1; j <= fieldCount; j++) {
          //System.out.println(rs.getString(j));
          record.add(rs.getString(j));
        }
        dataSource.add(record);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    String[][] strs = new String[dataSource.size()][fieldCount];
    for (int i = 0; i < dataSource.size(); i++) {
      Vector vec = (Vector) (dataSource.elementAt(i));
      for (int j = 0; j < fieldCount; j++) {
        strs[i][j] = (String) (vec.elementAt(j));
      }
    }
    return strs;
  }

  /**@param sqlstr String sql语句
   * @param top int 每页纪录数
   * @param pageIndex int 页码
   * @param pagecount Page 纪录总数的输出参数
   * @return String[][] 结果集*/
  static public String[][] getRecord(String sqlstr, int top, int pageIndex, Page pagecount) {
    int fieldCount = 0;
    Vector dataSource = new Vector();
    Connection conn = DBMng.getConnection();
    Statement stmt = null;
    ResultSet rs = null;
    try {
      if (conn.isClosed()) {
        conn = DBMng.getConnection();
      }
      stmt = conn.createStatement();
      rs = stmt.executeQuery(sqlstr);
      //int i=0;
      while (rs.next()) {
        Vector record = new Vector();
        fieldCount = rs.getMetaData().getColumnCount();
        //System.out.println(fieldCount);
        for (int j = 1; j <= fieldCount; j++) {
          //System.out.println(rs.getString(j));
          record.add(rs.getString(j));
        }
        dataSource.add(record);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    String strs[][];

    pagecount.recordCount = dataSource.size();
    int size = 0;
    if (top > dataSource.size() - top * pageIndex) {
      size = dataSource.size() - top * pageIndex;
      if (size < 0) {
        System.out.println("error  String[size][fieldCount] size:" + size + " fieldCount:" + fieldCount);
        return null;
      }
      strs = new String[size][fieldCount];
    } else {
      size = top;
      strs = new String[top][fieldCount];
    }
    for (int i = 0; i < size; i++) {
      //System.out.println( top * ( pageIndex )  + i );
      Vector vec = (Vector) (dataSource.elementAt((top * (pageIndex)) + i));
      for (int j = 0; j < fieldCount; j++) {
        strs[i][j] = (String) (vec.elementAt(j));
      }
    }
    return strs;
  }


  /**
   * 执行sql语句
   * @param sqlstr String  sql语句
   * @return int 影响的行数
   */
  static public int PoolExcuteSql(DBPoolCon dbpool, String sqlstr) {
    Statement stmt = null;
    int result = 0;
    try {
      stmt = dbpool.get().createStatement();
      result = stmt.executeUpdate(sqlstr);

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

  /**
   * 执行带结果集的sql语句
   * @param sqlstr String sql语句
   * @return String[][] 结果集
   */
  static public String[][] poolGetRecord(DBPoolCon dbpool, String sqlstr) {
    int fieldCount = 0;
    Vector dataSource = new Vector();
    Statement stmt = null;
    ResultSet rs = null;
    try {
      stmt = dbpool.get().createStatement();
      rs = stmt.executeQuery(sqlstr);
      while (rs.next()) {
        Vector record = new Vector();
        fieldCount = rs.getMetaData().getColumnCount();
        //System.out.println(fieldCount);
        for (int j = 1; j <= fieldCount; j++) {
          //System.out.println(rs.getString(j));
          record.add(rs.getString(j));
        }
        dataSource.add(record);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    String[][] strs = new String[dataSource.size()][fieldCount];
    for (int i = 0; i < dataSource.size(); i++) {
      Vector vec = (Vector) (dataSource.elementAt(i));
      for (int j = 0; j < fieldCount; j++) {
        strs[i][j] = (String) (vec.elementAt(j));
      }
    }
    return strs;
  }


  public static void Assert(String msg) {
    if (debug) {
      System.out.println(msg);
    }
  }

  public static void sleep(long millis) {
    try {
      Thread.sleep(millis);
    } catch (InterruptedException e) {
      System.err.println("sleep interruptedException!");
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -