本文介绍了未报告的异常java.sql.SQLException;必须被抓或宣布被抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试编译以下代码时遇到此错误。我想知道我做错了什么。

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


推荐答案

你需要 catch 您方法中的异常:

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

声明方法抛出 SQLException

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

这篇关于未报告的异常java.sql.SQLException;必须被抓或宣布被抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:30