Jdbc工具类

/**
*JDBC工具类
/
public class JdbcUtil
{
    private static Logger logger;
    private Connection connection;
    private String driverName;
    private String url;
    private String username;
    private String password;
    
    static {
        JdbcUtil.logger = LoggerFactory.getLogger((Class)JdbcUtil.class);
    }
    
    public JdbcUtil( String driverName,  String url,  String username,  String password) {
        this.driverName = driverName;
        this.url = url;
        this.username = username;
        this.password = password;
        try {
            Class.forName(driverName);
            this.connection = DriverManager.getConnection(url, username, password);
        }
        catch (Exception ex)
        {
            logger.error(e,"");
        }
    }
    
    // 通过外传参数构建实例
    public static JdbcUtil getInstance( String driverName,  String url,  String username,  String password) {
        return new JdbcUtil(driverName, url, username, password);
    }
    
    // 从配置文件构建实例
    public static JdbcUtil getInstance() {
         Properties pro = new Properties();
        try {
            pro.load(new FileInputStream("jdbc.properties"));
            return new JdbcUtil(pro.getProperty("jdbc.driverClassName"), pro.getProperty("jdbc.url"), pro.getProperty("jdbc.username"), pro.getProperty("jdbc.password"));
        }
        catch (Exception e) {
            logger.error(e,"");
            return null;
        }
    }
    
    // 获得Connection对象
    public Connection getConnection() {
        return this.connection;
    }
    
    // 重置Connection对象
    public Connection resetConnection() {
        this.close();
        try {
            Class.forName(this.driverName);
            this.connection = DriverManager.getConnection(this.url, this.username, this.password);
        }
        catch (Exception e) {
            logger.error(e,"");
        }
        return this.connection;
    }
    
   
    
    public List<HashMap> query( String sql) {
        Statement stmt = null;
        ResultSet rs = null;
         List<HashMap> lists = new ArrayList<HashMap>();
        try {
            stmt = this.connection.createStatement();
            rs = stmt.executeQuery(sql);
             ResultSetMetaData data = rs.getMetaData();
            while (rs.next()) {
                 HashMap jb = new HashMap();
                for (int count = data.getColumnCount(), i = 1; i <= count; ++i) {
                     String columnName = data.getColumnName(i);
                     Object value = rs.getObject(i);
                    if (value instanceof Timestamp) {
                        jb.put(columnName, rs.getTimestamp(columnName));
                    }
                    else if (value != null && "class [B".equals(new StringBuilder().append(value.getClass()).toString())) {
                         InputStream inputStram = rs.getBinaryStream(columnName);
                        jb.put(columnName, getByte(inputStram));
                    }
                    else if (value instanceof BigDecimal) {
                        jb.put(columnName, value);
                    }
                    else if (value == null) {
                        jb.put(columnName, null);
                    }
                    else if (value instanceof String) {
                        jb.put(columnName, value);
                    }
                    else {
                        String columnValue = rs.getString(i);
                        columnValue = ((columnValue == null) ? "" : columnValue);
                        jb.put(columnName, columnValue);
                    }
                }
                lists.add(jb);
            }
        }
        catch (SQLException e) {
            logger.error(e,"");
            return lists;
        }
        finally {
            StreamUtil.close(rs, stmt);
        }
        return lists;
    }
    
    public boolean executeSql( String sql) {
        Statement stmt = null;
        boolean flag = false;
        try {
            stmt = this.connection.createStatement();
            stmt.execute(sql);
            flag = true;
        }
        catch (SQLException e) {
             logger.error(e,"");
        }
        return flag;
    }
    
    public static byte[] getByte( InputStream value) {
         ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
         byte[] buff = new byte[100];
        int rc = 0;
        try {
            while ((rc = value.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
        }
        catch (IOException e) {
            logger.error(e,"");
        }
         byte[] in2b = swapStream.toByteArray();
        return in2b;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值