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

📄 basedao.java

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Vector;


import cn.dongsw.fw.StatementUtils;

public class BaseDao {
	private JdbcConnection jdbcConn = new JdbcConnection();
	
	/**
	 * @author 董双伟
	 * 2009-5-15 上午10:48:06
	 * TODO 查找数据
	 * 输入:
	 * 功能:
	 * 输出:
	 * @param sql
	 * @param types
	 * @param params
	 * @return
	 */
	public List find(String sql,int [] types, Object [] params){
			Connection conn = jdbcConn.getConnection();
			PreparedStatement pst = null;
			List result = new Vector();
			ResultSet rs = null;
			try {
				pst = conn.prepareStatement(sql);
				if (types != null && params != null) {
					StatementUtils.setParameterValue(pst,
							types, params);
	
				} else if (params != null) {
					StatementUtils.setParameterValue(pst,
							params);
				}
	
				rs = pst.executeQuery();
				ResultSetMetaData rsmd = rs.getMetaData();
				int colum = rsmd.getColumnCount();
				int[] type = new int[colum + 1];
				for (int i = 1; i <= colum; i++) {
					type[i] = rsmd.getColumnType(i);
				}
				while (rs.next()) {
					Object[] values = new Object[colum];
					for (int i = 1; i <= colum; i++) {
						switch (type[i]) {
						case Types.NUMERIC:
							values[i - 1] = new Double(rs
									.getDouble(i));
						case Types.VARCHAR:
							values[i - 1] = rs.getString(i);
							break;
						case Types.DATE:
							values[i - 1] = rs.getDate(i);
							break;
						case Types.TIMESTAMP:
							values[i - 1] = rs.getTimestamp(i);
							break;
						default:
							values[i - 1] = rs.getObject(i);
							break;
						}
					}
					result.add(values);
				}
	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					pst.close();
					pst = null;
					rs.close();
				} catch (Exception e) {
				}
			}
			return result;
	}
	
	/**
	 * @author 董双伟
	 * 2009-5-15 上午10:47:46
	 * TODO 保存和更新数据
	 * 输入:
	 * 功能:
	 * 输出:
	 * @param sql
	 * @param types
	 * @param params
	 */
	public void saveOrUpdate(String sql,int [] types, Object [] params){
		
		Connection conn = jdbcConn.getConnection();
		PreparedStatement pst = null;
		try{
			pst = conn.prepareStatement(sql);
			if (types != null && params != null) {
				StatementUtils.setParameterValue(pst,
						types, params);
	
			} else if (params != null) {
				StatementUtils.setParameterValue(pst,
						params);
			}
			pst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				pst.close();
				pst = null;
			} catch (Exception e) {
			}
		}
	}
	/**
	 * @author 董双伟
	 * 2009-5-15 上午10:47:28
	 * TODO 删除数据
	 * 输入:
	 * 功能:
	 * 输出:
	 * @param sql
	 * @param types
	 * @param params
	 */
public void delete(String sql,int [] types, Object [] params){
		
		Connection conn = jdbcConn.getConnection();
		PreparedStatement pst = null;
		try{
			pst = conn.prepareStatement(sql);
			if (types != null && params != null) {
				StatementUtils.setParameterValue(pst,
						types, params);
	
			} else if (params != null) {
				StatementUtils.setParameterValue(pst,
						params);
			}
			pst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				pst.close();
				pst = null;
			} catch (Exception e) {
			}
		}
	}
}

⌨️ 快捷键说明

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