這裏提供使用Map返回當行記錄的封裝
public Map<String, Object> selectSimpleResult(String sql, List<Object> params)
和 返回List<Map<String,Object>>
返回多行記錄從而查詢多行記錄,這裏list代表多行記錄,每一行當中的都代表一個鍵值對,從而實現返回多行記錄
public List<Map<String, Object>> selectMoreResult(String sql, List<Object> params)
package jdbcTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author chencong
* @Time 2017年8月3日 上午9:37:00
* @TODO 進行封裝jdbc方法的增刪改查 , 同時提供反射機制封裝
*/
public class JDBCUtils {
private static final String DRIVER_URL = "jdbc:sqlserver://localhost:1433;DataBaseName=mangerQQSys";
private static final String DRIVER_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DB_USER = "sa";
private static final String DB_PASSWORD = "123456";
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public JDBCUtils() {
try {
Class.forName(DRIVER_NAME);
System.out.println("連接成功!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 提供Connection的getConnection()方法獲得一個數據庫連接
*
* @return 返回數據庫連接Connection
*/
public Connection getConnection() {
try {
connection = DriverManager.getConnection(DRIVER_URL, DB_USER, DB_PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/**
* 增加、刪除、修改
* <p>
* sql = "select stuId , stuName form student where stuId = ?"
* 此時的params中就爲這裏?的佔位符
*
* @param sql
* 將要執行的的sql語句
* @param params
* sql語句中的參數
* @return 返回是否執行成功
* @throws SQLException
* 拋出sql異常
*/
public boolean updateByPreparedStatement(String sql, List<Object> params) throws SQLException {
if (connection == null) {
return false;
}
if (sql == null || params == null || params.isEmpty()) {
return false;
}
int result = -1;
boolean flag = false;
int index = 1;
preparedStatement = connection.prepareStatement(sql);
// 遍歷參數列表
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(index, params.get(i));
index++;
}
result = preparedStatement.executeUpdate();
flag = result > 0 ? true : false;
return flag;
}
/**
* 單條記錄查詢
*
* @param sql
* 將要執行的sql語句
* @param params
* 該條sql語句當中的參數列表
* @return 返回單條記錄的map集合
* @throws SQLException
*/
public Map<String, Object> selectSimpleResult(String sql, List<Object> params) throws SQLException {
if (connection == null) {
return null;
}
if (sql == null || params == null || params.isEmpty()) {
return null;
}
Map<String, Object> map = new HashMap<>();
int index = 1;
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(index, params.get(i));
index++;
}
// 返回查詢結果
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int col_length = metaData.getColumnCount();
while (resultSet.next()) {
for (int i = 0; i < col_length; i++) {
String col_name = metaData.getColumnName(i + 1); // 列名從1開始
Object col_value = resultSet.getObject(col_name); // 通過列名查詢該列的值
if (col_value == null) {
col_value = "";
}
map.put(col_name, col_value);
}
}
return map;
}
/**
* 多行記錄查詢
*
* @param sql
* @param params
* @return
* @throws SQLException
*/
public List<Map<String, Object>> selectMoreResult(String sql, List<Object> params) throws SQLException {
if (connection == null) {
return null;
}
if (sql == null || params == null || params.isEmpty()) {
return null;
}
int index = 1;
List<Map<String, Object>> list = new ArrayList<>();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(index, params.get(i));
index++;
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int col_length = metaData.getColumnCount();
while (resultSet.next()) {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < col_length; i++) {
String col_name = metaData.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);
}
return list;
}
/**
* 關閉數據庫連接
*
* @param resultSet
* @param statement
* @param connection
* @throws SQLException
*/
public void closeAll(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
}
/**
* 關閉連接
*
* @param resultSet
* @param statement
* @throws SQLException
*/
public void closeAll(ResultSet resultSet, Statement statement) throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
}
}