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

📄 commondao.java

📁 JAVA E拍网上购物业务管理 使用CRM业务流程
💻 JAVA
字号:
package org.ch02zf.dao.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import org.apache.log4j.Logger;

public class CommonDao {
	private Connection conn = null;
	private String sqlvalue = null;
	private List values = null;
	private Logger logger = Logger.getLogger(CommonDao.class.getName());
	public void setConn(Connection conn) {
		this.conn = conn;
	}
	public void setSqlvalue(String sqlvalue) {
		this.sqlvalue = sqlvalue;
	}
	public void setValues(List values) {
		this.values = values;
	}
	
	//给PreparedStatement占位符传参
	private void setValues(PreparedStatement pstmt,List values) throws SQLException
	{
		for(int i=0;i<values.size();i++)
		{
			pstmt.setObject(i+1, values.get(i));
		}
	}
	
	public Result executeQuery() throws SQLException
	{
		Result result = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		Statement stmt = null;
		
		try{
			if(values != null && values.size()>0)
			{
				pstmt = conn.prepareStatement(sqlvalue);
				this.setValues(pstmt, values);
				rs = pstmt.executeQuery();
			}
			else
			{
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sqlvalue);
				logger.info(sqlvalue);
			}
			result = ResultSupport.toResult(rs);
		}catch(Exception e)
		{
			logger.info("查询失败"+e.getMessage());
		}finally
		{
			if(rs != null)
			{
				rs.close();
				rs = null;
			}
			if(stmt != null)
			{
				stmt.close();
				stmt = null;
			}
			if(pstmt != null)
			{
				pstmt.close();
				pstmt = null;
			}
		}
		return result;
	}
	
	public int executeUpdate() throws SQLException
	{
		int flagRows = 0;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		Statement stmt = null;
		try{
			if(values != null && values.size()>0)
			{
				pstmt = conn.prepareStatement(sqlvalue);
				this.setValues(pstmt, values);
				flagRows = pstmt.executeUpdate();
				logger.info(sqlvalue);
			}
			else
			{
				stmt = conn.createStatement();
				flagRows = stmt.executeUpdate(sqlvalue);
			}
			
		}catch(Exception e)
		{
			logger.info("修改失败"+e.getMessage());
		}finally
		{
			if(rs != null)
			{
				rs.close();
				rs = null;
			}
			if(stmt != null)
			{
				stmt.close();
				stmt = null;
			}
			if(pstmt != null)
			{
				pstmt.close();
				pstmt = null;
			}
		}
		return flagRows;
	}
	
}

⌨️ 快捷键说明

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