All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.j2mvc.framework.dao.callback.Creator Maven / Gradle / Ivy

Go to download

强烈建议使用J2mvc 2.1以后的版本。 version 2.1.01 1.更换JSON依赖包. version 2.1.02 1.移除com.j2mvc.StringUtils.getUtf8()方法调用. 更改为getCharset() version 2.1.03 1.更新JNDI连接设置 version 2.1.04 1.修改works.xml配置url-pkg-prefixes改为pkg

There is a newer version: 2.1.12
Show newest version
package com.j2mvc.framework.dao.callback;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.apache.log4j.Logger;
import com.j2mvc.framework.Session;

/**
 * 创建预编译语句
 * @author 杨朔
 * 2014年3月28日 创建
 */
public class Creator extends PreparedStatementCreator {
	Logger log = Logger.getLogger(getClass().getName());

	private String sql;
	private Object[] values;
	
	public Creator(String sql, Object[] values) {
		super();
		this.sql = sql;
		this.values = values;
	}
	@Override
	public PreparedStatement execute(Connection con)throws SQLException {
		PreparedStatement pstmt = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
		if(Session.sqlLog)
			log.info("编译前语句 >> "+pstmt.toString().split(":")[1].replaceAll("\\*\\* NOT SPECIFIED \\*\\*", "?"));
		if(values!=null)
		for(int i=0;i clazz = value.getClass();
			int index = i+1;
			// 得到数据类型
			if(String.class.isAssignableFrom(clazz) ||
					Date.class.isAssignableFrom(clazz)){
				pstmt.setString(index, (String) value);
			}else if(Integer.class.isAssignableFrom(clazz)){
				pstmt.setInt(index, !value.equals("")?(Integer)value:0);
			}else if(Long.class.isAssignableFrom(clazz))
				pstmt.setLong(index, !value.equals("")?(Long)value:0);
			else if(Float.class.isAssignableFrom(clazz))
				pstmt.setFloat(index, (Float) value);
			else if(Double.class.isAssignableFrom(clazz))
				pstmt.setDouble(index, (Double) value);
			else if(Boolean.class.isAssignableFrom(clazz))
				pstmt.setBoolean(index, (Boolean) value);
			else{
				pstmt.setBytes(index, StreamUtil.objectToBytes(value));		
			}
		}
		if(Session.sqlLog)
			log.info("编译后 >> "+pstmt);
		return pstmt;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy