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() ; 
        }
    }

然后就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值