JavaWeb项目的数据库访问简单基础类
package com.etc.dao; import java.lang.reflect.Field; 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.List; import com.etc.util.ConfigManager; /** 数据库访问基础类 */ public class BaseDao { String driver = ConfigManager.getInstance().getString("driver"); String url = ConfigManager.getInstance().getString("url"); String user = ConfigManager.getInstance().getString("user"); String password = ConfigManager.getInstance().getString("password"); /** 获得数据库连接 */ public Connection getConnection() { Connection conn = null;// 数据库连接对象 if (conn == null) { try { Class.forName(driver);// 加载驱动 conn = DriverManager.getConnection(url, user, password);// 建立连接 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } /** 关闭资源 */ public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** 增删改通用方法 */ public int executeUpdate(String sql, Object... args) { if (sql == null || args == null) { return -2;// 参数为空 } int result = -1;// 增删改失败 PreparedStatement ps = null;// 预编译对象 Connection conn = this.getConnection(); try { ps = conn.prepareStatement(sql); if (args != null) { for (int i = 0; i < args.length; i++) { ps.setObject(i + 1, args[i]); } } result = ps.executeUpdate();// 执行增删改 } catch (SQLException e) { e.printStackTrace(); } finally { this.closeAll(null, ps, conn); } return result; } /**加载T对象 */ public <T> T getResult(Class<T> clazz, String sql, Object... args) { if (sql == null || clazz == null) { return null; } T resultObject = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = this.getConnection(); ps = conn.prepareStatement(sql); if (args != null) { for (int i = 0; i < args.length; i++) { ps.setObject(i + 1, args[i]); } } rs = ps.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); int couLength = metaData.getColumnCount(); while (rs.next()) { resultObject = clazz.newInstance(); for (int i = 0; i < couLength; i++) { String colName = metaData.getColumnName(i + 1); Object colValue = rs.getObject(colName); if (colValue == null) { colValue = ""; } Field field = clazz.getDeclaredField(colName); field.setAccessible(true); field.set(resultObject, colValue); } } } catch (Exception e) { e.printStackTrace(); } finally { this.closeAll(rs, ps, conn); } return resultObject; } /** 查询多条记录 */ public <T> List<T> listResult(Class<T> clazz, String sql, Object... args) { if (sql == null || clazz == null) { return null; } List<T> list = new ArrayList<T>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = this.getConnection(); ps = conn.prepareStatement(sql); if (args != null) { for (int i = 0; i < args.length; i++) { ps.setObject(i + 1, args[i]); } } rs = ps.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); int colLength = metaData.getColumnCount(); while (rs.next()) { T resultObject = clazz.newInstance(); for (int i = 0; i < colLength; i++) { String colName = metaData.getColumnName(i + 1); Object colValue = rs.getObject(colName); if (colValue == null) { colValue = ""; } Field field = clazz.getDeclaredField(colName); field.setAccessible(true); field.set(resultObject, colValue); } list.add(resultObject); } } catch (Exception e) { e.printStackTrace(); } finally { this.closeAll(rs, ps, conn); } return list; } }
本博客文章未经许可,禁止转载和商业用途!
如有疑问,请联系: 2083967667@qq.com