java jdbc 封装类_JAVA封装的JDBC工具类

import java.io.IOException;

import java.io.InputStream;

import java.sql.*;

import java.util.*;

/**

* 工具类:负责数据库操作

*

*/

public class DBUtil {

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配置文件 配置文件在src下

InputStream in = DBUtil.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");

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 封装jdbc连接

*

* @return

*/

public static Connection getConnection() {

try {

Class.forName(driverClass);

connection = DriverManager.getConnection(url, username_sql, password_sql);

} catch (Exception e) {

e.printStackTrace();

}

return connection;

}

/**

* 封装jdbc增加,删除,修改

*

* @param sql

* @param params

* @return

* @throws SQLException

*/

public boolean executeUpdate(String sql, Object... params) throws SQLException {

boolean flag = false;

try {

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;

} catch (Exception e) {

e.printStackTrace();

} finally {

closeAll();

}

return flag;

}

/**

* 封装jdbc查询方法

*

* @param sql

* @param params

* @return

* @throws SQLException

*/

public static List> executeQuery(String sql, Object... params) throws SQLException {

List> list = new ArrayList>();

try {

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 map = new HashMap();

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);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

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();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值