mysql-connector6.0.6 jdbc及dataSource学习
最近在研究springmvc4.3.10+mybatis3.4.4在配置的dataSource的时候老是报错,顺手整理一下遇见的几个异常;
1,java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
导致原因:是因为mysql的版本与mysql-connector6.0.6不兼容;
解决方法一:将mysql-connector更换到5.x.x版本;
解决方法二:将mysql升级到最细稳定版本;
由于本人是学习用所以这里采用的是第二种方案,将mysql_5.1升级到了mysql_5.7.19版本,方案一之前项目用了好几次就不说了;
2,警告: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.
警告代码如下:
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/wxq?characterEncoding=utf8&useUnicode=true&useSSL=false" ;
String user = "root" ;
String password = "ROOT" ;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
if(con != null ){
String sql = "SELECT @@VERSION as version";//查询mysql版本号
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("version"));
}
rs.close();
con.close();
System.out.println("success");
}
}catch(SQLException ex){
System.out.println("数据库连接失败!");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
ex.printStackTrace() ;
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这个警告意思是com.mysql.jdbc.Driver这个驱动已经过期,新驱动com.mysql.cj.jdbc.Driver,新驱动会自动注册
所以只要将驱动改成如下
Class.forName("com.mysql.cj.jdbc.Driver");//可以省略
整体如下:
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/wxq?characterEncoding=utf8&useUnicode=true&useSSL=false" ;
String user = "root" ;
String password = "ROOT" ;
try{
Connection con = DriverManager.getConnection(url, user, password);
if(con != null ){
String sql = "SELECT @@VERSION as version";//查询mysql版本号
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("version"));
}
rs.close();
con.close();
System.out.println("success");
}
}catch(SQLException ex){
System.out.println("数据库连接失败!");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
ex.printStackTrace() ;
}
}
3.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.
这个异常的意思是:服务器的时区没有认证,你必须配置服务器的时区或者配置jdbc驱动的serverTimezone属性,百度一下上海的时区是Asia/Shanghai
修改代码如下:
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/wxq?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai" ;
String user = "root" ;
String password = "ROOT" ;
try{
System.out.println("url="+url);
Connection con = DriverManager.getConnection(url, user, password);
if(con != null ){
String sql = "SELECT @@VERSION as version";//查询mysql版本号
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("version"));
}
rs.close();
con.close();
System.out.println("success");
}
}catch(SQLException ex){
System.out.println("数据库连接失败!");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
ex.printStackTrace() ;
}
}
上一篇: 针灸和按摩 “灭”秋燥之火
下一篇: 中医要护齿 养肾是关键