10
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

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)

enter image description here enter image description here enter image description here

Vasan
  • 4,810
  • 4
  • 20
  • 39
Abdou Nasser Chan
  • 103
  • 1
  • 1
  • 5

2 Answers2

9

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

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

This shows the string as:

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

This U+202A character is LEFT-TO-RIGHT EMBEDDING.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 2
    @AbdouNasserChan Here you go: `jdbc:mysql://localhost:3306/miniprojectdb` – lexicore May 12 '18 at 23:52
  • nice but it generate me a new error is :SQLException: The server time zone value 'Maroc (heure dÂ?été)' 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. SQLState: 01S00 VendorError0 – Abdou Nasser Chan May 12 '18 at 23:55
  • 2
    @AbdouNasserChan This is a separate question. Your original question is answered. – lexicore May 12 '18 at 23:56
  • yes thank you for help but if you only can help me with this also i will be thankful – Abdou Nasser Chan May 13 '18 at 00:00
  • @AbdouNasserChan I don't use MySQL and don't have much experience with it but the error message tells you what to do. – lexicore May 13 '18 at 00:01
  • ok now i fixed the last error and connect successfully by adding this ("jdbc:mysql://localhost:3306/miniprojectdb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root",""); i got it from here: https://stackoverflow.com/questions/26515700/mysql-jdbc-driver-5-1-33-time-zone-issue – Abdou Nasser Chan May 13 '18 at 00:23
3

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

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