数据库工具类(scala语言)

C3P0工具类

package com.air.antispider.stream.common.util.database;

import com.air.antispider.stream.common.util.jedis.PropertiesUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
*/
public class c3p0Util {
public static ComboPooledDataSource cpds = null;

/**
 * 创建c3p0连接池
 */
private static void createConnectionPool(){
    Properties prop = PropertiesUtil.getProperties("c3p0.properties");
    cpds = new ComboPooledDataSource(true);
    try {
        cpds.setJdbcUrl(prop.getProperty("jdbcUrl"));
        cpds.setDriverClass(prop.getProperty("driverClass"));
        cpds.setUser(prop.getProperty("username"));
        cpds.setPassword(prop.getProperty("password"));
        cpds.setMinPoolSize(new Integer(prop.getProperty("minPoolSize")));
        cpds.setMaxPoolSize(new Integer(prop.getProperty("maxPoolSize")));
        cpds.setInitialPoolSize(new Integer(prop.getProperty("initialPoolSize")));
        cpds.setMaxIdleTime(new Integer(prop.getProperty("maxIdleTime")));
        cpds.setAcquireIncrement(new Integer(prop.getProperty("acquireIncrement")));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static synchronized void connectionPoolInit(){
    if(cpds == null){
        createConnectionPool();
    }
}

/**
 * 获取连接池
 * @return 返回一个连接
 */
public static Connection getConnection(){
    try{
        if(cpds == null){
            connectionPoolInit();
        }
        Connection conn = cpds.getConnection();
        return conn;
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

/**
 * 关闭连接
 * @param conn Connection
 * @param pst PreparedStatement
 * @param rs ResultSet
 */
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
    if(rs!=null){
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if(pst!=null){
        try {
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if(conn!=null){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static void close(Connection conn,PreparedStatement pst){
    if(pst!=null){
        try {
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if(conn!=null){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static void close(Connection conn){
    if(conn!=null){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

查询工具类

package com.air.antispider.stream.common.util.database

import scala.collection.mutable.ArrayBuffer

/**

  • 工具类的作用:通过sql和value读取数据库的某一个字段
  • Created by wangsenfeng on 2019/1/4.
    */
    object QueryDB {
    def queryData(sql: String, field: String): ArrayBuffer[String] = {
    //创建ab,用来封装数据
    val arr = new ArrayBufferString
    //获取连接
    val conn = c3p0Util.getConnection
    //执行sql语句
    val ps = conn.prepareStatement(sql)
    val rs = ps.executeQuery()
    //封装数据
    while (rs.next()){
    arr.+=(rs.getString(field))
    }
    c3p0Util.close(conn,ps,rs)
    //返回结果
    arr
    }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值