码迷,mamicode.com
首页 > 其他好文 > 详细

阿里云链接池

时间:2019-04-03 16:32:27      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:.sql   connect   rgs   statement   []   rac   new   dstat   res   

JDBCUtils工具类

package com.zdy.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCutils {
private static DataSource ds;
static {
try {
Properties ps = new Properties();
ps.load(JDBCutils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(ps);
} catch (Exception e) {
e.printStackTrace();
}
}

public static Connection getConnection() throws SQLException {
return ds.getConnection();
}

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

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

public static DataSource getData() {
return ds;
}
}
测试类
package com.zdy.jdbc;

import com.zdy.utils.JDBCutils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class druidDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pre = null;
try {
conn = JDBCutils.getConnection();
pre = conn.prepareStatement("insert into emp values(1017,‘张三‘,2,1005,‘2019-04-03‘,5000,100,30)");
pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCutils.close(pre, conn);
}
}
}

阿里云链接池

标签:.sql   connect   rgs   statement   []   rac   new   dstat   res   

原文地址:https://www.cnblogs.com/zdy-123/p/10649630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!