2

i work on project using intellij idea 15, so when i'm trying to connect to data base i got this error

No suitable driver found for jdbc:mysql://localhost:3306/egov

this is my datasource file

public class DataSource {

private String url;
private String login;
private String password;
private Connection connection;
private Properties properties;
private static DataSource instance;

private DataSource() {
    try {
        properties = new Properties();
        properties.load(new FileInputStream(new File("configuration.properties")));
        url = properties.getProperty("url");
        login = properties.getProperty("login");
        password = properties.getProperty("password");
        connection = DriverManager.getConnection(url, login, password);
    } catch (SQLException | IOException ex) {
        System.out.println(ex.getMessage());
    }
}
public Connection getConnection() {
    return connection;
}
public static DataSource getInstance() {
    if (instance == null) {
        instance = new DataSource();
    }
    return instance;
}
}

and this is the configuration.properties file

url=jdbc:mysql://localhost:3306/egov
login=root
password=

and also i add the jar file mysql-connector-java any one know how to fix this problem

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34

2 Answers2

2

You need to load the db driver files via the IDEA GUI. Go to your View menu, and select Database from the Tool Window submenu. In the Database window click the "wrench" icon in the toolbar at the top, to open up the Data Sources and Drivers window. If you don't have a MySQL data source listed under Project Data Sources, click the + button at the top left to add it, otherwise just click on the "MySQL" line under Project Data Sources. Look near the bottom of the window for a link that says "Download missing driver files", and click it to install the necessary drivers into IDEA.

MarkNFI
  • 556
  • 2
  • 9
1

Amine Harbaoui, did you try this?

login = properties.getProperty("login");
    password = properties.getProperty("password");

     Class.forName("com.mysql.jdbc.Driver");// include this line in your code.

    connection = DriverManager.getConnection(url, login, password);
} catch (SQLException | IOException ex) {
ffSouza
  • 93
  • 8