dbutils

本文介绍了一个Java程序中使用的DbUtils工具类,该工具类通过封装数据库连接池和查询运行器,实现了数据库增删改查操作的简化。文章详细展示了如何利用DbUtils进行数据插入、更新、删除及查询等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package g.Factory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class DbUtils
{

	public static void main(String[] args) throws Exception
	{
		// TODO Auto-generated method stub
		String sql = "select * from usrs";

		System.out.println(exeJSONObject(sql, null));
		// TestSql();
	}

	private static DruidDataSource dataSource = null;
	static
	{
		try
		{
			dataSource = new DruidDataSource();
			dataSource.setDriverClassName("com.mysql.jdbc.Driver");
			dataSource.setUrl("jdbc:mysql://localhost:3306/wxbase?autoReconnect=true&useUnicode=true&characterEncoding=utf-8");
			dataSource.setUsername("root");
			dataSource.setPassword("qq1111");
			dataSource.setInitialSize(3);
			dataSource.setMinIdle(1);
			dataSource.setMaxActive(10);
			// dataSource.setFilters("stat");
			dataSource.setPoolPreparedStatements(false);

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private static synchronized Connection getConnection()
	{
		Connection conn = null;
		try
		{
			conn = dataSource.getConnection();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return conn;
	}

	public static JSONObject getParamJsonObject(HttpServletRequest request)
	{
		if (request.getParameter("ir") == null || !request.getParameter("ir").equalsIgnoreCase("1"))
			return null;
		Map<String, String[]> m = request.getParameterMap();
		JSONObject json = new JSONObject(true);
		for (String key : m.keySet())
		{
			String s = "";
			for (String v : m.get(key))
			{
				if (s == "")
					s += v;
				else
					s += "," + v;
			}
			json.put(key, s);
		}
		return json;
	}

	public static int exeInsert(JSONObject json, String tableName) throws Exception
	{
		String sql = "insert into " + tableName + " (";
		for (Entry<String, Object> entry : json.entrySet())
			sql += entry.getKey() + ",";
		sql += ") values ('";
		for (Entry<String, Object> entry : json.entrySet())
			sql += entry.getValue() + "','";
		sql += ");";
		sql = sql.replace(",')", ")").replace(",)", ")");
		int i = exeCommand(sql, null);
		return i;
	}

	public static int exeUpdate(JSONObject json, String tableName, String where) throws SQLException
	{
		String sql = "update " + tableName + " set ";
		for (Entry<String, Object> entry : json.entrySet())
			sql += entry.getKey() + "='" + entry.getValue() + "',";
		sql += "where (" + where + ");";
		sql = sql.replaceAll(",where", " where");
		int i = exeCommand(sql, null);
		return i;
	}

	public static int exeDelete(String sql) throws SQLException
	{
		int i = exeCommand(sql, null);
		return i;
	}

	public static int exeCommand(String sql, Object[] parm) throws SQLException
	{
		Connection con = getConnection();
		QueryRunner runner = new QueryRunner();
		int i = runner.update(con, sql, parm);

		if (!con.isClosed())
			con.close();

		return i;
	}

	public static List<Map<String, Object>> exeFinds(String sql, Object[] parm) throws SQLException
	{
		Connection con = getConnection();
		QueryRunner runner = new QueryRunner();
		List<Map<String, Object>> listmap = runner.query(con, sql, new MapListHandler(), parm);
		if (!con.isClosed())
			con.close();
		return listmap;
	}

	public static JSONArray exeJSONArray(String sql, Object[] parm) throws SQLException
	{
		Connection con = getConnection();
		QueryRunner runner = new QueryRunner();
		List<Map<String, Object>> listmap = runner.query(con, sql, new MapListHandler(), parm);
		if (!con.isClosed())
			con.close();
		return JSONArray.parseArray(JSON.toJSONString(listmap, SerializerFeature.WriteDateUseDateFormat));
	}

	public static Map<String, Object> exeFind(String sql, Object[] parm) throws SQLException
	{
		Connection con = getConnection();
		QueryRunner runner = new QueryRunner();
		Map<String, Object> map = runner.query(con, sql, new MapHandler(), parm);
		if (!con.isClosed())
			con.close();
		return map;
	}

	public static JSONObject exeJSONObject(String sql, Object[] parm) throws SQLException
	{
		Connection con = getConnection();
		QueryRunner runner = new QueryRunner();
		Map<String, Object> map = runner.query(con, sql, new MapHandler(), parm);
		if (!con.isClosed())
			con.close();
		return JSONObject.parseObject(JSON.toJSONString(map, SerializerFeature.WriteDateUseDateFormat));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值