package cn.com.DaoTier;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Jmlc on 2017/8/22.
*/
public class DBHelper {
private static final String DRIVENAME = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/Jsp_Servlet";
private static final String USER = "root";
private static final String PASSWORD = "root";
private int columnCount = 0;
private int affectedLine = 0;//受影响的行数
private Connection connection = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private ResultSetMetaData resultSetMetaData = null;
/**
* 加载驱动
*/
static {
try {
Class.forName(DRIVENAME);
System.out.println("驱动加载成功。");
} catch (Exception e) {
System.out.println("驱动加载失败:" + e.getMessage());
}
}
/**
* 连接数据库
*
* @return
*/
public Connection gainConnection() {
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("连接数据库成功。");
} catch (SQLException e) {
System.out.println("连接数据库失败!--" + e.getMessage());
}
return connection;
}
/**
* 查询(有参、无参)
*
* @param sql
* @param params
* @return
*/
public List<Object> gainQuery(String sql, Object[] params) {
List<Object> list = new ArrayList<>();
if (params != null) {
resultSet = gainResultSet(sql, params);
} else {
resultSet = gainResultSet(sql);
System.out.println(sql);
}
try {
while (resultSet.next()) {
//rs.get+数据库中对应的类型+(数据库中对应的列别名)
String username = resultSet.getString("username");
String password = resultSet.getString("password");
if (username == null && password == null) {
list.add("error");
return list;
} else {
list.add(username);
list.add(password);
System.out.println("结果集解析成功。");
return list;
}
}
} catch (SQLException e) {
System.out.println("结果集解析出错!--" + e.getMessage());
list.add("error");
} finally {
closeConnection();
}
return list;
}
/**
* 获取结果集(无参)
*
* @param sql
* @return
*/
private ResultSet gainResultSet(String sql) {
connection = this.gainConnection();
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
System.out.println("获取结果集(无参)成功。");
} catch (SQLException e) {
System.out.println("获取结果集(无参)失败!--" + e.getMessage());
}
return resultSet;
}
/**
* 获取结果集(有参)
*
* @param sql
* @param params
* @return
*/
private ResultSet gainResultSet(String sql, Object[] params) {
connection = this.gainConnection();
try {
preparedStatement = connection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
resultSet = preparedStatement.executeQuery();
System.out.println("获取结果集(有参)成功。");
} catch (SQLException e) {
System.out.println("获取结果集(有参)失败!--" + e.getMessage());
} finally {
closeConnection();
}
return resultSet;
}
/**
* 更新(无参)
*
* @param sql
*/
public int gainUpdate(String sql) {
connection = this.gainConnection();
try {
statement = connection.createStatement();
affectedLine = statement.executeUpdate(sql);
System.out.println("更新(无参)成功。");
} catch (SQLException e) {
System.out.println("更新(无参)失败!--" + e.getMessage());
} finally {
closeConnection();
}
return affectedLine;
}
/**
* 更新(有参)
*
* @param sql
* @param params
* @return
*/
public int gainUpdate(String sql, Object[] params) {
connection = this.gainConnection();
try {
preparedStatement = connection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
affectedLine = preparedStatement.executeUpdate();
System.out.println("更新(有参)成功。");
} catch (SQLException e) {
System.out.println("更新失败!--" + e.getMessage());
} finally {
closeConnection();
}
return affectedLine;
}
private void closeConnection() {
if (resultSet != null) {
try {
resultSet.close();
System.out.println("资源关闭成功。");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (statement != null) {
try {
statement.close();
System.out.println("资源关闭成功。");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
System.out.println("资源关闭成功。");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (connection != null) {
try {
connection.close();
System.out.println("资源关闭成功。");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}