初学者在Mysql8.0连接时的几个常见基本问题

  最近在做一些java web整合时使用的最新版Mysql8.0.3,发现Mysql连接中的几个问题,总结如下:

复制代码
package db;//自定义包名
import java.sql.*;

public class test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String user="root";
        String password="123456";
        String url="jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=GMT%2B8";//mydb为Mysql数据库中创建的数据库实例名
        String driver="com.mysql.cj.jdbc.Driver";    
        
        String tableName="studinfo";//studinfo为数据库mydb中的表名
        String sqlstr;
        Connection con=null;
        Statement stmt=null;
        ResultSet rs=null;
        
        try
        {
            Class.forName(driver);
            con=DriverManager.getConnection(url, user, password);
            stmt=con.createStatement();        
            
            sqlstr="insert into "+tableName+ " value('1111','honey',21)";//into的后面和value前面一定要添加一个空格value后面与左括号之间有无空格无所谓
            stmt.executeUpdate(sqlstr);
            
            sqlstr="select * from "+ tableName;
            rs=stmt.executeQuery(sqlstr);
            
            ResultSetMetaData rsmd=rs.getMetaData();
            int j=0;
            j=rsmd.getColumnCount();
            for(int k=0;k<j;k++)
            {
                System.out.print(rsmd.getColumnName(k+1));
                System.out.print("\t");
            }
            
            System.out.println();
            
            while(rs.next())
            {
                for(int i=0;i<j;i++)
                {
                    System.out.print(rs.getString(i+1));
                    System.out.print("\t");
                }        
                System.out.println();
            }                
        }
        catch(ClassNotFoundException e1)
        {
            System.out.print("数据库驱动不存在!");
            System.out.print(e1.toString());
        }
        catch(SQLException e2)
        {
            System.out.print("数据库存在异常!");
            System.out.print(e2.toString());
        }
        finally
        {
            try
            {
                if(rs!=null)
                    rs.close();
                if(stmt!=null)
                    stmt.close();
                if(con!=null)
                    con.close();    
            }
            catch(SQLException e)
            {
                System.out.print(e.toString());
            }
        } 
    }
}
复制代码

 

  常见错误提示1:

  以上配置中,url中如果driver没有添加cj,则会在连接的时候出现以下错误提示:

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.

  解决办法:根据提示,很显然这种driver配置方式在此版本中已经被废弃,因此需要将driverClass配置为:com.mysql.cj.jdbc.Driver。

  常见错误提示2:

  以上配置中,url中如果没有设置useSSL=false,则会在连接的时候出现以下错误提示:

1
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 <br>SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate<br> 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<br> verification.

  解决办法:在连接字符串中添加?useSSL=false

  常见错误提示3:

  以上配置中,url中如果没有设置serverTimezone=UTC,则会在连接的时候出现以下错误提示:

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.

   解决办法:在连接字符串中添加&amp;serverTimezone=GMT%2B8

  常见错误提示4

  错误提示:对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。

  解决办法:在 xml 中 &符号是作为实体字符形式存在的。故需要在连接字符串中的将ServerTime前面的&符号修改为&amp;,参见上面的代码。

 

  注意:以上参数最重要的是serverTimezone=GMT%2B8不可缺少,其它参数都是可选项,且在Java的属性文件(*.propertise)中URL中的&符号不能写成&amp;。至于编码方式characterEncoding=utf-8或utf8均可。

posted @   rainbow70626  阅读(11108)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示