Please can I have an example of connecting to a maria/mysql database
I have tried many examples/tutorials to no avail.
If you don’t show what you’ve tried and how it failed, you won’t get an answer. We can’t guess which tutorials you’ve read. The only possible answer based on the information you’ve provided would be to send a link to a tutorial—but that wouldn’t help you, would it?
Like I said, I used several tutorial, and many were found to have outdated procedures. Which link of the hundreds would you like only for you to find it uses redundant functions? . If you are unable to provide a step by step guide from scratch or point me to an updated guide, then why comment. That is not very helpful, is it?
The problem is that this is not really a Kotlin-specific question. It’s not even really a Java-specific question… you should really be looking at the documentation for the databases, and the Java documentation around connecting to databases, and the Java libraries that are provided by those database providers to connect to their databases.
However, I’ll give you a quick cheat sheet: you will need two things to connect to a database.
The first one is a driver library for that database. There’s an interface in the Java standard library, java.sql.Driver
. A driver library will have a class that implements that interface. So if you want to connect to a MySQL database, you need the MySQL Java driver. If you want to connect to a MariaDB database, you need the MariaDB Java driver.
The second thing you need is a library to interact with the database. (I think you can do this with just the raw driver, but you have to write your own queries, handle mapping the data from the database etc, it’s a lot of work.) There’s a lot of options; two that I have used are the Jdbi library, and the Jetbrains Exposed library. Pick one, read the docs for how to connect to the database and query it.
Thank you. I appreciate. This is the latest most scale-down version I tried which was converted from java to kotlin by android studio. I used all the andorid, mariadb and java and kotlin manuals, but nothing seems to get me past this point. Eveything else on the android app, written in kotlin, works fine, it just does not want to give me a connection. i have seen something about threading, but also that did not work… yet…
val host = "jdbc:mariadb://192.168.9.37:3306/c2_bb_cat"
val username = "removed"
val password = "removed"
try {
DriverManager.getConnection(host, username, password)
val sql = "SELECT client_ref from clients LIMIT 1"
val statement = conn?.prepareStatement(sql)
val resultSet = statement?.executeQuery()
binding.entrydate.text = resultSet as CharSequence? as CharSequence?
if (resultSet?.next() == true)
{
binding.entrydate.text = resultSet.getString("client_ref") + "\n"
}
} catch (e: SQLException) {
binding.entrydate.text = "error\n"
}
When I click the button, nothing happens, when I remove the connection outside the try loop, it crashes.
The goal is then pretty simple: Click the button, and it pops up with successful connect prompt.
I am sure this is pretty much used a lot... or am I stuck having to use a web call to a php page that can handle this?
So I have tried everything thus far from what I could find from posts to get past the strict mode issue and where I used those examples but with the same results.
FATAL EXCEPTION: main
Process: com.example.myapplicationa, PID: 31118
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1667)
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:646)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:405)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1123)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:545)
at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:174)
at org.mariadb.jdbc.Driver.connect(Driver.java:92)
at java.sql.DriverManager.getConnection(DriverManager.java:580)
at java.sql.DriverManager.getConnection(DriverManager.java:218)
at com.example.myapplicationa.SecondFragment.syncRecords(SecondFragment.kt:116)
at com.example.myapplicationa.SecondFragment.onViewCreated$lambda$2(SecondFragment.kt:53)
at com.example.myapplicationa.SecondFragment.$r8$lambda$Q5szzul4ki_5jGMgJsZ3-85XVb8(Unknown Source:0)
at com.example.myapplicationa.SecondFragment$$ExternalSyntheticLambda4.onClick(D8$$SyntheticClass:0)
at android.view.View.performClick(View.java:7542)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1218)
at android.view.View.performClickInternal(View.java:7519)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:29476)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7924)
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:936)
Solution.
The code that worked;
// Create a connection
Class.forName(“org.mariadb.jdbc.Driver”) // Initialize it
val connectionString = “jdbc:mysql://192.168.9.41:3306/test_db”
val username = “user”
val password = “pass”
val con = DriverManager.getConnection(connectionString, username, password)
In the Main Activity class I had to add these two lines:
(This is what caused my app to crash if missing)
“val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)”