python爬虫豆瓣读书top250+数据清洗+数据库+Java后端开发+Echarts数据可视化(三)

之前的博客已经写了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项目,建好后进行如下操作。

  1. 在项目的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>
  1. 在项目的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
  1. 在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在这里插入图片描述
在这里插入图片描述
这样问题就解决了!

今天复习的内容比较少,主要是对准备工作进行了复习和记录。明天开始进行正式工作啦!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值