未报告的exceptionjava.sql.SQLException; 必须被捕获或宣布被抛出?

我试图编译下面的代码时出现这个错误。 我想知道我做错了什么。

未报告的exceptionjava.sql.SQLException; 必须被逮捕或宣布被抛出
 的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); } 

您或者需要在您的方法中捕获exception:

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

或者声明抛出SQLException的方法:

 private void setupInfo() throws SQLException { // call methods that might throw SQLException } 

抓住例外或抛出。 更好地使用IDE(Eclipse或Netbeans),它将在您按回车键时告诉您错误。

这行代码引发一个未捕获的exception:

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

尝试这个:

 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(); } 

总是尝试从您的IDE获得帮助。 IDE通常可以自动修复错误。 按alt +在IntelliJ IDEA中input或在Eclipse中按Ctrl + 1,然后select修复错误。