【问题标题】:Unhandled exception Class.forname("com.google.cloud.sql.jdbc.Driver") in Android StudioAndroid Studio 中未处理的异常 Class.forname("com.google.cloud.sql.jdbc.Driver")
【发布时间】:2015-07-30 14:32:26
【问题描述】:

我在 Android Studio 中有一个具有 Google Cloud Endpoints 模块的项目。我正在尝试将我的端点模块连接到我在同一个项目中拥有的 Google Cloud SQL 实例。

在 IDE 中我看到以下错误:

Unhandled exception: java.lang.ClassNotFoundException

我的 gradle build 显示:

Error:(82, 26) error: unreported exception ClassNotFoundException; must be caught or declared to be thrown 
Error:(87, 26) error: unreported exception ClassNotFoundException; must be caught or declared to be thrown

我已在appengine-web.xml 中启用了 J 连接器

我不确定我需要做什么才能将我的 SQL 数据库连接到我的 Google App Engine。它似乎比我想象的要复杂。

我的代码:

@ApiMethod(name = "getLesson")
public Lesson getLesson(@Named("id") Long id) {

    Lesson l = new Lesson();

    l.setLessonId(345);
    l.setLessonColour("Blue");
    l.setLessonImage("itshappening.gif");



    String url = null;
    if (SystemProperty.environment.value() ==
            SystemProperty.Environment.Value.Production) {
        // Connecting from App Engine.
        // Load the class that provides the "jdbc:google:mysql://"
        // prefix.
        Class.forName("com.google.cloud.sql.jdbc.Driver");
        url =
                "jdbc:google:mysql://app:instance?user=root";
    } else {
        // Connecting from an external network.
        Class.forName("com.mysql.jdbc.Driver");
        url = "jdbc:mysql://000.000.000.000:3306?user=root";
    }

    Connection conn = null;
    try {
        conn = DriverManager.getConnection(url);
    } catch (SQLException e) {
        l.setLessonDescription(e.getStackTrace().toString());
    }

    try {
        ResultSet rs = conn.createStatement().executeQuery(
                "SELECT 1 + 56");
    } catch (SQLException e) {
        e.printStackTrace();
    }

    logger.info("Calling getLesson method");

    return l;
}

任何帮助、cmets 或指导将不胜感激。

【问题讨论】:

    标签: java android sql google-app-engine jdbc


    【解决方案1】:

    当找不到给定的类时,方法Class.forName() 将抛出ClassNotFoundException

    由于ClassNotFoundExceptionchecked exception,您实际上必须处理可能发生的可能性。您可以通过将其传递给调用方法来执行此操作。为此,您必须将其添加到方法的签名中:

    @ApiMethod(name = "getLesson")
    public Lesson getLesson(@Named("id") Long id) throws ClassNotFoundException {
        // ...
    

    那么调用当前方法的方法就要处理它。

    或者,您也可以直接在当前方法中处理它,使用try/catch block

    try {
        // ...
    
        Class.forName("com.google.cloud.sql.jdbc.Driver");
    
        // ...
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    

    在本例中,它将简单地将异常的堆栈跟踪打印到System.err。您可以将错误处理更改为您想要的任何内容。

    【讨论】:

    • 叹息。没有我想象的那么复杂......感谢您的洞察力/帮助。我应该看到这个。
    【解决方案2】:

    ClassNotFoundException 是一个检查异常,所以你必须要么捕获它要么抛出它,正如错误所说。

    遵循您当前的异常处理方案:

    try {
        Class.forName("com.google.cloud.sql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        l.setLessonDescription(e.getStackTrace().toString());
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 2018-04-01
      相关资源
      最近更新 更多