package com.budget.utils; 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; import java.util.ResourceBundle; public class JdbcUtil { static ResourceBundle resource; static{ resource = ResourceBundle.getBundle("jdbc"); //配置文件名:jdbc.properties } public static String getValueByKey(String key){ return resource.getString(key); } public static final String JDBC_DRIVER = JdbcUtil.getValueByKey("jdbc.driver");//配置文件属性名 public static final String JDBC_URL = JdbcUtil.getValueByKey("jdbc.url"); public static final String JDBC_USER = JdbcUtil.getValueByKey("jdbc.user"); public static final String JDBC_PASSWORD = JdbcUtil.getValueByKey("jdbc.password"); /** * 类加载时加载数据库驱动 */ static{ try { Class.forName(JDBC_DRIVER); } catch (ClassNotFoundException e) { System.out.println("驱动加载失败!"); e.printStackTrace(); } } /** * 获取数据库连接的方法 * @return 数据库连接对象conn */ public static Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD); System.out.println("获取连接成功!"); } catch (SQLException e) { System.out.println("获取连接失败!"); e.printStackTrace(); } return conn; } /** * 执行更新操作(插入、修改、删除) * @param sql 要执行的SQL语句 * @param params SQL语句预编译参数(如无可省略) * @return rows 影响的行数 */ public static int executeUpdate(String sql,Object...params){ Connection conn = getConnection(); PreparedStatement ps = null; int rows=0; try { ps = conn.prepareStatement(sql); if(params!=null&¶ms.length>0){ for(int i=0;i<params.length;i++){ ps.setObject(i+1,params[i]); } } rows = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ try { closeResource(conn, ps, null); } catch (SQLException e) { e.printStackTrace(); } } return rows; } /** * 执行查询操作 * @param sql 要执行的查询sql语句 * @param params SQL语句预编译参数(如无可省略) * @return list 结果集,每一条结果为所有查询的字段名和字段值为键值对的Map集合 */ public static List<Map<String, Object>> executeQuery(String sql,Object...params){ Connection conn = getConnection(); PreparedStatement ps=null; ResultSet set=null; List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { ps = conn.prepareStatement(sql); if(params!=null&¶ms.length>0){ for(int i=0;i<params.length;i++){ ps.setObject(i+1,params[i]); } } set = ps.executeQuery(); ResultSetMetaData rsmd = set.getMetaData(); int columnCount = rsmd.getColumnCount(); while(set!=null&&set.next()){ Map<String,Object> map = new HashMap<String,Object>(); for(int i=1;i<=columnCount;i++){ map.put(rsmd.getColumnName(i), set.getObject(i)); } list.add(map); } } catch (SQLException e) { e.printStackTrace(); }finally { try { closeResource(conn,ps,set); } catch (SQLException e) { e.printStackTrace(); } } return list; } /** * 释放资源 * @param conn Connection对象 * @param ps PreparedStatement对象 * @param rs ResultSet对象 * @throws SQLException */ private static void closeResource(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(conn!=null){ conn.close(); } } //测试连接 public static void main(String[] args){ Connection connection = JdbcUtil.getConnection(); System.out.println(connection); // ArrayList<Object> listValues = new ArrayList<Object>(); // String logsql="insert into inf_log(NAME,STATUS,STARTTIME,ENDTIME,ROWNUMS,MESSAGE) values (?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?)"; // JdbcUtil.executeUpdate(logsql.toString(),listValues.toArray()); } }