JDBCUtil資料庫查詢工具類的終極封裝
阿新 • • 發佈:2018-11-10
JDBCUtil.class
package com.hwadee.util;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
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.List;
import java.util.Properties;
import com.hwadee.entity.Book;
import com.hwadee.entity.User;
public class JDBCUtil {
public static String url = null;
public static String username = null;
public static String password = null;
public static String Driver = null;
static {
InputStream inputStream = JDBCUtil.class.getResourceAsStream("/db.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
url = properties.getProperty("url" );
username = properties.getProperty("username");
password = properties.getProperty("password");
Driver = properties.getProperty("Driver");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//載入資料庫驅動
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取資料庫連線物件
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
/**
* 新增刪除修改資料
* @return
*/
public static boolean insertDeleteUpdateExecute(String sql,ArrayList<Object> paras) {
Connection conn=getConnection();
PreparedStatement pst=null;
boolean flag = false;
int index = 1;
try {
pst=conn.prepareStatement(sql);
if(paras != null&& paras.size()>0) {
pst.clearParameters();
for(int i = 0;i<paras.size();i++) {
pst.setObject(index++, paras.get(i));
}
}
int result=pst.executeUpdate();
flag = result > 0? true : false;
} catch (SQLException e) {
flag = false;
e.printStackTrace();
}finally {
close(conn, pst);
}
return flag;
}
/**
* 獲取單個物件 可用於登入註冊驗證
* @param sql
* @param paras
* @param cls
* @return
*/
public static <T> T findBySingleObject(String sql,ArrayList<Object> paras,Class<T> cls) {
Connection conn =getConnection();
PreparedStatement pst = null;
ResultSet rs=null;
T singleObject = null;
int index = 1;
try {
pst = conn.prepareStatement(sql);
if(paras != null && paras.size()>0) {
pst.clearParameters();
for(int i = 0;i<paras.size();i++) {
pst.setObject(index++,paras.get(i));
}
}
rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while(rs.next()) {
singleObject = cls.newInstance();
for(int i = 0;i<columnCount;i++) {
String columnName = rsmd.getColumnName(i+1);
Object columnValue = rs.getObject(columnName);
Field field = cls.getDeclaredField(columnName);
field.setAccessible(true);
field.set(singleObject, columnValue);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close(conn, pst);
}
return singleObject;
}
/**
*
* 列表查詢
*/
public static<T> List<T> queryListExecute(String sql,ArrayList<Object> paras,Class<T> cls ){
Connection conn = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
T singleObject = null;
int index = 1;
List <T> list = new ArrayList<T>();
try {
pst = conn.prepareStatement(sql);
if(paras !=null && paras.size()>0) {
pst.clearParameters();
for(int i = 0;i<paras.size();i++) {
pst.setObject(index++, paras.get(i));
}
}
rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while(rs.next()) {
singleObject = cls.newInstance();
for(int i = 0;i<columnCount;i++) {
String columnName = rsmd.getColumnName(i+1);
Object columdValue = rs.getObject(columnName);
Field field = cls.getDeclaredField(columnName);
field.setAccessible(true);
field.set(singleObject, columdValue);
}
list.add(singleObject);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close(conn, pst, rs);
}
return list;
}
/*
* 釋放資源
*/
public static void close(Connection conn, PreparedStatement pst) {
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn, PreparedStatement pst,
ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
db.properties
url = jdbc:mysql://localhost:3306/library?zeroDateTimeBehavior=convertToNull
username = root
password = 123456
Driver = com.mysql.jdbc.Driver