package chat.util;
|
|
import java.sql.Connection;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
public class DBUtil {
|
|
private DruidDataSource dataSource;
|
|
public static boolean IsEmbedDB = false;
|
|
public DBUtil() {
|
dataSource = new DruidDataSource();
|
this.dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
this.dataSource.setUrl("jdbc:mysql://rm-uf65021a947ky5h453o.mysql.rds.aliyuncs.com:3306/medeasy_doctor?useSSL=false");
|
this.dataSource.setUsername("user");
|
this.dataSource.setPassword("U7t6XJnnHMii");
|
//设置连接池初始化的连接个数
|
//this.dataSource.setPoolPreparedStatements(true);
|
//this.dataSource.setMaxOpenPreparedStatements(25);
|
this.dataSource.setInitialSize(50);
|
this.dataSource.setMaxActive(100);
|
this.dataSource.setMinIdle(20);
|
}
|
|
public DruidDataSource getDataSource() {
|
return dataSource;
|
}
|
|
public Connection getConnection() {
|
Connection conn = null;
|
try {
|
conn = dataSource.getConnection();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
return conn;
|
}
|
|
public void close(Connection conn, PreparedStatement pst, ResultSet rs) {
|
if (rs != null) {
|
try {
|
rs.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
close(conn, pst);
|
}
|
|
public void close(Connection conn, PreparedStatement pst) {
|
if (pst != null) {
|
try {
|
pst.close();
|
} catch(SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
close(conn);
|
}
|
|
public void close(Connection conn) {
|
if (conn != null) {
|
try {
|
conn.close();
|
} catch(SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public void commit(Connection conn) {
|
if (conn != null) {
|
try {
|
conn.commit();
|
} catch(SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public void rollback(Connection conn) {
|
if (conn != null) {
|
try {
|
conn.rollback();
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|