当前位置: 首页>数据库>正文

java 操作数据库的事务可以写for java实现数据库

  1. 需要三个导入jar包,dbcp pool connection,这三个jar包都是apache公司的开源数据库连接帮助jar包,下载地址 http://commons.apache.org/ 
  2. 导入成功后添加三个帮助类,和一个配置文件,如下所示
dbhelp.java
package dzu.sc.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * 此类不供人员直接调用
 * 数据库连接工具类
 * */
public class DBHelp {

	private static BasicDataSource ds = new BasicDataSource();
	static {
		Properties properties = new Properties();
		try {
			properties.load(DBHelp.class.getClassLoader().getResourceAsStream(
					"dbconfig.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		String characterEncoding=properties.getProperty("characterEncoding");
		int InitialSize = Integer.parseInt(properties
				.getProperty("InitialSize"));
		int MaxActive = Integer.parseInt(properties.getProperty("MaxActive"));
		long MaxWait = Long.parseLong(properties.getProperty("MaxWait"));
		int MinIdle = Integer.parseInt(properties.getProperty("MinIdle"));
		
		ds.setDriverClassName(driver);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setInitialSize(InitialSize);
		ds.setMaxWait(MaxWait);
		ds.setMaxActive(MaxActive);
		ds.setMinIdle(MinIdle);
		//设置连接编码方式
		ds.setConnectionProperties(characterEncoding);
		//System.out.println("数据库初始化已完成.....");
	}

	/**
	 * 获取数据库连接对象
	 * 
	 * @return
	 */
	private Connection getConnection() {
		try {
			Connection conn = ds.getConnection();
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 执行insert update delete语句
	 * 
	 * @param sql
	 */
	public int executeSQL(String sql, Object... args) {
		int result=0;
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i + 1, args[i]);
			}
			System.out.println(stat.toString());
			result=stat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(stat, conn);
		}
		return result;
	}

	/**
	 * 查询select
	 * @throws SQLException 
	 */
	public List executeQuery(String sql, Object... args) {
		List list=null;
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i + 1, args[i]);
			}
			System.out.println(stat.toString());
			rs = stat.executeQuery();
			list=resultSetToList(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(stat, conn);
		}
		return list;
	}

	/**
	 * 释放连接
	 * 
	 * @param stat
	 * @param conn
	 */
	private void close(PreparedStatement stat, Connection conn) {
		close(null, stat, conn);
	}

	/**
	 * 释放连接
	 * 
	 * @param rs
	 * @param stat
	 * @param conn
	 */
	private void close(ResultSet rs, PreparedStatement stat, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stat != null) {
					stat.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 将resultset转化为List
	 * */
	private  List resultSetToList(ResultSet rs) throws java.sql.SQLException {   
        if (rs == null)   
            return Collections.EMPTY_LIST;   
        ResultSetMetaData md = rs.getMetaData(); 
        int columnCount = md.getColumnCount(); 
        List list = new ArrayList();   
        //将map放入集合中方便使用个别的查询
        Map rowData = new HashMap();   
        while (rs.next()) {   
         rowData = new HashMap(columnCount);
         //将集合放在map中
         for (int i = 1; i <= columnCount; i++) {   
                 rowData.put(md.getColumnName(i), rs.getObject(i));   
         }   
         list.add(rowData);   
        }  
        return list;   
}
}
dbhelp.java
package dzu.sc.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * 此类不供人员直接调用
 * 数据库连接工具类
 * */
public class DBHelp {

