Android 使用 jdbc 连接到 google cloud sql



我正在尝试从我的Android应用程序连接到Cloud 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连接器.jar但它不起作用有人可以帮助我吗?嘟嘟

我想你错过了在你的URL中使用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);

第二:

相关内容

  • 没有找到相关文章

最新更新