I'm new to creating Android applications,I write code in Kotlin and I need your help.
I want to connect my Android application to my local server which is located on the MySQl computer. I already wrote the code and thought that it would work one hundred percent but nothing worked. I need to for example, my user could register and this data was sent to the server and then I could easily log into this account using this data.
I already have a website that is already connected to this local server and to the database and everything works fine.
This is file "Dbhelper"
package com.example.myapplication
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
class Dbhelper(private val url: String, private val user:
String, private val password: String) {
init {
try {
Class.forName("com.mysql.cj.jdbc.Driver")
} catch (e: ClassNotFoundException) {
e.printStackTrace()
}
}
fun connect(): Connection? {
try {
return DriverManager.getConnection(url, user, password)
} catch (e: SQLException) {
e.printStackTrace()
}
return null
}
fun isUserExists(login: String, email: String): Boolean {
connect()?.use { connection ->
val query = "SELECT * FROM appmovie_users WHERE login = ? OR email = ?"
val preparedStatement = connection.prepareStatement(query)
preparedStatement.setString(1, login)
preparedStatement.setString(2, email)
val resultSet: ResultSet = preparedStatement.executeQuery()
return resultSet.next()
}
return false
}
fun addUser(login: String, email: String, password: String): Boolean {
connect()?.use { connection ->
val query = "INSERT INTO appmovie_users (login, email, password) VALUES (?, ?, ?)"
val preparedStatement = connection.prepareStatement(query)
preparedStatement.setString(1, login)
preparedStatement.setString(2, email)
preparedStatement.setString(3, password)
val rowsAffected = preparedStatement.executeUpdate()
return rowsAffected > 0
}
return false
}
}
This is file "RegistrationActivity.kt"
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
class RegistrationActivity : AppCompatActivity() {
private lateinit var viewModel: RegistrationViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.registration)
val userLogin = findViewById<EditText>(R.id.user_login)
val userEmail = findViewById<EditText>(R.id.user_email)
val userPass = findViewById<EditText>(R.id.user_pass)
val button = findViewById<Button>(R.id.button_reg)
val linkToAuth = findViewById<TextView>(R.id.link_to_auth)
val logoName = findViewById<TextView>(R.id.logoName)
val dbUrl = "jdbc:mysql://localhost:3306/database-users-movieapp"
val dbUser = "root"
val dbPassword = ""
val dbHelper = Dbhelper(dbUrl, dbUser, dbPassword)
viewModel = ViewModelProvider(this)[RegistrationViewModel::class.java]
button.setOnClickListener {
val login = userLogin.text.toString().trim()
val email = userEmail.text.toString().trim()
val pass = userPass.text.toString().trim()
if (login == "" || email == "" || pass == "") {
Toast.makeText(this, "Not all fields are filled", Toast.LENGTH_LONG).show()
} else {
if (dbHelper.isUserExists(login, email)) {
Toast.makeText(this, "User with the same login or email already exists", Toast.LENGTH_LONG).show()
} else {
viewModel.registerUser(login, email, pass)
}
}
}
}
} This is file "RegistrationViewModel"
package com.example.myapplication
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class RegistrationViewModel : ViewModel() {
private val DB_URL = "jdbc:mysql://localhost:3306/database-
users-movieapp"
private val DB_USER = "root"
private val DB_PASSWORD = ""
private val databaseHelper = Dbhelper(DB_URL, DB_USER, DB_PASSWORD)
val registrationResult = MutableLiveData<Boolean>()
fun registerUser(login: String, email: String, password: String) {
viewModelScope.launch(Dispatchers.IO) {
val isRegistrationSuccessful = databaseHelper.addUser(login, email, password)
registrationResult.postValue(isRegistrationSuccessful)
}
}
}
I tried quite a lot of things but nothing helps, still when I enter the registration data and click on the button to submit, nothing happens