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

📄 preparedstatementhelper.java

📁 Speedframework--基于类型元数据的羽量级ORM.完全基于Java类型元数据和反射机制同时不需要任何对象关系映射配置文件的轻量级ORM框架,它充分利用了类型本身的信息,使用Metadata
💻 JAVA
字号:
package org.speedframework.execute;

import java.sql.*;

import org.apache.log4j.Logger;
import org.speedframework.exception.*;
import org.speedframework.util.ConvertUtil;
import org.speedframework.util.SQLHelper;

import java.util.List;
import java.util.ArrayList;

/**
 * <p>
 * Title: SpeedFrameworkWork锟街久诧拷锟斤拷
 * </p>
 * 
 * <p>
 * Description: PreparedStatement锟斤拷锟斤拷值锟斤拷
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company: SpeedFrameworkWork team
 * </p>
 * 
 * @author 锟斤拷志锟斤拷 锟界话锟斤拷13824431576 锟斤拷伟锟斤拷 锟界话锟斤拷
 * @version 1.0
 */
public class PreparedStatementHelper implements ExecuteHelper{
	static final Logger log = Logger.getLogger(PreparedStatementHelper.class);
	private Connection con;
	public PreparedStatementHelper(Connection con) {
		this.con=con;
	}

	/* (非 Javadoc)
	 * @see org.speedframework.execute.ExecuteHelper#setExecuteUpdate(java.sql.Connection, java.sql.PreparedStatement, java.lang.String, java.lang.Object[])
	 */
	public  int setExecuteUpdate(PreparedStatement ps, String SQL, Object[] parmvalue) throws SQLException, ConnectionException, ParamException {
	
		if (SQL == null) {
			throw new ParamException(" SQL is null ");
		}
		if (con == null) {
			throw new ConnectionException(" Connection is null ");
		}
	
		ps = con.prepareStatement(SQL);
	
		for (int i = 0; i < parmvalue.length; i++) {
			ConvertUtil.importData(ps, i + 1, parmvalue[i],true);
		}
	
		return ps.executeUpdate();
	}

	/* (非 Javadoc)
	 * @see org.speedframework.execute.ExecuteHelper#setExecuteQuery(java.sql.PreparedStatement, java.lang.Object[])
	 */
	public  ResultSet setExecuteQuery(PreparedStatement ps, Object[] parmvalue) throws SQLException {
		if (parmvalue.length > 0) {
			for (int i = 0; i < parmvalue.length; i++) {
				ConvertUtil.importData(ps, i + 1, parmvalue[i],false);
			}
		}
		return ps.executeQuery();
	}

	/* (非 Javadoc)
	 * @see org.speedframework.execute.ExecuteHelper#executeInsert(java.sql.Connection, java.sql.PreparedStatement, java.lang.String, java.lang.Object[])
	 */
	public  List executeInsert(PreparedStatement ps, String SQL, Object[] parmvalue) throws ParamException, ConnectionException, SQLException {
	
			if (SQL == null) {
				throw new ParamException(" SQL is null ");
			}
			if (con == null) {
				throw new ConnectionException(" Connection is null ");
			}
	
	// ps = con.prepareStatement(SQL,PreparedStatement.RETURN_GENERATED_KEYS);
			ps = con.prepareStatement(SQL);
	
			for (int i = 0; i < parmvalue.length; i++) {
				ConvertUtil.importData(ps, i + 1, parmvalue[i],false);
			}
	// ps.setString(1,parmvalue[0].toString());
	// ps.setString(2,parmvalue[1].toString());
	// ps.setString(3,parmvalue[2].toString());
	// ps.setString(4,parmvalue[3].toString());
			ps.executeUpdate();
	
			List keys = null;
			String type = SQLHelper.getDataBaseType(con);
	
			if (!type.toLowerCase().equals("oracle")
					&& !type.toLowerCase().equals("microsoft sql server")
					&& !type.toLowerCase().equals("postgresql")
					&& type.indexOf("db2")==-1
					&& type.indexOf("hsql")==-1
					&& type.indexOf("access")==-1) {
				ResultSet rs = ps.getGeneratedKeys();
				if (rs.next()) {
					keys = new ArrayList();
					rs.beforeFirst();
					while (rs.next()) {
						keys.add(new Integer(rs.getString(1)));
					}
					// Obtain the generated key that results from the query.
					rs.close();
				}
			}else{
				log.warn(type+" autogenerated keys is not supported.");
			}
	
			return keys;
	
		}

	/* (非 Javadoc)
	 * @see org.speedframework.execute.ExecuteHelper#executeBatch(java.sql.Connection, java.lang.String, java.lang.Object[][])
	 */
	public  int[] executeBatch(String SQL, Object[][] params) throws ParamException, ConnectionException, SQLException {
			if (SQL == null) {
				throw new ParamException(" SQL is null ");
			}
			if (con == null) {
				throw new ConnectionException(" Connection is null ");
			}
			PreparedStatement ps = null;
			int[] reValue = null;
			try {
				ps = con.prepareStatement(SQL);
				for (int i = 0; i < params.length; i++) {
					for (int j = 0; j < params[i].length; j++) {
	// log.debug(params[i][j].toString());
						ConvertUtil.importData(ps, j + 1, params[i][j],false);
					}
	// ps.setInt(1,67);
					ps.addBatch();
				}
				
	// reValue = ps.executeBatch();
				reValue = ps.executeBatch();
			} catch (Exception ex) {
	// ex.printStackTrace();
				throw new ConnectionException(ex.getMessage());
			} finally {
				if (ps != null) {
					ps.close();
				}
			}
			return reValue;
		}

	/* (非 Javadoc)
	 * @see org.speedframework.execute.ExecuteHelper#setExecuteCall(java.sql.Connection, java.sql.CallableStatement, java.lang.String, java.lang.Object[])
	 */
	public  boolean setExecuteCall(CallableStatement proc, String SQL, Object[] parmvalue) throws SQLException, ConnectionException, ParamException {
	
		if (SQL == null) {
			throw new ParamException(" SQL is null ");
		}
		if (con == null) {
			throw new ConnectionException(" Connection is null ");
		}
	
		proc = con.prepareCall(SQL);
		for (int i = 0; i < parmvalue.length; i++) {
			ConvertUtil.importData(proc, i + 1, parmvalue[i],false);
		}
		return proc.execute();
	}

	/* (非 Javadoc)
	 * @see org.speedframework.execute.ExecuteHelper#setFunctionCall(java.sql.Connection, java.sql.CallableStatement, java.lang.String, java.lang.Object[], int)
	 */
	public  Object setFunctionCall( CallableStatement proc, String SQL, Object[] parmvalue, int returnType) throws SQLException, ConnectionException, ParamException {
	
		if (SQL == null) {
			throw new ParamException(" SQL is null ");
		}
		if (con == null) {
			throw new ConnectionException(" Connection is null ");
		}
		proc = con.prepareCall(SQL);
		proc.registerOutParameter(1, returnType);
		for (int i = 0; i < parmvalue.length; i++) {
			ConvertUtil.importData(proc, i + 2, parmvalue[i],false);
		}		
		proc.execute();
		return proc.getObject(1);		
	}

	


}

⌨️ 快捷键说明

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