java.sql.SQLNonTransientConnectionException:由于基础异常无法加载连接类

huangapple go评论70阅读模式
英文:

java.sql.SQLNonTransientConnectionException Cannot load connection class because of underlying exception

问题

以下是翻译好的部分:

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

}

以下是抛出的错误信息:

无法加载连接类,因为底层异常: com.mysql.cj.exceptions.WrongArgumentException: 无法解析主URL部分的格式错误的数据库URL。

我的MySQL Workbench版本是8.0.21,mysql-connector/J也是相同版本。
java.sql.SQLNonTransientConnectionException:由于基础异常无法加载连接类

英文:

New to JDBC and trying to connect to data base in MySql workbench. Following is the Java code

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

}

Following is the error being thrown:

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

My MySQL workbench version is 8.0.21 with same version for mysql-connector/J
java.sql.SQLNonTransientConnectionException:由于基础异常无法加载连接类

答案1

得分: 2

我认为问题出在你的URL中的双斜杠(//)。尝试使用以下代码:

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

I assume the problem is the double slash (//) in your URL. Try

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

instead.

huangapple
  • 本文由 发表于 2020年8月24日 17:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63558723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定