JDBC 再封装
package login.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBManager<T> {
public static String DRIVER = "com.mysql.jdbc.Driver";
public static String URL = "jdbc:mysql:///db";
public static String USERNAME = "root";
public static String PASSWORD = "root";
/**
* 获取数据库连接
* @return
*/
public Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 执行insert update delete SQl
* @param sql SQL语句
* @param params 参数列表
* @return
*/
public int executeSQL(String sql, Object... params) {
Connection conn = getConnection();
PreparedStatement ps = null;
int rows = 0;
try {
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rows = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(ps, conn);
}
return rows;
}
/**
* 根据Select查询产生Object对象
* @param sql
* @param map
* @param params
* @return
*/
public T queryForObject(String sql, IRowMap<T> map, Object... params) {
T obj = null;
Connection conn = getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs = ps.executeQuery();
if (rs.next()) {
obj = map.mapRow(rs);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs, ps, conn);
}
return obj;
}
/**
* 根据SQL查询 返回int类型结果
* @param sql
* @param params
* @return
*/
public int queryForInt(String sql, Object... params) {
int obj = 0;
Connection conn = getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs = ps.executeQuery();
if (rs.next()) {
obj = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs, ps, conn);
}
return obj;
}
/**
* 根据Select查询产生List集合
* @param sql
* @param map
* @param params
* @return
*/
public List<T> queryForList(String sql, IRowMap<T> map, Object... params) {
List<T> list = null;
Connection conn = getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs = ps.executeQuery();
while (rs.next()) {
if (list == null) {
list = new ArrayList<T>();
}
T obj = map.mapRow(rs);
list.add(obj);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs, ps, conn);
}
return list;
}
public void close(PreparedStatement ps, Connection conn) {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public void close(ResultSet rs, PreparedStatement ps, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
package login.util;
import java.sql.ResultSet;
import java.sql.SQLException;
public interface IRowMap<T> {
public T mapRow(ResultSet rs)throws SQLException;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。