1

Basically everything's connected and database works properly except for the fact that I'm unable to print anything out of it into the console. And I dunno how 2 fix this very problem. Help me out pls

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;

class Main{
    private static final String url = "jdbc:mysql://localhost:3306/";
    private static final String user = "root";
    private static final String password = "12345678";
    public static void main(String args[]){
        try {
            Class.forName("com.mysql.jdbc.Driver");//it goes to catch immediately after this line
            Connection con = DriverManager.getConnection(url, user, password);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from emp");
            while (rs.next()) {
                int price = rs.getInt(3);
                System.out.println(price);
            }
            con.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }

    }
}

That what it prints in the end of execution:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Process finished with exit code 0 enter image description here

andrewJames
  • 19,570
  • 8
  • 19
  • 51
Matthew
  • 19
  • 4
  • Have you checked that the table contains data? – jmizv Dec 11 '20 at 19:46
  • It definitely contains data. The problem is in the 1 line of the main method I suppose – Matthew Dec 11 '20 at 19:50
  • can you share the schema of the emp table – Shachaf.Gortler Dec 11 '20 at 19:59
  • _"it goes to catch immediately after this line"_ - and prints an error message and stack trace, I assume. You can [edit] your question to show this (formatted) information. Also, there are many questions on SO about JDBC URLs + MySQL + `Class.forName()` + driver JAR file locations + classpath issues. Have you researched those? – andrewJames Dec 11 '20 at 20:02
  • I went through a bunch of pages about that but I guess I missed something – Matthew Dec 11 '20 at 20:05
  • Two pointers: (1) Using `Class.forName()` is [no longer required](https://stackoverflow.com/questions/5484227/jdbc-class-forname-vs-drivermanager-registerdriver) in most situations involving modern JDBC drivers. (2) You may still get `ClassNotFoundException` even after removing that line of code. See [here](https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database/2840358#2840358). – andrewJames Dec 11 '20 at 20:54

2 Answers2

1

You should name your database when connecting to MySQL:

private static final String url = "jdbc:mysql://localhost:3306/your_database_name";
jmizv
  • 1,172
  • 2
  • 11
  • 28
1

It appears you have an off-by-one issue. Column 3 is the "Manufacturer" (a string). According to the docs "the first column is 1, the second is 2, ..."

https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getInt(int)

rs.getString(3);
rs.getInt(4);
rs.getInt("ProductCount");

I just noticed you added the error message. It appears you need to add the mysql connector jar. See if this post answers your question :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

Marshall C
  • 359
  • 3
  • 8
  • You r right, mate but the compiler doesn't even get to execute that part of me code. Cos smth is wrong with the first line of Main – Matthew Dec 11 '20 at 20:15