Android Studio中未处理的异常Class.forname("com.google.cloud.sql.jdbc.Driver") [英] Unhandled exception Class.forname("com.google.cloud.sql.jdbc.Driver") in Android Studio

查看:293
本文介绍了Android Studio中未处理的异常Class.forname("com.google.cloud.sql.jdbc.Driver")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project in Android Studio that has a Google Cloud Endpoints module. I'm trying to connect my endpoints module to an instance of Google Cloud SQL that I have in that same project.

在IDE中,我看到以下错误:

In the IDE I see the following error:

Unhandled exception: java.lang.ClassNotFoundException

我的gradle版本显示:

My gradle build shows:

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

我不确定将SQL数据库连接到Google App Engine所需做什么.似乎比我想的要复杂.

I'm not sure what I need to do to connect to my SQL db to my Google App Engine. It seems to be more complicated than I thought.

我的代码:

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

任何帮助,评论或指导将不胜感激.

Any help, comments or guidance would be appreciated.

推荐答案

方法

The method Class.forName() will throw a ClassNotFoundException when the given class cannot be located.

由于ClassNotFoundException已检查的异常,因此您实际上必须处理可能发生的可能性.您可以通过将其传递给调用方法来执行此操作.为此,您必须将其添加到方法的签名中:

Since ClassNotFoundException is a checked exception, you actually have to deal with the possibility that one might occur. You can do this either by passing it on to the calling method. To do this, you have to add it to your method's signature:

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

然后调用当前方法的方法必须处理它.

Then the method that called the current method has to deal with it.

或者,您也可以使用

在此示例中,它将仅将异常的堆栈跟踪信息打印到System.err.您可以将错误处理更改为所需的任何内容.

In this example, it will simply print the stack trace of the exception to System.err. You can change that error handling to whatever you want.

这篇关于Android Studio中未处理的异常Class.forname("com.google.cloud.sql.jdbc.Driver")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