[英]Android connect to google cloud sql with jdbc
i'm trying to connect to my database at cloud sql from my android applicion. 我正在尝试从我的android应用程序连接到云sql的数据库。 initially i try to open connection with the data base and than working with AsyncTask for queries. 最初,我尝试打开与数据库的连接,然后使用AsyncTask进行查询。
While i tried to connect i get this error: 当我尝试连接时出现此错误:
java.sql.SQLException: No suitable driver
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err:
at java.sql.DriverManager.getConnection(DriverManager.java:186)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:213)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at com.example.user.trackyournevi.database.GoogleConnection.initDBConnection(GoogleConnection.java:46)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at com.example.user.trackyournevi.database.GoogleConnection.<init>(GoogleConnection.java:40)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at com.example.user.trackyournevi.database.GoogleConnection.getsInstance(GoogleConnection.java:35)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at com.example.user.trackyournevi.LoginActivity.onCreate(LoginActivity.java:86)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.Activity.performCreate(Activity.java:6876)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.ActivityThread.access$1100(ActivityThread.java:221)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.os.Looper.loop(Looper.java:158)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7224)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
05-08 21:51:29.533 12626-12626/com.example.user.trackyournevi W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
my code is: 我的代码是:
package com.example.user.trackyournevi.database;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GoogleConnection {
private static final String TAG = "GoogleConnection";
private static GoogleConnection mInstance = null;
private Connection connection;
private String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private String DB_NAME = "xxx";
private String DB_USER = "xxx";
private String DB_PASSWORD = "xxx";
private String DB_URL = String.format("jdbc://mysql://xxx.xxx.xxx.xxx:3306/");
public static synchronized GoogleConnection getsInstance() {
//Use the application context, which will insure that you don't
//accidentally leak on Activity's context
if(mInstance == null)
mInstance = new GoogleConnection();
return mInstance;
}
private GoogleConnection() {
initDBConnection();
}
private void initDBConnection() {
try {
Class.forName(DRIVER_CLASS);
this.connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
} catch (SQLException | ClassNotFoundException e){
//Log.e(TAG, "Can not connect to db");
e.printStackTrace();
System.exit(0);
}
}
I tried to replace mysql-connector.jar but it's not working Is someone can help me? 我试图替换mysql-connector.jar但无法正常工作有人可以帮助我吗? Tnx TNX
I think you miss to use the DB_NAME in your URL : 我认为您错过了在URL中使用DB_NAME的方法:
private String DB_URL = String.format("jdbc://mysql://xxx.xxx.xxx.xxx:3306/");
//WHERE IS THE DB NAME-----------------------------------------------------^
Second the url should look like this : 第二个URL应该看起来像这样:
jdbc:mysql://<host>:<port>/<database_name>
You have to remove the //
: 您必须删除//
:
jdbc://mysql://xxx.xxx.xxx.xxx:3306/
// ^^------------------------------------------
I think you need something like this : 我认为您需要这样的东西:
private String DB_URL = String.format("jdbc:mysql://xxx.xxx.xxx.xxx:3306/" + DB_NAME);
Second : 第二:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.