之前的博客已经写了python爬取豆瓣读书top250的相关信息和清洗数据,以及将数据导入数据库并创建相应的数据表。接下来进行项目准备工作。
如果有没看懂的或是不了解上一部分说的是什么内容的,请看
https://blog.csdn.net/qq_45804925/article/details/112848887
https://blog.csdn.net/qq_45804925/article/details/112898570
现在开始具体内容的复习:
1. 项目准备工作
1.1 Eclipse关联本地tomcat
1.2 Eclipse关联本地maven
以上两个步骤比较普通、常见,可以自行去百度一下
1.3 数据库连接池配置
在进行这些之前首先要创建maven项目,建好后进行如下操作。
- 在项目的pom.xml中添加对mysql连接jar包和dbcp连接池的依赖:
<!-- 配置本项目中依赖的jar包 -->
<dependencies>
<!-- mysql连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池jar包 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
- 在项目的src/main/resources下新建一个文件,名为jdbc.properties,并在其中添加如下配置:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/doubanbook?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
initSize=3
maxSize=3
使用mysql8.x的同学,适用下面的配置:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/doubanbook?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username=root
password=root
initSize=3
maxSize=3
- 在cn.geo.doubanbook.util包下创建DBUtils.java文件
public class DBUtils {
private static BasicDataSource dataSource;
static {
// 创建数据库连接池对象
dataSource = new BasicDataSource();
// 读取配置文件中的配置
Properties prop = new Properties();
InputStream ips =
DBUtils.class.getClassLoader()
.getResourceAsStream("jdbc.properties");
try {
prop.load(ips);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
String initSize = prop.getProperty("initSize");
String maxSize = prop.getProperty("maxSize");
// 对数据库连接池进行设置
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setInitialSize(Integer.parseInt(initSize));
dataSource.setMaxActive(Integer.parseInt(maxSize));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取连接池中的一个空闲连接
* @return 连接对象
* @throws SQLException
*/
public static Connection getConn()
throws SQLException {
return dataSource.getConnection();
}
}
以上几个操作截图如下:
在上图pom.xml中出现了如下错误:
Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document 'https://maven.apache.org/xsd/maven-4.0.0.xsd', because 1) could not find the document;
2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
- cvc-elt.1: Cannot find the declaration of element 'project'.
如下图所示:
解决办法如下:
右键项目——>Maven——>Update Projects——>勾选上Force Update of Snapchat/Releases
这样问题就解决了!
今天复习的内容比较少,主要是对准备工作进行了复习和记录。明天开始进行正式工作啦!