自己封装的一个JDBC工具类

源码:

package com.util;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.*;

/**
 * 工具类:负责数据库操作
 * Created by Ethel_oo on 2018/3/19.
 */
public class DBCompleteUtil {

    private static String driverClass;
    private static String url;
    private static String username_sql;
    private static String password_sql;

    private static Connection connection;
    private static PreparedStatement preparedStatement;
    private static ResultSet resultSet;
	
    static {
        try {
            //读取SQL配置文件
            InputStream in = DBCompleteUtil.class.getClassLoader().getResourceAsStream("/jdbc.properties");
            Properties properties = new Properties();
            properties.load(in);
            //获取参数
            driverClass = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            username_sql = properties.getProperty("username");
            password_sql = properties.getProperty("password");
            System.out.println(driverClass + " " + url + " " + username_sql + " " + password_sql + " ");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 封装jdbc连接
     *
     */
    public static Connection getConnection() {
        try {
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, username_sql, password_sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 封装jdbc增加,删除,修改
     *
     */
    public static boolean executeUpdate(String sql, Object... params) throws SQLException {
        boolean flag = false;
        int result = -1;
        connection = getConnection();
        preparedStatement = connection.prepareStatement(sql);
        int index = 1;
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(index++, params[i]);
            }
        }
        result = preparedStatement.executeUpdate();
        flag = result > 0 ? true : false;
        closeAll();
        return flag;
    }

    /**
     * 封装jdbc查询方法
     *
     */
    public static List<Map<String, Object>> executeQuery(String sql, Object... params) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        int index = 1;
        connection = getConnection();
        preparedStatement = connection.prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(index++, params[i]);
            }
        }
        resultSet = preparedStatement.executeQuery();
        ResultSetMetaData setMetaData = resultSet.getMetaData();
        // 获取列的数量
        int col_len = setMetaData.getColumnCount();
        while (resultSet.next()) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < col_len; i++) {
                String col_name = setMetaData.getColumnName(i + 1);
                Object col_value = resultSet.getObject(col_name);
                if (col_value == null) {
                    col_value = "";
                }
                map.put(col_name, col_value);
            }
            list.add(map);
        }
        closeAll();
        return list;
    }

    /**
     * close所有的jdbc操作
     */
    public static void closeAll() {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

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

使用记录

查询

	String sql="select * from user where username = ? and password = ?";
	Object[] params = new Object[] {username, password};
	List<Map<String, Object>> list = null;
	
	try {
		list = DBCompleteUtil.executeQuery(sql, params);
	} catch (SQLException e) {
		e.printStackTrace();
	}

增删改

	String sql="insert into user values(?, ?, ?)";
	Object[] params = new Object[] {UUID.randomUUID().toString(), username, password};
	boolean flag = false;
	
	try {
		flag = DBCompleteUtil.executeUpdate(sql, params);
	} catch (SQLException e) {
		e.printStackTrace();
	}

猜你喜欢

转载自my.oschina.net/u/2427564/blog/1798722