-2

so i have declared an SQL connection like below :

public class db {

public void run() throws SQLException {
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "rizkiaswangga", "");
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

i'm just trying to connect MariaDB to my Java project firstly, but although i have declared SQLException, i still encounter an error message like below :

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable code - unreported exception java.sql.SQLException; must be caught or declared to be thrown

when i try to run from other class :

public class Start extends javax.swing.JFrame {

/**
 * Creates new form Start
 */
public Start() {
    initComponents();
    db myDb = new db();
    myDb.run();
}

what is wrong on my code? any help would be appreciated :)

1 Answers1

0

In run method you have declared throws SQLException but you are not throwing exception. Also in Start method you have not added throws SQLException.

Remove throw SQLException from public void run() method.

public class db {

    public void run() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "rizkiaswangga", "");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
Dhaval Gajjar
  • 2,508
  • 1
  • 2
  • 13