	private static BasicDataSource ds = new BasicDataSource();
	static {
		Properties properties = new Properties();
		try {
			properties.load(DBHelp.class.getClassLoader().getResourceAsStream(
					"dbconfig.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		String characterEncoding=properties.getProperty("characterEncoding");
		int InitialSize = Integer.parseInt(properties
				.getProperty("InitialSize"));
		int MaxActive = Integer.parseInt(properties.getProperty("MaxActive"));
		long MaxWait = Long.parseLong(properties.getProperty("MaxWait"));
		int MinIdle = Integer.parseInt(properties.getProperty("MinIdle"));
		
		ds.setDriverClassName(driver);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setInitialSize(InitialSize);
		ds.setMaxWait(MaxWait);
		ds.setMaxActive(MaxActive);
		ds.setMinIdle(MinIdle);
		//设置连接编码方式
		ds.setConnectionProperties(characterEncoding);
		//System.out.println("数据库初始化已完成.....");
	}

	/**
	 * 获取数据库连接对象
	 * 
	 * @return
	 */
	private Connection getConnection() {
		try {
			Connection conn = ds.getConnection();
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 执行insert update delete语句
	 * 
	 * @param sql
	 */
	public int executeSQL(String sql, Object... args) {
		int result=0;
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i + 1, args[i]);
			}
			System.out.println(stat.toString());
			result=stat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(stat, conn);
		}
		return result;
	}

	/**
	 * 查询select
	 * @throws SQLException 
	 */
	public List executeQuery(String sql, Object... args) {
		List list=null;
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i + 1, args[i]);
			}
			System.out.println(stat.toString());
			rs = stat.executeQuery();
			list=resultSetToList(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(stat, conn);
		}
		return list;
	}

	/**
	 * 释放连接
	 * 
	 * @param stat
	 * @param conn
	 */
	private void close(PreparedStatement stat, Connection conn) {
		close(null, stat, conn);
	}

	/**
	 * 释放连接
	 * 
	 * @param rs
	 * @param stat
	 * @param conn
	 */
	private void close(ResultSet rs, PreparedStatement stat, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stat != null) {
					stat.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 将resultset转化为List
	 * */
	private  List resultSetToList(ResultSet rs) throws java.sql.SQLException {   
        if (rs == null)   
            return Collections.EMPTY_LIST;   
        ResultSetMetaData md = rs.getMetaData(); 
        int columnCount = md.getColumnCount(); 
        List list = new ArrayList();   
        //将map放入集合中方便使用个别的查询
        Map rowData = new HashMap();   
        while (rs.next()) {   
         rowData = new HashMap(columnCount);
         //将集合放在map中
         for (int i = 1; i <= columnCount; i++) {   
                 rowData.put(md.getColumnName(i), rs.getObject(i));   
         }   
         list.add(rowData);   
        }  
        return list;   
}
}
DB.java
package dzu.sc.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 此类供程序员直接调用
 * */
public class DB {
	private static DBHelp dbHelp = null;
	static {
		dbHelp = new DBHelp();
	}

	/**
	 * 缺省查询
	 * */
	public static List select(String tableName) {
		String sql = "select * from " + tableName;
		return dbHelp.executeQuery(sql);
	}

	/**
	 * 特定条件查询
	 * */
	public static List select(String tableName, Map<String, Object> where) {
		String sql = "";
		int i = 0;
		Object[] args = new Object[where.size()];
		sql = "select * from " + tableName;
		if (where.size() != 0) {
			sql += " where ";
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i != 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=?";
					args[i++] = value;

				}
			}

		}
		return dbHelp.executeQuery(sql, args);
	}

