JDBCUtil操作数据库

一、JDBCUtil类

//需要导入commons-dbcp的jar包

package com.util;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;

public class JDBCUtils {
//    private static String driverName;
//    private static String url;
//    private static String user;
//    private static String password;
    private static BasicDataSource bds;
    static {
        try {
//            InputStream in=JDBCUtils.class.getClassLoader().getResourceAsStream("dbconnection.properties");
//            Properties properties=new Properties();
//            properties.load(in);
            Properties properties=new Properties();
            properties.load(new FileInputStream("src/main/resources/dbconnection.properties"));
            bds=new BasicDataSource();
            bds.setDriverClassName(properties.getProperty("driverClassName"));
            bds.setUrl(properties.getProperty("url"));
            bds.setUsername(properties.getProperty("username"));
            bds.setPassword(properties.getProperty("password"));
            bds.setMaxActive(Integer.parseInt(properties.getProperty("maxActive")));;
            bds.setMaxWait(Integer.parseInt(properties.getProperty("maxWait")));
            bds.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    /**
     * 
     * @return 获取连接
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
//        return DriverManager.getConnection(url, user, password);
        return bds.getConnection();
    }
    /**
     * 关闭连接
     */
    public static void releaseResources(Connection conn) {
        try {
            if(conn!=null) {
                conn.setAutoCommit(true);
                conn.close();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}

二、properties数据库配置文件

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@dx.abc.com:152:orcl
username=NC
password=2016
#最大连接数据库连接数,设 0 为没有限制
maxActive=1
#最大等待毫秒数, 单位为 ms, 超过时间会出错误信息
maxWait=5000
#最大等待连接中的数量,设 0 为没有限制
maxIdle=5 

三、调用JDBCUtils获取数据库连接

public void jdbcUtils() throws SQLException {
        Statement sta=null;
        ResultSet rs=null;
        Connection conn=null;
        try {
            conn=JDBCUtils.getConnection();
            sta=conn.createStatement();
            rs=sta.executeQuery("select * from tm_user tu where tu.real_name='姚景明'");
            if(rs.next()) {
                logger.info("USER_NAME:"+rs.getString("USER_NAME")+"/REAL_NAME:"+rs.getString("REAL_NAME"));
            }
        } catch (Exception e) {
            // TODO: handle exception
            logger.info(e);
        }finally {
            if(rs!=null) rs.close();
            if(conn!=null) JDBCUtils.releaseResources(conn);
            if(sta!=null) sta.close();
            
        }
    } 

猜你喜欢

转载自blog.csdn.net/qq_37924385/article/details/82896646