英文:
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也是相同版本。
英文:
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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论