背景
学习数据库的使用,上次没有记录,现在都回忆不起来了,所以这次重新学的时候顺便记录下。
配置环境
win10
jdk11
idea
mysql8.0.13
DBCP连接使用
用配置文件目前我连接不来
jar包
mysql-connector-java-8.0.14
commons-pool2-2.6.0
commons-logging-1.2
commons-dbcp2-2.5.0
使用代码连接数据库
代码
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
BasicDataSource dataSource = new BasicDataSource();
/*mysql数据库的连接,参考我上篇文章*/
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/webdemo?useSSL=false&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("root");
测试
Connection conn = dataSource.getConnection();
String sql = "INSERT INTO category VALUES('ee','ee');";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
/*增删改:执行更新*/
System.out.println(preparedStatement.executeUpdate());
曾经报错
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory