10

I got this error while trying to compile the below code. I would like to know what is I have done wrong.

unreported exception java.sql.SQLException; must be caught or declared to be thrown
 Class.forName(myDriver);

               ^
private void setupInfo() {

    Driver driver = new org.gjt.mm.mysql.Driver();
    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "123456";

    String problemFeatureSpecTableName = "ProblemFeatureSpec";
    String solutionFeatureSpectTableName = "SolutionFeatureSpec";
    String classTableName = "Class";
    String extraDataTableName = "ExtraData";
    String casebaseTablename = "CaseBase";
    String problemTableName = "Problem";
    String solutionTableName = "Solution";
    String inactiveContextsTableName = "InactiveContext";
    String constantsTableName = "Constants";
    dbInfo = new DBInfo(new JDBCDriverInfo(driverName, url, username, password),constantsTableName);
    problemSpecInfo = new FeatureSpecRDBInfo(problemFeatureSpecTableName, classTableName, extraDataTableName);
    solutionSpecInfo = new FeatureSpecRDBInfo(solutionFeatureSpectTableName, classTableName, extraDataTableName);
    rdbCasebaseInfo = new RDBCaseBaseInfo(casebaseTablename, solutionTableName, problemTableName, inactiveContextsTableName);
}
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
karikari
  • 6,627
  • 16
  • 64
  • 79
  • 4
    You're reading outdated MySQL JDBC tutorials/examples. The [MM driver](http://mmmysql.sourceforge.net/old-index.html) has been taken over by MySQL over 8 years ago and is since then known as [Connector/J](http://dev.mysql.com/downloads/connector/j/) with `com.mysql.jdbc.Driver`. The MM driver classname is only retained for backwards compatibility, but you should really be using `com.mysql.jdbc.Driver`. Your compilation error does by the way not match with the posted source code. You aren't using `Class#forName()` anywhere in the source. Even more, it doesn't throw `SQLException` at all. – BalusC Dec 16 '10 at 04:23

4 Answers4

13

You either need to catch the exception in your method:

public void setupInfo()
{
    try
    {
        // call methods that might throw SQLException
    }
    catch (SQLException e)
    {
        // do something appropriate with the exception, *at least*:
        e.printStackTrace();
    }
}

Or declare the method to throw SQLException:

private void setupInfo() throws SQLException
{
    // call methods that might throw SQLException
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Catch the exception or throw it. Better use an IDE (Eclipse or Netbeans), which will tell you the error the moment you press enter.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pakka Techie
  • 682
  • 5
  • 6
1

This line of code throws an uncaught exception:

Driver driver = new org.gjt.mm.mysql.Driver();

try this:

try {
   Driver driver = new org.gjt.mm.mysql.Driver();
}
catch (java.sql.SQLException e) {
  // you may want to do something useful here
 // maybe even throw new RuntimException();
}
Bnjmn
  • 1,973
  • 1
  • 20
  • 34
0

Always try to get help from your IDE. IDEs can often fix the error automatically. Press alt+enter in IntelliJ IDEA or ctrl+1 in Eclipse and choose to fix the error.

Nowaker
  • 12,154
  • 4
  • 56
  • 62
  • Unfortunately (IMO) the typical correction for this error is to catch the exception, print a stacktrace to stderr and continue as if nothing had happened. It is generally not the right thing to do. – Stephen C Dec 16 '10 at 05:32
  • Usually there are two options - catch or throws. – Nowaker Dec 16 '10 at 13:43