初学者在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,则会在连接的时候出现以下错误提示:
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=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.
解决办法:在连接字符串中添加&serverTimezone=GMT%2B8
常见错误提示4:
错误提示:对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。
解决办法:在 xml 中 &符号是作为实体字符形式存在的。故需要在连接字符串中的将ServerTime前面的&符号修改为&,参见上面的代码。
注意:以上参数最重要的是serverTimezone=GMT%2B8不可缺少,其它参数都是可选项,且在Java的属性文件(*.propertise)中URL中的&符号不能写成&。至于编码方式characterEncoding=utf-8或utf8均可。