JAVA 代码连接 MySQL 注意事项

/**
 * <dependency>
 *     <groupId>mysql</groupId>
 *     <artifactId>mysql-connector-java</artifactId>
 *     <version>8.0.18</version>
 * </dependency>
 */
/**
 * SELECT @@version;
 * 8.0.16
 */
public class JDBCTest {
    //java.sql.Connection
    public static Connection getConnection() {
        //jdbc:mysql://127.0.01:6033/mysql?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
        String url = "jdbc:mysql://127.0.01:3306/mysql";
        String user = "root";
        String password = "123456";
        String className = "com.mysql.cj.jdbc.Driver";
        try {
            Class.forName(className);
            return DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    @Test
    public void testGetConnection() {
        Connection connection = getConnection();
        Assert.assertNotNull(connection);
    }
}

1 不同版本url有些不同

1.1 低版本的写法

jdbc.url = jdbc:mysql://localhost:3306/dbName
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.username = root
jdbc.password = 123456

 1.2 高版本的写法

jdbc.url = jdbc:mysql://localhost:3306/dbName?serverTimezone = UTC
jdbc.driverClass = com.mysql.cj.jdbc.Driver
jdbc.username = root
jdbc.password = 123456

1.3 都写com.mysql.jdbc.Driver 也可以

其实写com.mysql.jdbc.Driver也是没有问题的,只是多了一个警告

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.

package com.mysql.jdbc;
import java.sql.SQLException;
public class Driver extends com.mysql.cj.jdbc.Driver {
    public Driver() throws SQLException {}
    static {
        System.err.println("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.");
    }
}
package com.mysql.cj.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    public Driver() throws SQLException {
    }
    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}

2 jdbc.url 后面可以跟的参数

jdbc.url 后面可以跟的参数
参数名作用示例
serverTimezone

JDBC连接MySQL6 com.mysql.cj.jdbc.Driver, 需要指定时区serverTimezone;在设定时区的时候,如果设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkonghttps://dev.mysql.com/doc/refman/8.0/en/time-zone-support.html

serverTimezone=Asia/Shanghai
characterEncoding

 

characterEncoding=UTF-8
useUnicode 

useUnicode=true&characterEncoding=UTF-8

useUnicode=true&characterEncoding=utf8

useSSL useSSL=false
allowPublicKeyRetrieval allowPublicKeyRetrieval=true
  zeroDateTimeBehavior=convertToNull
tinyInt1isBit tinyInt1isBit=false
jdbc:mysql://ip:port/dbName?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8

3 xml 和 properties文件写法的不同

xml中回将 “&” 当成了特殊符号,我们可以把 “&” 转义一下即可(&amp; 就是  & 的意思)

<!--创建数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="root"/>
    <property name="password" value="Password"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/DatabaseName?serverTimezone=UTC&amp;characterEncoding=utf-8"/>
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
</bean>

4 整合数据库连接池 

jdbc.properties

jdbc.url = jdbc:mysql://localhost:3306/dbName?serverTimezone=UTC&characterEncoding=utf-8
jdbc.driverClass = com.mysql.cj.jdbc.Driver
jdbc.username = root
jdbc.password = 123456
<!---读取jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driverClass}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.4</version>
</dependency>

 下面这个c3p0是个废物,不要选择这个

<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值