2017-05-08 92 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

我在接下来的回答中发布了新的问题(它很长) –