Android Studio 中未处理的异常 Class.forname("com.google.cloud.sql.jdbc.Driver")
Unhandled exception Class.forname("com.google.cloud.sql.jdbc.Driver") in Android Studio
我在 Android Studio 中有一个项目,它有一个 Google Cloud Endpoints 模块。我正在尝试将我的端点模块连接到我在同一个项目中拥有的 Google Cloud SQL 实例。
在 IDE 我看到以下错误:
Unhandled exception: java.lang.ClassNotFoundException
我的 gradle 构建显示:
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;
}
如有任何帮助、评论或指导,我们将不胜感激。
ClassNotFoundException
是一个已检查的异常,因此您必须按照错误说明捕获它或抛出它。
要遵循您当前的异常处理方案:
try {
Class.forName("com.google.cloud.sql.jdbc.Driver");
} catch (ClassNotFoundException e) {
l.setLessonDescription(e.getStackTrace().toString());
}
无法找到给定 class 时的方法 Class.forName()
will throw a ClassNotFoundException
。
由于 ClassNotFoundException
是 checked 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
。您可以将该错误处理更改为您想要的任何内容。
我在 Android Studio 中有一个项目,它有一个 Google Cloud Endpoints 模块。我正在尝试将我的端点模块连接到我在同一个项目中拥有的 Google Cloud SQL 实例。
在 IDE 我看到以下错误:
Unhandled exception: java.lang.ClassNotFoundException
我的 gradle 构建显示:
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。好像比我想象的要复杂
我的代码:
@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;
}
如有任何帮助、评论或指导,我们将不胜感激。
ClassNotFoundException
是一个已检查的异常,因此您必须按照错误说明捕获它或抛出它。
要遵循您当前的异常处理方案:
try {
Class.forName("com.google.cloud.sql.jdbc.Driver");
} catch (ClassNotFoundException e) {
l.setLessonDescription(e.getStackTrace().toString());
}
无法找到给定 class 时的方法 Class.forName()
will throw a ClassNotFoundException
。
由于 ClassNotFoundException
是 checked 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
。您可以将该错误处理更改为您想要的任何内容。