有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mysql java。sql。由于基础异常,SQLNonTransientConnectionException无法加载连接类

JDBC新手,正在尝试连接MySql workbench中的数据库。下面是Java代码

import java.sql.*;


public class Demo {
    
    public static void main(String[] args) throws Exception
    {   
        String url = "jdbc:mysql://localhost:3306//students?useSSL=false"; 
        String user = "root";
        String password = "root";
        String query = "Select * from students";
        
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, user, password);  
        Statement st = con.createStatement();
        ResultSet rt = st.executeQuery(query);
        
        rt.next();
        String name = rt.getString("stu_name");
        
        System.out.println(name);
        
        st.close();
        con.close();
    }

}

以下是引发的错误:

Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.

我的MySQL workbench版本是8.0.21,与MySQL connector/J的版本相同 Database in workbench


共 (1) 个答案

  1. # 1 楼答案

    我想问题是URL中的双斜杠(//)。试试看

     String url = "jdbc:mysql://localhost:3306/students?useSSL=false"; 
    

    相反