Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

Unreported exception java.sql.SQLException; must be caught or declared to be thrown?

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...