源碼:
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();
}