package utils; import org.apache.log4j.Logger; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class DBUtil4Mysql { String JDBC_DRIVER="com.mysql.jdbc.Driver"; String DB_URL; String user; String passwd; static Logger log=LogUtils.getLogger(); /** * 初始化参数 * @param dbUrl * @param port * @param dbName * @param user * @param passwd */ public void init(String dbUrl, String port, String dbName, String user, String passwd){ this.DB_URL=String.format("jdbc:mysql://%s:%s/%s",dbUrl,port,dbName); this.user=user; this.passwd=passwd; } /** * 获取Map<String,Object>的结果 * @param querySql * @return */ public Map<String,Object> queryMapResult(String querySql){ try { Class.forName(JDBC_DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); log.error("未找到数据库驱动,数据库驱动注册失败"); } Connection connection = null; Statement statement=null; ResultSet resultSet=null; Map<String,Object> map=null; try { connection = DriverManager.getConnection(DB_URL,user,passwd); statement=connection.createStatement(); resultSet = statement.executeQuery(querySql); ResultSetMetaData metaData= resultSet.getMetaData(); int count=metaData.getColumnCount(); while (resultSet.next()){ map=new HashMap<String, Object>(); for(int i=1;i<=count;i++){ map.put(metaData.getColumnName(i),resultSet.getObject(i)); } } } catch (SQLException e) { e.printStackTrace(); log.error("获取数据失败"); } close(resultSet,statement,connection); return map; } /** * 获取List<Map<String,Object>>的结果 * @param querySql * @return */ public List<Map<String,Object>> queryListMapResult(String querySql){ try { Class.forName(JDBC_DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); log.error("未找到数据库驱动,数据库驱动注册失败"); } Connection connection = null; Statement statement=null; ResultSet resultSet=null; List<Map<String,Object>> list=null; try { connection = DriverManager.getConnection(DB_URL,user,passwd); statement=connection.createStatement(); resultSet = statement.executeQuery(querySql); ResultSetMetaData metaData= resultSet.getMetaData(); int count=metaData.getColumnCount(); list=new ArrayList<Map<String, Object>>(); while (resultSet.next()){ Map<String,Object> map=new HashMap<String, Object>();; for(int i=1;i<=count;i++){ map.put(metaData.getColumnName(i),resultSet.getObject(i)); } list.add(map); } } catch (SQLException e) { e.printStackTrace(); log.error("获取数据失败"); } close(resultSet,statement,connection); return list; } /** * 关闭各连接 * @param resultSet * @param statement * @param connection */ private void close(ResultSet resultSet,Statement statement,Connection connection){ try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); log.error("resultSet 关闭失败"); } finally { try { if (statement != null) { statement.close(); } } catch (SQLException e) { e.printStackTrace(); log.error("statement 关闭失败"); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); log.error("connection 关闭失败"); } } } } }