• JavaWeb —— JDBC Driver驱动及连接问题


    一、JDBC驱动版本及连接

    com.mysql.jdbc.Driver 连接MySQL 5版本  是用 mysql-connector-java-5.***.jar

     1 //MySQL5.0连接
     2  //注册数据库驱动
     3  Class.forName("com.mysql.jdbc.Driver");
     4  //通过DriverManager获取数据库连接
     5  String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
     6  String user = "root";
     7  String password = "******";
     8  con = DriverManager.getConnection(url, user, password);
     9  //通过Connection对象获取Statement对象
    10  st = con.createStatement();
    11  //使用Statement对象执行SQL语句
    12  String sql = "select * from goods";
    13  rs = st.executeQuery(sql);

    com.mysql.cj.jdbc.Driver 连接MYSQL 6及以上版本  是用mysql-connector-java-6.***.jar 或 6以上的jar包

     1 //MySQL8.0连接
     2 //注册数据库驱动
     3 Class.forName("com.mysql.cj.jdbc.Driver");
     4 /通过DriverManager获取数据库连接
     5 String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";
     6 String user = "root";
     7 String password = "******";
     8 con = DriverManager.getConnection(url, user, password);
     9 //通过Connection对象获取Statement对象
    10 st = con.createStatement();
    11 //使用Statement对象执行SQL语句
    12 String sql = "select * from goods";
    13 rs = st.executeQuery(sql);

    二.、MySQL连接问题

    1 String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true";

    若不是用SSL连接,通过设置useSSL=false禁用;若需要是用SSL连接,为服务器证书验证提供信任库,设置useSSL=true

    serverTimezone时区设置,在中国可以设置serverTimezone=Shanghai或serverTimezone=Hongkong不设置会出现错误

    useUnicode=true&characterEncoding=utf-8 指定字符的编码、解码格式

    autoReconnect=true 数据库连接异常中断时,自动重新连接

  • 相关阅读:
    Flutter DraggableScrollableSheet 可滚动对象的容器
    Flutter 避免阻塞ui线程
    Android Kotlin 数据驱动模板
    ng mock服务器数据
    rxjs 常用的subject
    Flutter 在同一页面显示List和Grid
    dart2native 使用Dart 在macOS,Windows或Linux上创建命令行工具
    Flutter 创建透明的路由页面
    ng 发布组件库
    js实现单张或多张图片持续无缝滚动
  • 原文地址:https://www.cnblogs.com/chaunceyji/p/14853345.html
Copyright © 2020-2023  润新知