簡體   English   中英

Android使用jdbc連接到谷歌雲sql

[英]Android connect to google cloud sql with jdbc

我正在嘗試從我的android應用程序連接到雲sql的數據庫。 最初,我嘗試打開與數據庫的連接,然后使用AsyncTask進行查詢。

當我嘗試連接時出現此錯誤:

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

我認為您錯過了在URL中使用DB_NAME的方法:

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

第二個URL應該看起來像這樣:

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

第二:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM