romishbek

ee
mail@pastecode.io avatar
unknown
kotlin
2 months ago
3.5 kB
1
Indexable
Never
//main.kt

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application



@Composable
@Preview
fun LoginScreen(db: DB) {
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    Column {
        OutlinedTextField(
            value = username,
            onValueChange = { username = it },
            label = { Text("Имя пользователя") }
        )
        OutlinedTextField(
            value = password,
            onValueChange = { password = it },
            label = { Text("Пароль") },
            visualTransformation = PasswordVisualTransformation(),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
        )
        Button(onClick = {
            if (db.checkCredentials(username, password)) {
                println("Успешный вход в систему!")
            } else {
                println("Ошибка входа в систему!")
            }
        }) {
            Text("Войти")
        }
    }
}

@Composable
@Preview
fun App(db: DB) {
    MaterialTheme {
        LoginScreen(db)
    }
}

fun main() = application {
    val db = DB()
    db.connect()

    Window(onCloseRequest = {
        //db.disconnect()
        exitApplication()
    }) {
        App(db)
    }
}

//db.kt
import java.sql.Connection
import java.sql.DriverManager

class DB {
    private val url = "jdbc:mysql://127.0.0.1:3306/bank"
    private val username = "root"
    private val password = "root"

    var connection: Connection? = null
        private set

    fun connect() {
        try {
            connection = DriverManager.getConnection(url, username, password)
            println("Подключение к базе данных успешно установлено!")
        } catch (e: Exception) {
            println("error")
            e.printStackTrace()
        }
    }

    fun disconnect() {
        try {
            connection?.close()
            println("Подключение к базе данных успешно закрыто!")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun checkCredentials(inputUsername: String, inputPassword: String): Boolean {
        var isValidCredentials = false

        try {
            val statement = connection?.createStatement()
            val resultSet = statement?.executeQuery("SELECT * FROM UserLogin WHERE Username = '$inputUsername'")

            if (resultSet?.next() == true) {
                val passwordHash = resultSet.getString("PasswordHash")
                isValidCredentials = passwordHash == inputPassword
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return isValidCredentials
    }
}
Leave a Comment