一些工具類

baseAction:

package com.util;

import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
/**
 *
 * 
 *@author wushengxin
 * 
 * Action的工具類
 */
public abstract class BaseAction extends ActionSupport implements
        ServletRequestAware, ServletResponseAware, ServletContextAware {
    protected HttpServletRequest request;
    protected HttpServletResponse response;

    protected HttpSession session;

    protected PrintWriter out;

    protected ServletContext servletContext;

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        if (request != null) {
            this.session = request.getSession();
        }
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
        if (this.response != null) {
            try {
                this.out = this.response.getWriter();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public abstract String execute() throws Exception;
}

日期工具:

package com.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import freemarker.template.SimpleDate;
/**
 *日期工具類
 * 
 *獲取系統時間
 * 格式(yyyyMMdd_HHmmss)
 * @author wushengxin
 */
public class DateUtil {

    public static String getTimeStamp(){
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");

        return dateFormat.format(date);
    }

    public static String getSysDateTime() {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return dateFormat.format(date);
    }

    public static long diffTimeForDay(String compareTime, String sysTime) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long days = 0;
        try {
            Date d1 = dateFormat.parse(compareTime);

            Date d2 = dateFormat.parse(sysTime);

            long diff = d1.getTime() - d2.getTime();

            days = diff / (1000 * 60 * 60 * 24);

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

    public static void main(String[] args) {
        System.out.println(DateUtil.getTimeStamp());
    }
}

``


全局變量工具類:

package com.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GlobalUtil {

public static final double PI = 3.1415;

public static String getSysDate() {
    Date date = new Date();

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    return dateFormat.format(date);
}

}




數據庫jdbc工具類:
package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;


import oracle.jdbc.OracleDriver;
/**
 *連接數據庫的工具類
 * 
 *
 * 
 * @author wushengxin
 */
public class JdbcUtil {

    private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    private static String username = "wsx";
    private static String password = "wsx";

    static {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * 
     *獲取數據庫連接
     * 
     * 
     */
    public static Connection getConn() {
        Connection conn = null;
        try {

            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    /**
     *
     * 更新操作
     *@return 布爾類型
     * @param sql
     * 
     */
    public static boolean executeUpdate(String sql) throws Exception {
        Connection conn = null;
        Statement stmt = null;
        boolean flag = false;
        try {
            conn = JdbcUtil.getConn();
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            JdbcUtil.closeResource(null, stmt, conn);
        }
        return flag;
    }
    /**
     *
     * 返回單條記錄時的操作
     *@return Map<String, Object>
     * @param sql
     * 
     */
    public static Map<String, Object> executeQuerySingle(String sql) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Map<String, Object> rowMap = null;
        ResultSetMetaData metaData = null;
        try {
            conn = JdbcUtil.getConn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            if (rs.next()) {
                rowMap = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i).toLowerCase();
                    Object columnValue = rs.getObject(columnName);
                    rowMap.put(columnName, columnValue);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeResource(rs, stmt, conn);
        }
        return rowMap;
    }
    /**
     *
     * 關閉數據庫連接
     *
     * @param rs stmt conn
     * 
     */
    public static void closeResource(ResultSet rs, Statement stmt,
            Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }

    }

}


json工具類:

package com.util;

import org.codehaus.jackson.map.ObjectMapper;
/**
*
* 把數據格式轉成json的工具類
*
* @author wushengxin
*
*/
public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();

public static String objectToJson(Object obj) {
    String jsonStr = "";
    if (obj != null) {
        try {
            jsonStr = objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return jsonStr;
}

}


獲取系統參數的工具類:

package com.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* 獲取系統參數的工具類
*
* @author wushengxin
*
*/
public class ParamUtil {

public static String getParamValue(int userid, String param_key) {
    String param_value = "";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    StringBuffer querySQL = new StringBuffer();
    querySQL.setLength(0);
    querySQL.append("Select param_value");
    querySQL.append(" From T_Sys_User_Param \n");
    querySQL.append(" where userid = '" + userid + "' ");
    querySQL.append(" and param_key = '" + param_key + "'");
    try {
        conn = JdbcUtil.getConn();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(querySQL.toString());
        if (rs.next()) {
            param_value = rs.getString("param_value");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        JdbcUtil.closeResource(rs, stmt, conn);
    }
    return param_value;
}

}

其它工具類:

package com.util;

import java.math.BigDecimal;
/**
*
* 把int類型轉爲BigDecimal的工具類
*
* @author wushengxin
*
*/
public class SysUtil {
public static int BigDecimalToInt(Object obj) {
BigDecimal bd = (BigDecimal)obj;
int result = bd.intValue();
return result;
}

}

“`

(僅供參考)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章