All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.zhuang.data.jdbc.JdbcUtils Maven / Gradle / Ivy
package com.zhuang.data.jdbc;
import com.zhuang.data.config.MyDataProperties;
import com.zhuang.data.exception.ExecuteSqlException;
import com.zhuang.data.exception.GetConnectionException;
import com.zhuang.data.jdbc.util.ResultSetUtils;
import java.sql.*;
import java.util.List;
import java.util.Map;
public class JdbcUtils {
private static MyDataProperties zhuangDataProperties;
static {
zhuangDataProperties = new MyDataProperties();
try {
Class.forName(zhuangDataProperties.getJdbcDriver());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int executeNonQuery(String sql, Object... params) {
return executeNonQuery(getConnection(), sql, params);
}
public static int executeNonQuery(Connection connection, String sql, Object... params) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
if (params != null) {
int pIndex = 1;
for (Object item : params) {
preparedStatement.setObject(pIndex++, item);
}
}
return preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new ExecuteSqlException(e.getMessage(), e);
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static T queryEntity(Class entityType, String sql, Object... params) {
return queryEntity(getConnection(), entityType, sql, params);
}
public static T queryEntity(Connection connection, Class entityType, String sql, Object... params) {
List resultList = queryEntities(connection, entityType, sql, params);
if (resultList.size() > 1)
throw new RuntimeException("“JdbcUtils.queryEntity”方法内查询结果集有多条记录!");
else if (resultList.size() == 1)
return resultList.get(0);
else
return null;
}
public static List queryEntities(Class entityType, String sql, Object... params) {
return queryEntities(getConnection(), entityType, sql, params);
}
public static List queryEntities(Connection connection, Class entityType, String sql, Object... params) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
if (params != null) {
int pIndex = 1;
for (Object item : params) {
preparedStatement.setObject(pIndex++, item);
}
}
ResultSet resultSet = preparedStatement.executeQuery();
List entityList = ResultSetUtils.readToEntities(resultSet, entityType);
return entityList;
} catch (SQLException e) {
throw new ExecuteSqlException(e.getMessage(), e);
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Map queryMap(String sql, Object... params) {
return queryMap(getConnection(), sql, params);
}
public static Map queryMap(Connection connection, String sql, Object... params) {
List resultList = queryMaps(connection, sql, params);
if (resultList.size() > 1)
throw new RuntimeException("“JdbcUtils.queryMap”方法内查询结果集有多条记录!");
else if (resultList.size() == 1)
return resultList.get(0);
else
return null;
}
public static List queryMaps(String sql, Object... params) {
return queryMaps(getConnection(), sql, params);
}
public static List queryMaps(Connection connection, String sql, Object... params) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
if (params != null) {
int pIndex = 1;
for (Object item : params) {
preparedStatement.setObject(pIndex++, item);
}
}
ResultSet resultSet = preparedStatement.executeQuery();
List maps = ResultSetUtils.readToMaps(resultSet);
return maps;
} catch (SQLException e) {
throw new ExecuteSqlException(e.getMessage(), e);
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(zhuangDataProperties.getJdbcUrl(), zhuangDataProperties.getJdbcUserName(), zhuangDataProperties.getJdbcPassword());
} catch (SQLException e) {
throw new GetConnectionException("JdbcDbAccessor获取Connection失败!", e);
}
}
}