文章目录
jdbc德鲁伊连接池代码:
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static DruidDataSource dataSource;
static {
try {
Properties properties = new Properties();
// 读取 jdbc.properties属性配置文件
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 从流中加载数据
properties.load(inputStream);
// 创建 数据库连接 池
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
System.out.println(dataSource.getConnection());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
新版的intellij IDEA中javaweb项目结构是这样的:
可能的出错点1:jdbc的配置文件要放在resources文件夹下
可能的出错点2:jdbc连接mysql8以上版本时的url处需要修改
username=root//用户名
password=密码
url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
driverClassName=com.mysql.cj.jdbc.Driver
initialSize=5
maxActive=10