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