我想从Android连接到云SQL。下面是我用来建立连接的代码。是否需要从云控制台执行任何代理设置?我是否需要在我的电脑上安装SQL或类似的东西?
此外,到目前为止,我已经写了下面的代码,并得到,没有找到合适的驱动程序,错误。
代码:
public void getDatafromSQL() {
Log.d("GeoL", "getSQL");
Connection connection;
String query = "Some query";
try {
String databaseName = "databaseName";
String instanceConnectionName = "instanceConnectionName";
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName,
instanceConnectionName);
connection = DriverManager.getConnection(jdbcUrl, user, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
Log.d("GeoL", String.valueOf(resultSet));
} catch (SQLException e) {
e.printStackTrace();
}
}错误信息:
07-28 16:55:22.881 11550-11550/com.example.pratyush.geofencing W/System.err: java.sql.SQLException: No suitable driver found for jdbc:mysql://google/[databasename]?cloudSqlInstance=[instanceName]&socketFactory=com.google.cloud.sql.mysql.SocketFactory
07-28 16:55:22.886 11550-11550/com.example.pratyush.geofencing W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:594)
07-28 16:55:22.886 11550-11550/com.example.pratyush.geofencing W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:219)
07-28 16:55:22.886 11550-11550/com.example.pratyush.geofencing W/System.err: at com.example.pratyush.geofencing.Main2Activity.getDatafromSQL(Main2Activity.java:169)
07-28 16:55:22.886 11550-11550/com.example.pratyush.geofencing W/System.err: at com.example.pratyush.geofencing.Main2Activity$1.onClick(Main2Activity.java:62)发布于 2017-07-28 11:55:38
因此,如果您想通过JDBC连接到外部MySQL数据库,就必须导入相应的JDBC驱动程序。
请查看以下链接:https://gist.github.com/cofearabi/5039135
这是Android的一个例子。驱动程序可从官方MySQL主页下载。
https://stackoverflow.com/questions/45372672
复制相似问题