【发布时间】: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