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

com.j2mvc.framework.dao.JdbcDaoSupport 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;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import com.j2mvc.framework.dao.callback.PreparedStatementCallBack;
import com.j2mvc.framework.dao.callback.PreparedStatementCreator;
/**
 * 
 * JDBC访问,通过预编译创建器创建语句,通过回调类返回查询结果
 * 
 * 2014-2-24 创建@杨朔
 */
public class JdbcDaoSupport {
	Logger log = Logger.getLogger(getClass().getName());
	protected String dataSourceName;
	public JdbcDaoSupport(){}
	/**
	 * 
	 * @param 
	 * @param creator 自定义创建器
	 * @param callBack 自定义回调
	 * 
	 */
	public  T execute(PreparedStatementCreator creator,PreparedStatementCallBack callBack){
		
		T t = null;
		Connection con = null;
		if(dataSourceName!=null && !dataSourceName.equals("")){
			con = DataSourceJndi.getConnection(dataSourceName);
		}
		if(con == null){
			con = DataSourceJndi.getConnection();
		}
		
		try {
			PreparedStatement pstmt = creator.execute(con);
			t =  callBack.execute(pstmt);
		} catch (Exception ex) {
			ex.printStackTrace();
			log.error((t!=null?t.getClass()+" error:":"") + ex.getMessage());
		}
		finally{
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					log.error("Connection SQLException error:"+e.getMessage()); 
				}
   		}
		return t;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy