MySQLHelper

package hr.baple.util;

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.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class MysqlHelper {

 public static String driverName;
 public static String dbURL;
 public static String dbName;
 public static String dbPwd;
 public static Connection connection;

 private MysqlHelper() {

 }

 static {
  // 加载驱动
  try {
   driverName = "com.mysql.jdbc.Driver";
   dbURL = "jdbc:mysql://192.168.10.227:3306/test";
   dbName = "root";
   dbPwd = "******";
   Class.forName(driverName);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
 }

 //
 private static Connection getConnection() throws SQLException {
  if (connection == null || connection.isClosed()) {
   connection = DriverManager.getConnection(dbURL, dbName, dbPwd);
  }
  return connection;
 }

 private static void close(Connection connection, Statement statement) {
  try {
   if (connection != null) {
    connection.close();
   }
   if (statement != null) {
    statement.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static void close(ResultSet rs) {
  try {
   if (rs != null) {
    rs.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static List<Map<String, Object>> convert(ResultSet rs)
   throws SQLException {
  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  if (rs != null && !rs.isClosed()) {
   ResultSetMetaData meta = rs.getMetaData();
   int colCount = meta.getColumnCount();
   while (rs.next()) {
    Map<String, Object> map = new HashMap<String, Object>();
    for (int i = 1; i <= colCount; i++) {
     String key = meta.getColumnName(i);
     Object value = rs.getObject(i);
     map.put(key, value);
    }
    list.add(map);

   }
  }
  return list;
 }

 private static void AttachParameters(PreparedStatement statement,
   Object... values) throws Exception {
  if (values != null) {
   for (int i = 0; i < values.length; i++) {
    statement.setObject(i + 1, values[i]);
   }
  }
 }

 //

 public static int ExecuteNonQuery(String sql, Object... parameterValues)
   throws SQLException, Exception {
  PreparedStatement statement = null;
  int result = 0;
  try {
   connection = getConnection();
   statement = connection.prepareStatement(sql);
   AttachParameters(statement, parameterValues);
   result = statement.executeUpdate();
   statement.clearParameters();
  } catch (Exception e) {
   close(connection, statement);
   throw e;
  }
  return result;
 }

 public static Object ExecuteScalar(String sql, Object... parameterValues)
   throws Exception {
  ResultSet rs = null;
  Object value = null;
  try {
   rs = ExecuteResultSet(sql, parameterValues);
   if (rs != null) {
    rs.next();
    value = rs.getObject(1);
   }
  } finally {
   close(rs);
  }

  return value;
 }

 public static ResultSet ExecuteResultSet(String sql,
   Object... parameterValues) throws Exception {
  PreparedStatement statement = null;
  ResultSet rs = null;
  try {
   connection = getConnection();
   statement = connection.prepareStatement(sql);
   AttachParameters(statement, parameterValues);
   rs = statement.executeQuery();
   statement.clearParameters();
  } catch (Exception e) {
   close(connection, statement);
   throw e;
  }
  return rs;
 }

 public static List<Map<String, Object>> ExecuteList(String sql,
   Object... parameterValues) throws Exception {
  ResultSet rs = null;
  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  try {
   rs = ExecuteResultSet(sql, parameterValues);
   list = convert(rs);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   close(rs);
  }
  return list;
 }

 public static ArrayList ExecuteReader(String sql, Object... parameterValues)
   throws Exception {
  ResultSet rs = null;
  ArrayList list = new ArrayList();
  try {
   rs = ExecuteResultSet(sql, parameterValues);
   ResultSetMetaData rsmd = rs.getMetaData();
   int column = rsmd.getColumnCount();
   while (rs.next()) {
    Object[] obj = new Object[column];//
    for (int i = 1; i <= column; i++) {
     obj[i - 1] = rs.getObject(i);
    }
    list.add(obj);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   close(rs);
  }
  return list;
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值