Java语言的Dao层设计(三)—— 数据库的操作工具类(MySQL8.0和JDBC)

在不使用框架的情况下,为了解决使用JDBC操作数据库时PrepareStatement命令繁琐的问题,本文提出了利用Java的可变长度参数(Object... args)和foreach循环的方法。通过创建数据库操作工具类,结合IControlDao接口和foreach循环设置参数,提高了代码的简洁性和安全性。同时提醒在使用该工具类时需注意参数匹配,避免未完全赋值的情况。
摘要由CSDN通过智能技术生成

数据库的操作工具类

在没有使用框架之前,我们在Dao层中我们经常会使用JDBC来操作数据库中的数据。通常我们为了安全会使用prepareStatement这个存储过程来为数据库操作语句赋值,但在面对一长串的PrepareStatement.SetXXX()命令,有时确实觉得有点儿重复做工的意味。

那么有没有什么办法解决这个问题呢?

其实是有的,使用java语言的可变长度参数(Object… args)和foreach循环可以解决这个问题。
数据库的信息类,包括数据库的名称、用户名、密码以及连接对象,这个类的信息也可以从文件当中获取,以获得更好的安全性。

package dao.baseDao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class UtilConnection implements IDao {
   
    /**
     * @program: Dao层
     * @description: 获取来自MySQL8.0的connection
     * @author: niceSiri
     * @create: 2020-07-05 20:29
     */
    private final String DBName = "shopweb";//连接的数据库
    private final String URL = "jdbc:mysql://localhost:3306/" + this.DBName + "?useSSL=false&serverTimezone=UTC";
    private final String userName = "siri";//数据库用户名
    private final String password = "123456";//数据库密码
    //上述代码可以根据个人条件不同更换,也可以从文件当中获取
    private Connection connection = null;

    /**
     * 使用MySQL初始化连接
     * 使用上面的地址和用户名密码
     */
    public UtilConnection() {
   
        try {
   
            Class.forName("com.mysql.cj.jdbc.Driver");
            this.connection = DriverManager.getConnection(URL, userName, password);
        } catch (ClassNotFoundException | SQLException e) {
   
            e.printStackTrace();
        }
    }

    public Connection getConnection
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
环境: Eclipse Java EE IDE for Web Developers. Version: Oxygen.3a Release (4.7.3a) mysql-connector-java-8.0.11 MySQL Server 8.0 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 驱动的换了个名字 Class.forName("com.mysql.cj.jdbc.Driver"); Fri Jun 08 13:09:27 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 加上useSSL=true或false 参考地址 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=true", "root", "123456"); java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. 加上serverTimezone=GMT 参考地址 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=true&serverTimezone=GMT", "root", "123456")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值