Java 格式错误的数据库 URL,无法解析主要 URL 部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50311481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:33:28  来源:igfitidea点击:

Malformed database URL, failed to parse the main URL sections

javamysql

提问by Abdou Nasser Chan

package miniproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Mysql_connection {

    private Connection con=null;
    private Statement st=null;
    private ResultSet res=null;
    public Mysql_connection() {
    try{        
            //Class.forName("com.mysql.jdbc.Driver");

            con=DriverManager.getConnection("jdbc:mysql:?//localhost:3306/miniprojectdb","root","");
            st=con.createStatement();

    }catch(SQLException e)
    {
        System.out.println("SQLException: "+ e.getMessage());
        System.out.println("SQLState: "+ e.getSQLState());
        System.out.println("VendorError"+ e.getErrorCode());   
    }
    }
    public void Data(){
    try{        
        String query="select * from produit";
        res=st.executeQuery(query);
        System.out.println("========================="); 
        while(res.next()){
            int a=res.getInt(1);
            String b=res.getString(2);
            int c=res.getInt(3);
            System.out.println("ProdID:"+a+" "+"ProdNom:"+b+" "+"Prix/Tonne:"+c);
        }
    }catch(Exception e){ 
        System.out.println(e);  
    }   
    }

}

I'm getting this Error:

我收到此错误:

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

SQLState: 08001

VendorError0

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

SQLState: 08001

供应商错误0

I'm using this (C:\Users\ABDOU NASSER\Desktop\mysql-connector-java-8.0.11\mysql-connector-java-8.0.11.jar) with the last eclipse version 2018 and as database XAMPP (MySQL)

我将此 ( C:\Users\ABDOU NASSER\Desktop\mysql-connector-java-8.0.11\mysql-connector-java-8.0.11.jar) 与最新的 Eclipse 版本 2018 和数据库 XAMPP (MySQL) 一起使用

enter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明

采纳答案by lexicore

There's an invisible character between mysql:and //localhostin your JDBC URL. You can check it here:

在您的 JDBC URL之间mysql://localhost中存在一个不可见字符。你可以在这里查看:

https://www.soscisurvey.de/tools/view-chars.php

https://www.soscissurvey.de/tools/view-chars.php

This shows the string as:

这将字符串显示为:

jdbc:mysql:?U+202A//localhost:3306/miniprojectdb

This U+202Acharacter is LEFT-TO-RIGHT EMBEDDING.

这个U+202A字符是LEFT-TO-RIGHT EMBEDDING

回答by Abdou Nasser Chan

You have a space in jdbc connection URL, remove that space and try again:

您在 jdbc 连接 URL 中有一个空格,请删除该空格并重试:

jdbc:mysql:?//localhost:3306/miniprojectdb
-----------^