zoukankan      html  css  js  c++  java
  • 运行ConnectionDemo时遇到的问题及解决方案

    20175227张雪莹 2018-2019-2 《Java程序设计》

    运行ConnectionDemo时遇到的问题及解决方案

    老师博客上提供确认数据库连接的代码

    import static java.lang.System.out;
    import java.sql.*;
    
    public class ConnectionDemo {
        public static void main(String[] args)
                                  throws ClassNotFoundException, SQLException {
            Class.forName("com.mysql.jdbc.Driver");
            String jdbcUrl = "jdbc:mysql://localhost:3306/demo";
            String user = "root";
            String passwd = "";
            try(Connection conn = 
                    DriverManager.getConnection(jdbcUrl, user, passwd)) {
                out.printf("已%s数据库连接%n", 
                        conn.isClosed() ? "关闭" : "打开");
            } 
        }
    }
    
    

    问题

    • 问题1:首次运行程序时,出现如图问题:

    即驱动包已更新过:com.mysql.jdbc.Drivermysql-connector-java 5中的,而com.mysql.cj.jdbc.Drivermysql-connector-java 6中的

    • 问题2:将问题2解决后运行代码时,又出现时区错误提示,如图:

    这其实是由于在安装的时候没有选择使用位置,mysql默认使用美国时间,所以得改成我们东八区的时间

    解决方案

    • 问题1解决方案:将代码第七行com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver

    • 问题2解决方案:将代码第八行jdbc:mysql://localhost:3306/demo改为jdbc:mysql://localhost:3306/demo?serverTimezone=UTC

    代码托管

  • 相关阅读:
    [loj6271]生成树求和
    [cf1209E]Rotate Columns
    [cf1491H]Yuezheng Ling and Dynamic Tree
    [atARC064F]Rotated Palindromes
    [cf1491G]Switch and Flip
    [cf1491F]Magnets
    [atARC063F]Snuke's Coloring 2
    [atARC062F]Painting Graphs with AtCoDeer
    [atARC061F]Card Game for Three
    [atARC112E]Rvom and Rsrev
  • 原文地址:https://www.cnblogs.com/zxy20175227/p/10778169.html
Copyright © 2011-2022 走看看