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

📄 statementutils.java

📁 一个简单的java web项目 基于servlet构建
💻 JAVA
字号:
package cn.dongsw.fw;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2005
 * 
 * @author greenli
 * @created
 * @version $Id: StatementUtils.java,v 1.1 2008/08/12 08:32:45 slc Exp $
 */

public class StatementUtils {
	public static int TYPE_UNKNOWN = Integer.MIN_VALUE;

	private static final Log log = LogFactory.getLog(StatementUtils.class);

	/**
	 * @param ps
	 * @param types
	 * @param params
	 * @throws SQLException
	 */
	public static void setParameterValue(PreparedStatement ps,
			final int[] types, final Object[] params) throws SQLException {

		for (int i = 0; i < params.length; i++) {
			setParameterValue(ps, i + 1, types[i], params[i]);
		}

	}

	/**
	 * @param ps
	 * @param params
	 * @throws SQLException
	 */
	public static void setParameterValue(PreparedStatement ps,
			final Object[] params) throws SQLException {

		for (int i = 0; i < params.length; i++) {
			setParameterValue(ps, i + 1, TYPE_UNKNOWN, params[i]);
		}

	}

	/**
	 * @param ps
	 * @param param
	 * @throws SQLException
	 */
	public static void setParameterValue(PreparedStatement ps,
			final Object param) throws SQLException {
		ps.setObject(1, param);

	}

	/**
	 * @param ps
	 * @param type
	 * @param param
	 * @throws SQLException
	 */
	public static void setParameterValue(PreparedStatement ps, int type,
			final Object param) throws SQLException {
		ps.setObject(1, param, type);

	}

	/**
	 * @param ps
	 * @param paramIndex
	 * @param sqlType
	 * @param inValue
	 * @throws SQLException
	 */
	public static void setParameterValue(PreparedStatement ps, int paramIndex,
			int sqlType, Object inValue) throws SQLException {
//		log.debug("Setting SQL statement parameter value; columnIndex="
//				+ paramIndex + ", parameter value='" + inValue
//				+ "', valueClass="
//				+ (inValue != null ? inValue.getClass().getName() : "null")
//				+ ", sqlType=" + sqlType);

		if (inValue == null) {
			if (sqlType == TYPE_UNKNOWN) {
				ps.setObject(paramIndex, inValue);
			} else {
				ps.setNull(paramIndex, sqlType);
			}
		}

		else { // inValue != null
			if (sqlType == TYPE_UNKNOWN) {
				ps.setObject(paramIndex, inValue);
			} else if (sqlType == Types.VARCHAR) {
				ps.setString(paramIndex, inValue.toString());
			} else if (sqlType == Types.DATE) {
				if ((inValue instanceof java.util.Date)
						&& !(inValue instanceof java.sql.Date)) {
					inValue = new java.sql.Date(((java.util.Date) inValue)
							.getTime());
				}
				ps.setObject(paramIndex, inValue, Types.DATE);
			} else if (sqlType == Types.TIME) {
				if ((inValue instanceof java.util.Date)
						&& !(inValue instanceof java.sql.Time)) {
					inValue = new java.sql.Time(((java.util.Date) inValue)
							.getTime());
				}
				ps.setObject(paramIndex, inValue, Types.TIME);
			} else if (sqlType == Types.TIMESTAMP) {
				if ((inValue instanceof java.util.Date)
						&& !(inValue instanceof java.sql.Timestamp)) {
					inValue = new java.sql.Timestamp(((java.util.Date) inValue)
							.getTime());
				}
				ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
			} else {
				ps.setObject(paramIndex, inValue, sqlType);
			}
		}
	}
}

⌨️ 快捷键说明

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