0

After googling the solution, most call for importing the jar file into the project library. Problem is, I've already added the file. I've even tried the alternative: adding the Netbeans library. Both don't work.

It just doesn't recognize "com.mysql.jdbc.Driver".

If it helps I'm trying to connect to XAMMP MySQL.

enter image description here

user1869558
  • 697
  • 3
  • 11
  • 21

1 Answers1

1

try add another version of and then clean and build the project

package com.sj;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class MysqlCon {

public static void main(String args[]) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from Student");
        while (rs.next()) {
            System.out.println(rs.getInt(2) + "  " + rs.getString(1));
        }
        con.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}
}
  • This resolved it. The issue was the "throws SQL exception" line. For some weird reason the exception was causing issues for the Class library. Also, "finally" should be omitted. (got that from the JDBC Basics Tutorial, might be outdated or irrelevant) – user1869558 Apr 14 '17 at 06:22