/**
* @Description: 连接数据库的工具类,被定义成不可继承且是私有访问
* @author chenlushun
* @date 2016年4月1日 上午11:34:41
*/
public final class DBUtils {
private static String url;
private static String user;
private static String psw;
private static Connection connection;
private PreparedStatement pstmt;// 有效的使SQL和参数分开,防止SQL注入,使程序更加健壮
private ResultSet rs;
public static void main(String[] args) {
connection = DBUtils.getConnection();
System.out.println(connection);
}
static {
Properties prop = new Properties();
try {
prop.load(DBUtils.class.getResourceAsStream("/jdbc.properties"));
user = prop.getProperty("jdbc.username");
psw = prop.getProperty("jdbc.passwd");
url = prop.getProperty("jdbc.url");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取数据库文件异常", e);
}
}
/* 防止通过构造器实例化 */
private DBUtils() {
}
/**
* 获取数据库的连接
*
* @return conn
*/
public static Connection getConnection() {
if (null == connection) {
try {
connection = DriverManager.getConnection(url, user, psw);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return connection;
}
/**
* 执行修改添加删除操作
*
* @param sql
* @param params
* @return
* @throws SQLException
*/
public boolean updateByPreparedStatment(String sql, List<?> params) throws SQLException {
boolean flag = false;
int result = -1;// 添加删除修改时所影响的数据库行数
pstmt = connection.prepareStatement(sql);
int index = 1;
// 填充sql的占位符
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
result = pstmt.executeUpdate();
flag = result > 0 ? true : false;
return flag;
}
/**
*
* @param sql
* @param params
*/
public List<Map<String, Object>> findResult(String sql, List<?> params) throws SQLException {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
rs = pstmt.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
int cols_len = metaData.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < cols_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = rs.getObject(cols_name);
if (cols_value == null) {
cols_value = "";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
return list;
}
/**
* 释放资源
*
* @param conn
* @param pstmt
* @param rs
*/
public void closeResources(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
}
}
}
<pre name="code" class="plain">jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.passwd=passwd
jdbc.url=jdbc:mysql://120.27.100.226:3306/authority