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

I am trying to use MySQL with android studio for an app for school. I’m having trouble getting the commands to run only when I am running with a simulator. In a separate file the commands work fine, and there is no crash. I know using mySQL probably isn’t the best idea, but my group doesn’t have many other options.

import java.sql.DriverManager
import java.sql.SQLException
fun getData(): Array<String> {
    // val url = "jdbc:mysql://localhost:3306/Stonks"
    val url =
        "jdbc:mysql://localhost:3306/Stonks?verifyServerCertificate=false&useSSL=false"
    val user = "Shane"
    val password = "pass"

    try {
        //it fails on this line
        val myCon = DriverManager.getConnection(url, user, password)

errors list:

W/System.err: 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:1006) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823) at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:453) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) 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:218) at com.example.fantasystocks1.MainActivity.getData(MainActivity.kt:94) at com.example.fantasystocks1.ui.Search.SearchFragment.onCreateView(SearchFragment.kt:34) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7660) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1605) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436) at java.net.Socket.connect(Socket.java:621) at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) W/System.err: at com.mysql.cj.NativeSession.connect(NativeSession.java:144) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953)

Answer

This is in your log:

Caused by: android.os.NetworkOnMainThreadException

You can’t connect to your database on a UI or main thread, use a separate thread, ansynctask, etc. Find more in this post

This is an ugly solution, put this before DriverManager.getConnection()

Kotlin:

val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)

Java:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);