0

This code works perfectly on console, but once I put it into an app it started throwing me this error:

I/System.out: SQLException: Could not create connection to database server.Error code: 08001

Using ex.toString() gives me:

java.sql.SQLNonTransientConnectionException: Could not create connection to database server.

Here is my code to connect to the server:

fun executeStatement(command: String?): ResultSet? {
    var conn: Connection? = null
    try {
        //initiate connection
        conn = DriverManager.getConnection(
            "jdbc:mysql://mysql.xxxxx.com:55207/xxxxxxxxxxx" +
                    "user=xxxxx&password=xxxxxxx"
        )
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance()

        //send commands to the server
        var statement: Statement? = null
        var resultSet: ResultSet? = null
        //System.out.println("Executing command: "+command);
        // confirms that the code is executing the command in the console
        statement = conn.createStatement()
        if (statement.execute(command)) {
            resultSet = statement.resultSet
            return resultSet
            //returns the results as a ResultSet
        }
    } catch (ex: SQLException) {
        // handle any errors and output to console
        println("SQLException: " + ex.message + "Error code: " + ex.sqlState)
    } catch (ex: ClassNotFoundException) {
        println("SQLException: " + ex.message)
    } catch (ex: IllegalAccessException) {
        ex.printStackTrace()
    } catch (ex: InstantiationException) {
        ex.printStackTrace()
    }
    return null
}

My app has internet permissions, the MySQL connector jar is the latest one (8.0.28); this code worked fine before I started adding a UI to it.

Sidenote: in trying to solve this problem, I have come across multiple pages where people have just been criticising the choice of using JDBC for security reasons. I'm just a student building this in my spare time, and this project won't get further than my phone, so the security disadvantages aren't a massive problem for me if I'm honest. I'm just trying to waste the least time in possible in trying to get this to work.

Full stacktrace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.revisionplannergenerator3, PID: 8803
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1002)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:819)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:449)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:242)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
        at java.sql.DriverManager.getConnection(DriverManager.java:580)
        at java.sql.DriverManager.getConnection(DriverManager.java:236)
        at com.example.revisionplannergenerator3.connectToDB.executeStatement(ConnectToDB.kt:39)
        at com.example.revisionplannergenerator3.TaskOrder.taskOrder(TaskOrder.kt:110)
        at com.example.revisionplannergenerator3.TaskTimes.generateSessions(TaskTimes.kt:24)
        at com.example.revisionplannergenerator3.TaskTimes.generateSessions$default(TaskTimes.kt:23)
        at com.example.revisionplannergenerator3.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:27)
        at com.example.revisionplannergenerator3.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:25)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.material.SurfaceKt$Surface$6.invoke(Surface.kt:267)
        at androidx.compose.material.SurfaceKt$Surface$6.invoke(Surface.kt:254)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.material.SurfaceKt.Surface-F-jzlyU(Surface.kt:251)
        at androidx.compose.material.SurfaceKt.Surface-F-jzlyU(Surface.kt:110)
        at com.example.revisionplannergenerator3.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:22)
        at com.example.revisionplannergenerator3.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:20)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.material.TextKt.ProvideTextStyle(Text.kt:252)
        at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81)
        at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.material.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:72)```
kishore
  • 1
  • 1
  • Please provide the full exception stacktrace. As far as I'm aware, recent versions of MySQL Connector/J fail to work with Android because it uses Java features not available on Android. – Mark Rotteveel Feb 23 '22 at 16:33
  • Also, [please reconsider your use of JDBC on Android](https://stackoverflow.com/q/15853367/115145). – CommonsWare Feb 23 '22 at 16:35
  • @MarkRotteveel I've added what I believe is the full exception stacktrace; please let me know if I have done something different. – kishore Feb 23 '22 at 19:59
  • That is not a stacktrace. A stacktrace contains all method calls leading up to the exception, including exception causes (other exceptions that caused this one to be thrown). – Mark Rotteveel Feb 24 '22 at 07:44
  • @MarkRotteveel Sorry about that; is this it? – kishore Feb 24 '22 at 09:21
  • Yes, but it doesn't seem to be complete. I would expect the `java.sql.SQLNonTransientConnectionException` to have at least one more _Caused by:_ with the actual cause of the exception. – Mark Rotteveel Feb 24 '22 at 09:33
  • 1
    @MarkRotteveel Yes, it wasn't complete; I wasn't sure how much to put on, and thought the rest was irrelevant. I did find a solution though; the problem was caused by the same error that you responded to here: [link](https://stackoverflow.com/questions/68508321/android-java-lang-noclassdeffounderror-failed-resolution-of-lcom-mysql-cj-mys) – kishore Feb 25 '22 at 09:14

0 Answers0