title: External MySQL database in Kotlin
body: <p>I'm making a Kotlin application which should have an external MySQL database. How can I set it up to get desired results? </p>
  
  <p>The application should get a result from External MySQL database as a string. I already tried to use this <a href="https://www.tutorialkart.com/kotlin/connect-to-mysql-database-from-kotlin-using-jdbc/" rel="nofollow noreferrer">link</a> but It seems that there is something wrong.</p>
  
  <p>I use this code:</p>
  
  <pre><code>class DBConnection {
      internal var conn: Connection? = null
      internal var username = "some username" 
      internal var password = "some password" 
  
      fun executeMySQLQuery() {
          var stmt: Statement? = null
          var resultset: ResultSet? = null
          try {
              stmt = conn!!.createStatement()
              resultset = stmt!!.executeQuery("SHOW DATABASES;")
              if (stmt.execute("SHOW DATABASES;")) {
                  resultset = stmt.resultSet
              }
              while (resultset!!.next()) {
                  println(resultset.getString("Database"))
              }
          } catch (ex: SQLException) {
              // handle any errors
              ex.printStackTrace()
          } finally {
              // release resources
              if (resultset != null) {
                  try {
                      resultset.close()
                  } catch (sqlEx: SQLException) {
                  }
                  resultset = null
              }
              if (stmt != null) {
                  try {
                      stmt.close()
                  } catch (sqlEx: SQLException) {
                  }
                  stmt = null
              }
              if (conn != null) {
                  try {
                      conn!!.close()
                  } catch (sqlEx: SQLException) {
                  }
                  conn = null
              }
          }
      }
  
      fun getConnection() {
          val connectionProps = Properties()
          connectionProps.put("user", username)
          connectionProps.put("password", password)
          try {
              Class.forName("com.mysql.jdbc.Driver").newInstance()
              conn = DriverManager.getConnection(
                  "jdbc:" + "mysql" + "://" +
                          "remotemysql.com" +
                          ":" + "3306" + "/" +
                          "",
                  connectionProps)
          } catch (ex: SQLException) {
              // handle any errors
              ex.printStackTrace()
          } catch (ex: Exception) {
              // handle any errors
              ex.printStackTrace()
          }
      }
  }
  </code></pre>
  
  <p>And in a main file I use this:</p>
  
  <pre><code>val submit: Button = findViewById(R.id.submitBtn)
  submit.setOnClickListener{
                  val dbConnection: DBConnection
                  dbConnection = DBConnection()
                  dbConnection.getConnection()
                  dbConnection.executeMySQLQuery()
  
          }
  </code></pre>
  
  <p>For now I just need to get a result from the database. When I used the mentioned code I got this error</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: sk.letsdream, PID: 18521
      kotlin.KotlinNullPointerException
          at sk.letsdream.dbMethods.DBConnection.executeMySQLQuery(DBConnection.kt:19)
          at sk.letsdream.DochadzkaActivity$onCreate$2.onClick(DochadzkaActivity.kt:86)
          at android.view.View.performClick(View.java:7125)
          at android.view.View.performClickInternal(View.java:7102)
          at android.view.View.access$3400(View.java:801)
          at android.view.View$PerformClick.run(View.java:27301)
          at android.os.Handler.handleCallback(Handler.java:883)
          at android.os.Handler.dispatchMessage(Handler.java:100)
          at android.os.Looper.loop(Looper.java:214)
          at android.app.ActivityThread.main(ActivityThread.java:7319)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
  </code></pre>
  
  <p><strong>EDIT:</strong>
  After adding <strong>mysql-connector-java-8.0.16</strong> it gives me an error that min. SDK should be 26. But after changing SDK to 26 it gives me this type error:</p>
  
  <pre><code>java.lang.BootstrapMethodError: Exception from call site #39 bootstrap method
          at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.&lt;clinit&gt;(AbandonedConnectionCleanupThread.java:58)
  </code></pre>
  
  <p>Where 58. line is:</p>
  
  <pre><code>Class.forName("com.mysql.jdbc.Driver").newInstance()
  </code></pre>
  
  <p>When we had older version of mysql connector <strong>mysql-connector-java-5.1.47</strong> it threw this error:</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
  </code></pre>
  
  <p>on this line of code:</p>
  
  <pre><code>var conn = DriverManager.getConnection("jdbc:mysql://remotemysql.com:3306/My_DB_Name", connectionProps)
  </code></pre>