0

I'm sorry for my question but can't solve this problems myself. Read different topics here and tried to read tutuorials...

I have MySQL server v8, and trying to start simple example in java to get data from server.

I set up classpat as system variable, put here mysql-connector-java-8.0.13.jar. Comman echo %CLASSPATH% shows it properly. Also i put connector in all project folders xD (didn't help either).

import java.sql.*;

public class file_test {

    // JDBC URL, username and password of MySQL server

    private static final String url = "jdbc:mysql://localhost:3306/MySQL80";
    private static final String user = "root";
    private static final String password = "123";

    // JDBC variables for opening and managing connection
    private static Connection con;
    private static Statement stmt;
    private static ResultSet rs;

    public static void main(String args[]) {
        String query = "select count(*) from actor";

        try {
            // opening database connection to MySQL server
            con = DriverManager.getConnection(url, user, password);

            // getting Statement object to execute query
            stmt = con.createStatement();

            // executing SELECT query
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                int count = rs.getInt(1);
                System.out.println("Total number of actors in the table : " + count);
            }

        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        } finally {
            //close connection ,stmt and resultset here
            try {
                con.close();
            } catch (SQLException se) { /*can't do anything */ }
            try {
                stmt.close();
            } catch (SQLException se) { /*can't do anything */ }
            try {
                rs.close();
            } catch (SQLException se) { /*can't do anything */ }
        }
    }

}

Problem is when i try to start projest, IJ says:

"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA\lib\idea_rt.jar=63236:C:\Program Files\JetBrains\IntelliJ IDEA\bin" -Dfile.encoding=UTF-8 -classpath F:\work\program\file_test\out\production\file_test file_test
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/MySQL80
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at file_test.main(file_test.java:21)
Exception in thread "main" java.lang.NullPointerException
    at file_test.main(file_test.java:38)
Process finished with exit code 1

I want to run this code from IJ and get data from DB as a result.

Andrey
  • 19
  • 7
  • You forgot to describe your actual problem, and we can't help you without that important piece information. Make sure to provide a [mcve], clearly describe your problem (including expected and actual results, exception stacktraces, etc), and make sure to provide a title that covers your question. – Mark Rotteveel Jan 28 '19 at 18:30
  • Have you tried this - https://stackoverflow.com/questions/16742085/adding-jar-files-to-intellijidea-classpath – Ramesh Subramanian Jan 29 '19 at 04:32

0 Answers0