2017-05-08 88 views
0

我試圖從我的Android應用程序連接到我的數據庫在雲數據庫。 最初我嘗試打開與數據庫的連接,而不是使用AsyncTask查詢。Android連接到谷歌雲數據庫與jdbc

雖然我試圖連接我得到這個錯誤:

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) 

我的代碼是:

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

我試圖取代的mysql-connector.jar但它不工作 是否有人能夠幫助我? TNX

回答

0

我想你好想使用DB_NAME您的網址:

private String DB_URL = String.format("jdbc://mysql://xxx.xxx.xxx.xxx:3306/"); 
//WHERE IS THE DB NAME-----------------------------------------------------^ 

其次網址應該是這樣的:

jdbc:mysql://<host>:<port>/<database_name> 

您必須刪除//

jdbc://mysql://xxx.xxx.xxx.xxx:3306/ 
// ^^------------------------------------------ 

我認爲你需要這樣的事情:

private String DB_URL = String.format("jdbc:mysql://xxx.xxx.xxx.xxx:3306/" + DB_NAME); 

二:

+0

你分辯..但鋼不工作 –

+0

什麼現在的問題是什麼?你嘗試使用我的最終網址嗎? 'private String DB_URL = String.format(「jdbc:mysql://xxx.xxx.xxx.xxx:3306 /」+ DB_NAME);'你是否刪除了兩個斜槓? @DanaS –

+0

我在接下來的回答中發佈了新的問題(它很長) –