package com.weavernorth.jdbc;
import java.sql.CallableStatement;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 数据库连接类 说明:封装了 无参,有参,存储过程的调用
*
* @author Dylan
*
*/
public class ConnectionDB {
/**
* 数据库驱动类名称
*/
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 连接字符串
*/
private static final String URLSTR = "jdbc:sqlserver://localhost:1433; databaseName=ecology8";
/**
* 用户名
*/
private static final String USERNAME = "sa";
/**
* 密码
*/
private static final String USERPASSWORD = "dylan";
/**
* 创建数据库连接对象
*/
private Connection connnection = null;
/**
* 创建PreparedStatement对象
*/
private PreparedStatement preparedStatement = null;
/**
* 创建CallableStatement对象
*/
private CallableStatement callableStatement = null;
/**
* 创建结果集对象
*/
private ResultSet resultSet = null;
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动错误");
System.out.println(e.getMessage());
}
}
/**
* 建立数据库连接
*
* @return 数据库连接
*/
public Connection getConnection() {
try {
connnection = DriverManager.getConnection(URLSTR, USERNAME,
USERPASSWORD);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return connnection;
}
/**
* insert update delete SQL语句的执行的统一方法
*
* @param sql
* SQL语句
* @param params
* 参数数组,若没有参数则为null
* @return 受影响的行数
*/
public int executeUpdate(String sql, Object[] params) {
int affectedLine = 0;
try {
connnection = this.getConnection();
preparedStatement = connnection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
affectedLine = preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}
return affectedLine;
}
/**
* SQL 查询将查询结果直接放入ResultSet中
*
* @param sql
* SQL语句
* @param params
* 参数数组,若没有参数则为null
* @return 结果集
*/
public ResultSet executeQueryRS(String sql, Object[] params) {
try {
connnection = this.getConnection();
preparedStatement = connnection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return resultSet;
}
/**
* SQL 查询将查询结果:一行一列
*
* @param sql
* SQL语句
* @param params
* 参数数组,若没有参数则为null
* @return 结果集
*/
public Object executeQuerySingle(String sql, Object[] params) {
Object object = null;
try {
connnection = this.getConnection();
preparedStatement = connnection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
object = resultSet.getObject(1);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}
return object;
}
/**
* 获取结果集,并将结果放在List中
*
* @param sql
* SQL语句
* @return List 结果集
*/
public List<Object> excuteQuery(String sql, Object[] params) {
ResultSet rs = executeQueryRS(sql, params);
ResultSetMetaData rsmd = null;
int columnCount = 0;
try {
rsmd = rs.getMetaData();
columnCount = rsmd.getColumnCount();
} catch (SQLException e1) {
System.out.println(e1.getMessage());
}
List<Object> list = new ArrayList<Object>();
try {
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
map.put(rsmd.getColumnLabel(i), rs.getObject(i));
}
list.add(map);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}
return list;
}
/**
* 存储过程带有一个输出参数的方法
*
* @param sql
* 存储过程语句
* @param params
* 参数数组
* @param outParamPos
* 输出参数位置
* @param SqlType
* 输出参数类型
* @return 输出参数的值
*/
public Object excuteQuery(String sql, Object[] params, int outParamPos,
int SqlType) {
Object object = null;
connnection = this.getConnection();
try {
callableStatement = connnection.prepareCall(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
callableStatement.setObject(i + 1, params[i]);
}
}
callableStatement.registerOutParameter(outParamPos, SqlType);
callableStatement.execute();
object = callableStatement.getObject(outParamPos);
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}
return object;
}
/**
* 关闭所有资源
*/
private void closeAll() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (callableStatement != null) {
try {
callableStatement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (connnection != null) {
try {
connnection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}