	/**
	 * 可以查询某个表的特定列
	 * */
	public static List select(String tableName, List<String> colums,
			Map<String, Object> where) {
		String sql = "select ";
		int i = 0;
		Object[] args = new Object[where.size()];
		// 拼接列选择
		for (int m = 0; m < colums.size(); m++) {
			sql += colums.get(m).toString() + ",";
		}
		// 去掉多余的逗号
		sql = sql.substring(0, sql.length() - 1);
		sql += " from " + tableName;
		if (where.size() != 0) {
			sql += " where ";
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=?";
					args[i++] = value;

				}
			}

		}
		return dbHelp.executeQuery(sql, args);
	}

	/**
	 * 
	 * 插入 支持and多条件工作
	 * */

	public static int insert(String tableName, Map<String, Object> map) {
		String sql = "insert into " + tableName;
		int i = 0;
		// 用于存放代替问号的真实数值
		Object[] args = new Object[map.size()];
		if (map.size() != 0) {
			// 拼接sql字符串
			Set keys = map.keySet();
			if (keys != null) {
				sql += "(";
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					String key = (String) iterator.next();
					Object value = map.get(key);
					sql += key + ",";
					args[i++] = value;
				}
				sql = sql.substring(0, sql.length() - 1) + ")";
				sql += " value(";
				for (int t = 0; t < map.size(); t++) {
					sql += "?,";
				}
				sql = sql.substring(0, sql.length() - 1) + ")";
			}

		}
		return dbHelp.executeSQL(sql, args);
	}

	/**
	 * 更新 map集合为更新的参数和值 where集合为约束条件
	 * */
	public static int update(String tableName, Map<String, Object> map,
			Map<String, Object> where) {
		String sql = "update " + tableName + " set ";
		int i = 0;
		// 用于存放代替问号的真实数值
		Object[] args = new Object[map.size() + where.size()];
		if (map.size() != 0) {
			// 拼接sql字符串
			Set keys1 = map.keySet();
			Set keys2 = where.keySet();
			if (keys1 != null) {
				Iterator iterator = keys1.iterator();
				// 接收所有的参数
				while (iterator.hasNext()) {
					String key1 = (String) iterator.next();
					Object value1 = map.get(key1);
					sql += key1 + "=?,";
					args[i++] = value1;
				}
				sql = sql.substring(0, sql.length() - 1) + " where ";
				// 接收约束条件
				if (keys2 != null) {
					Iterator iterator2 = keys2.iterator();
					// 接收所有的参数
					while (iterator2.hasNext()) {
						if (i > map.size()) {
							sql += " and ";
						}
						String key2 = (String) iterator2.next();
						Object value2 = where.get(key2);
						sql += key2 + "=?";
						args[i++] = value2;
					}
				}

			}

		}
		return dbHelp.executeSQL(sql, args);
	}

	/**
	 * 删除 map集合为约束条件, 支持and多条件工作
	 * */
	public static int delete(String tableName, Map<String, Object> map) {
		String sql = "delete from " + tableName + " where ";
		int i = 0;
		// 用于存放代替问号的真实数值
		Object[] args = new Object[map.size()];
		if (map.size() != 0) {
			// 拼接sql字符串
			Set keys = map.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				// 接收所有的参数
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					String key = (String) iterator.next();
					Object value = map.get(key);
					sql += key + "=? ";
					args[i++] = value;
				}
			}

		}
		return dbHelp.executeSQL(sql, args);
	}

	/**
	 * 获取某个表的记录条数
	 * */
	public static int Conunt(String tableName) {
		List list = dbHelp.executeQuery("select count(*) as allcount from "
				+ tableName);
		Map map = (Map) list.get(0);
		return Integer.parseInt(map.get("allcount").toString());
	}

	/**
	 * 两表联合查询(查询全部的信息)
	 * */
	public static List union2Select(String table1, String table2,
			Map<String, Object> where) {
		String sql = "select * from " + table1 + " as t1 inner join " + table2
				+ " as t2 on ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);

	}

	/**
	 * 两表联合查询(查询指定列的信息)
	 * */
	public static List union2Select(String table1, String table2,
			List<String> colums, Map<String, Object> where) {
		String sql = "select ";
		for (int i = 0; i < colums.size(); i++) {
			sql += colums.get(i).toString() + ",";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql += " from " + table1 + " as t1 inner join " + table2 + " as t2 on ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);

	}

	/**
	 * 三表联合(全部查询)
	 * */

	public static List union3Select(String table1, String table2,
			String table3, List<String> colums, Map<String, Object> where) {
		String sql = "select ";
		for (int i = 0; i < colums.size(); i++) {
			sql += colums.get(i).toString() + ",";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql += " from " + table1 + "," + table2 + ", " + table3 + " where ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);
	}

	/**
	 * 三表联合查询(全部查询)
	 * */
	public static List union3Select(String table1, String table2,
			String table3, Map<String, Object> where) {
		String sql = "select * from " + table1 + "," + table2 + ", " + table3
				+ " where ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);

	}

}
DB.java
package dzu.sc.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 此类供程序员直接调用
 * */
public class DB {
	private static DBHelp dbHelp = null;
	static {
		dbHelp = new DBHelp();
	}

	/**
	 * 缺省查询
	 * */
	public static List select(String tableName) {
		String sql = "select * from " + tableName;
		return dbHelp.executeQuery(sql);
	}

	/**
	 * 特定条件查询
	 * */
	public static List select(String tableName, Map<String, Object> where) {
		String sql = "";
		int i = 0;
		Object[] args = new Object[where.size()];
		sql = "select * from " + tableName;
		if (where.size() != 0) {
			sql += " where ";
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i != 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=?";
					args[i++] = value;

				}
			}

		}
		return dbHelp.executeQuery(sql, args);
	}

	/**
	 * 可以查询某个表的特定列
	 * */
	public static List select(String tableName, List<String> colums,
			Map<String, Object> where) {
		String sql = "select ";
		int i = 0;
		Object[] args = new Object[where.size()];
		// 拼接列选择
		for (int m = 0; m < colums.size(); m++) {
			sql += colums.get(m).toString() + ",";
		}
		// 去掉多余的逗号
		sql = sql.substring(0, sql.length() - 1);
		sql += " from " + tableName;
		if (where.size() != 0) {
			sql += " where ";
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=?";
					args[i++] = value;

				}
			}

		}
		return dbHelp.executeQuery(sql, args);
	}

	/**
	 * 
	 * 插入 支持and多条件工作
	 * */

	public static int insert(String tableName, Map<String, Object> map) {
		String sql = "insert into " + tableName;
		int i = 0;
		// 用于存放代替问号的真实数值
		Object[] args = new Object[map.size()];
		if (map.size() != 0) {
			// 拼接sql字符串
			Set keys = map.keySet();
			if (keys != null) {
				sql += "(";
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					String key = (String) iterator.next();
					Object value = map.get(key);
					sql += key + ",";
					args[i++] = value;
				}
				sql = sql.substring(0, sql.length() - 1) + ")";
				sql += " value(";
				for (int t = 0; t < map.size(); t++) {
					sql += "?,";
				}
				sql = sql.substring(0, sql.length() - 1) + ")";
			}

		}
		return dbHelp.executeSQL(sql, args);
	}

	/**
	 * 更新 map集合为更新的参数和值 where集合为约束条件
	 * */
	public static int update(String tableName, Map<String, Object> map,
			Map<String, Object> where) {
		String sql = "update " + tableName + " set ";
		int i = 0;
		// 用于存放代替问号的真实数值
		Object[] args = new Object[map.size() + where.size()];
		if (map.size() != 0) {
			// 拼接sql字符串
			Set keys1 = map.keySet();
			Set keys2 = where.keySet();
			if (keys1 != null) {
				Iterator iterator = keys1.iterator();
				// 接收所有的参数
				while (iterator.hasNext()) {
					String key1 = (String) iterator.next();
					Object value1 = map.get(key1);
					sql += key1 + "=?,";
					args[i++] = value1;
				}
				sql = sql.substring(0, sql.length() - 1) + " where ";
				// 接收约束条件
				if (keys2 != null) {
					Iterator iterator2 = keys2.iterator();
					// 接收所有的参数
					while (iterator2.hasNext()) {
						if (i > map.size()) {
							sql += " and ";
						}
						String key2 = (String) iterator2.next();
						Object value2 = where.get(key2);
						sql += key2 + "=?";
						args[i++] = value2;
					}
				}

			}

		}
		return dbHelp.executeSQL(sql, args);
	}

	/**
	 * 删除 map集合为约束条件, 支持and多条件工作
	 * */
	public static int delete(String tableName, Map<String, Object> map) {
		String sql = "delete from " + tableName + " where ";
		int i = 0;
		// 用于存放代替问号的真实数值
		Object[] args = new Object[map.size()];
		if (map.size() != 0) {
			// 拼接sql字符串
			Set keys = map.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				// 接收所有的参数
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					String key = (String) iterator.next();
					Object value = map.get(key);
					sql += key + "=? ";
					args[i++] = value;
				}
			}

		}
		return dbHelp.executeSQL(sql, args);
	}

	/**
	 * 获取某个表的记录条数
	 * */
	public static int Conunt(String tableName) {
		List list = dbHelp.executeQuery("select count(*) as allcount from "
				+ tableName);
		Map map = (Map) list.get(0);
		return Integer.parseInt(map.get("allcount").toString());
	}

	/**
	 * 两表联合查询(查询全部的信息)
	 * */
	public static List union2Select(String table1, String table2,
			Map<String, Object> where) {
		String sql = "select * from " + table1 + " as t1 inner join " + table2
				+ " as t2 on ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);

	}

	/**
	 * 两表联合查询(查询指定列的信息)
	 * */
	public static List union2Select(String table1, String table2,
			List<String> colums, Map<String, Object> where) {
		String sql = "select ";
		for (int i = 0; i < colums.size(); i++) {
			sql += colums.get(i).toString() + ",";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql += " from " + table1 + " as t1 inner join " + table2 + " as t2 on ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);

	}

	/**
	 * 三表联合(全部查询)
	 * */

	public static List union3Select(String table1, String table2,
			String table3, List<String> colums, Map<String, Object> where) {
		String sql = "select ";
		for (int i = 0; i < colums.size(); i++) {
			sql += colums.get(i).toString() + ",";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql += " from " + table1 + "," + table2 + ", " + table3 + " where ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);
	}

	/**
	 * 三表联合查询(全部查询)
	 * */
	public static List union3Select(String table1, String table2,
			String table3, Map<String, Object> where) {
		String sql = "select * from " + table1 + "," + table2 + ", " + table3
				+ " where ";
		int i = 0;
		if (where.size() != 0) {
			Set keys = where.keySet();
			if (keys != null) {
				Iterator iterator = keys.iterator();
				while (iterator.hasNext()) {
					if (i > 0) {
						sql += " and ";
					}
					Object key = iterator.next();
					Object value = where.get(key);
					sql += key + "=" + value;
					i++;
				}

			}
		}
		return dbHelp.executeQuery(sql);

	}

}
JsonUtil.java
package dzu.sc.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sun.org.apache.bcel.internal.generic.NEW;


/**
 * json的操作类
 *
 */
public class JsonUtil {
	
/**
 * 传过list来将list里面的map取出来进行再加工生成json格式字符串
 * */
public static String ListToJson (List list ) {
	List newList =new ArrayList();
	int  listsize=list.size();
	Map oldMap = null; 
	Map newMap=null;
	for(int i=0;i<listsize;i++){
		oldMap = (Map)list.get(i);
		newMap=new HashMap();
        //将集合放在map中
		Set set=oldMap.keySet();
		Iterator iterator=set.iterator();
       while(iterator.hasNext()) {
        	Object key=iterator.next();
        	Object value=oldMap.get(key);
        	newMap.put("\""+key+"\"","\""+value+"\"");   
        }   
       newList.add(newMap);   
	}
	
	String str=newList.toString();
	str=str.replace('=', ':');
	return str;
}
}
JsonUtil.java
package dzu.sc.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sun.org.apache.bcel.internal.generic.NEW;


/**
 * json的操作类
 *
 */
public class JsonUtil {
	
/**
 * 传过list来将list里面的map取出来进行再加工生成json格式字符串
 * */
public static String ListToJson (List list ) {
	List newList =new ArrayList();
	int  listsize=list.size();
	Map oldMap = null; 
	Map newMap=null;
	for(int i=0;i<listsize;i++){
		oldMap = (Map)list.get(i);
		newMap=new HashMap();
        //将集合放在map中
		Set set=oldMap.keySet();
		Iterator iterator=set.iterator();
       while(iterator.hasNext()) {
        	Object key=iterator.next();
        	Object value=oldMap.get(key);
        	newMap.put("\""+key+"\"","\""+value+"\"");   
        }   
       newList.add(newMap);   
	}
	
	String str=newList.toString();
	str=str.replace('=', ':');
	return str;
}
}

说明:DB类为程序员直接调用的类,里面封装了对数据库的增删改查等操作,参数需要1.表名;2.列名;3.where条件其中表名为字符串格式,列名用list来包装,条件需要放到一个map集合中,函数返回list集合,返回的list集合中封装了很多map,方便单独查询每一个的值,jsonutil帮助类能够将返回的list集合直接转换为json格式发送到前台,程序员只要再写一个单独的severlet来调用完成后返回前台数据集合,再在前台进行样式的拼接。


https://www.xamrdz.com/database/6vu1959933.html

相关文章: