遇到java.sql.SQLException:Android中没有合适的驱动程序

时间:2015-01-14 11:49:15

标签: android mysql

在Android上我尝试连接到Google Cloud SQL中托管的MySQL数据库,我在Java Swing中进行开发时,基本上使用相同的代码连接并执行创建,读取,更新和删除操作。我已在Android中稍作调整,例如添加日志消息。

在我的连接管理器类中,特别是在方法openConnection()中我遇到以下错误:

  

01-14 12:36:25.891:I / UsersLogInDataConnMgr(3769):openConnection() -   连接faliure: - java.sql.SQLException:没有合适的驱动程序

经过类似的帖子后,我确保避免了以下错误,这些错误似乎与我遇到的错误问题有关:

  • 确保MySQL Connector / J在我的构建路径中。我可以证实 这是因为我可以在引用的库中看到它。
  • 确保我的网址格式正确。

此外,我尝试在openconnection()方法中执行Driver.getConnection()之前添加以下代码:

    try {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

这导致以下错误:

  

01-14 13:12:14.445:W / System.err(4180):java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

以下是我的 UsersLogInDataConnMgr 类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import android.util.Log;

public class UsersLogInDataConnMgr {

    private static UsersLogInDataConnMgr instance = null;
    private static final String USERNAME = "uname";
    private static final String PASSWORD = "pwrd";
    private static final String M_CONN_STRING =
            "jdbc:mysql://XXX.XXX.XXX.XX:3306/testdb";
    private DBType dbType = DBType.MYSQL;
    private Connection conn = null;
    private String TAG = "UsersLogInDataConnMgr";

    private UsersLogInDataConnMgr() {

    }

    public static UsersLogInDataConnMgr getInstance() {
        if (instance == null) {
            instance = new UsersLogInDataConnMgr();
        }

        return instance;
    }

    public void setDBType(DBType dbType) {
        this.dbType = dbType;
    }

    private boolean openConnection() {
        try {

            switch (dbType) {

                case MYSQL:
                    conn = DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);

                    Log.i(TAG, "\nopenConnection() - conn == " + conn + " Return value is TRUE");

                    return true;

                default:
                    Log.i(TAG, "\nopenConnection() - Return value is FALSE");
                    return false;
            }
        } catch (SQLException e) {
            Log.i(TAG, "\nopenConnection() - Connection faliure: - " + e);

            return false;
        }
    }

    public Connection getConnection() {
        if (conn == null) {
            if (openConnection()) {
                Log.i(TAG, "\nUsersLogInDataConnMgr - CONNECTION OPENED");
                return conn;
            } else {
                Log.i(TAG, "\nUsersLogInDataConnMgr - CONNECTION FAILED TO OPEN");
                return null;
            }
        }
        return conn;
    }

    public void close() {
        Log.i(TAG, "\nUsersLogInDataConnMgr - CLOSING CONNECTION");
        try {
            conn.close();
            conn = null;
        } catch (Exception e) {
            Log.i(TAG, "\nUsersLogInDataConnMgr - CONNECTION NOT CLOSED");          
        }
    }

}

以下是我的 DBType 类:

public enum DBType {
    MYSQL
}

以下是我的 DBUtil 类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import android.util.Log;

public class DBUtil {

    private static UsersLogInDataConnMgr instance = null;
    private static final String USERNAME = "uname";
    private static final String PASSWORD = "pwrd";
    private static String TAG = "DBUtil";

    public static Connection getConnection(DBType dbType) throws SQLException {

        switch (dbType) {
            case MYSQL:
                return DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
            default:
                return null;
        }

    }

    public static void processException(SQLException e) {
        Log.e(TAG, "Error message: " + e.getMessage());
        Log.e(TAG, "Error code: " + e.getErrorCode());
        Log.e(TAG, "SQL state: " + e.getSQLState());
    }

}

感谢任何帮助。

0 个答案:

没有答案