判断数据库连接
public static void main(String[] args) {
try {
Connection conn = JdbcUtils.getConnection();
System.out.println("数据库连接成功");
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("数据库连接失败");
}
}
配备Druid连接池 JdbcUtils工具类
#mysql8.0 database settings
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://ip:port/hvk_l?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone =Asia/Shanghai
username=root
password=qwerty123
initialSize=5
maxActive=10
maxWait=3000
minIdle=3
public class JdbcUtils {
private static DataSource ds;
static{
try {
Properties pro = new Properties();
pro.load(JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Statement stmt,Connection conn){
close(null, stmt, conn);
}
public static void close(ResultSet rs, Statement stmt, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return ds;
}
}