标签:.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