美文网首页
Java连接MySQL问题记录

Java连接MySQL问题记录

作者: duyi324 | 来源:发表于2018-12-23 11:23 被阅读0次

最近学习Java服务器开发,需要用到MySQL数据库连接,在此记录一下遇到的问题及解决方案。


连接MySQL

按照RUNOOB.COM的教程,连接MySQL数据库,Java代码为:

public class MySQLDemo { 
    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";
 
    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "123456";
 
    public static void connectTest() {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
        
            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
        
            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);
        
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}

没想到直接运行马上马上就报错了。


错误一:Could not create connection to database server - java mysql connector

此问题是由于RUNOOB.COM提供的Jar包过期导致无法连接最新版MySQL数据库。

【解决方法】

下载最新版的Jar包
密码: igvz
并将代码中的驱动修改一下:

 // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  

错误二:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

解决了错误一后,接着来了错误二,报错代码为:

// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);

此问题是由于tomcat找不到MYSQL JAR包导致的。

【解决方法】

将上一步下载的mysql-connector-java-8.0.11.jar文件拷贝到tomcat根目录下的lib文件夹中。

错误三:java.sql.SQLException: 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=UTC即可解决。

static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?serverTimezone=UTC";

警告四:WARN: Establishing SSL connection without server's identity verification is not recommended.

输出控制台一直有这个警告,这是由于MySQL要求服务器有SSL验证。

【解决方法】

继续修改数据库连接字符串,添加useSSL=false参数:

static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";

相关文章

网友评论

      本文标题:Java连接MySQL问题记录

      本文链接:https://www.haomeiwen.com/subject/fwggkqtx.html