'Displaying database from ResultSet in HTML table

First of all, I know that there is a question very similar to this one but the case and solution didn't seem to work or apply for me, I apologize if I am wrong!

I want to display a database as an HTML table using Kotlin and POSTGRESQL. I am new to databases and backend overall, I managed to create a function that executes a SELECT * FROM statement and retrieves the information using resultSet and a while loop. I then stored the information inside a list and I sent it as a response.

This works fine but I want to make it look a bit cleaner, right now it is hard to read and messy, so I thought an html table would make sense. What would be the best way to approach this?

fun readHistory(dataSource: DataSource): List<History> {
    dataSource.connection.use { c ->
        var listOfOperations = mutableListOf<History>()
        val statement = c.createStatement()
        if(statement.execute("SELECT * FROM calculator.history ORDER BY created_at")) {
            val resultSet = statement.resultSet
            while (resultSet.next()) {
                val history = History(id = resultSet.getLong("id"),
                operation = resultSet.getString("operation"),
                created_at = resultSet.getDate("created_at"),
                n1 = resultSet.getDouble("n1"),
                n2 = resultSet.getDouble("n2"))
                listOfOperations.add(history)

            }
        }
        return listOfOperations
    }
}

Sending the response:

val historyList = readHistory(dataSource)
http.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
http.getResponseSender().send(historyList.toString()

I have been trying using kotlinx html and sending this as a response but I don't know how to add the readHistory function inside the html code

val text = buildString {
    appendLine("<!DOCTYPE html>")
    appendHTML().html {
        head {
            style {
                unsafe {
                    raw("""
                        .historyTable, .tdTest, .trTest {
                             border:1px solid black;
                            }
                        """)
                }
            }
        }
        body {
            table("historyTable"){
                tr("trTest"){
                    td("tdTest")
                    +"Test"
                    td("tdTest")
                    +"Test2"
                    td("tdTest")
                    +"Test3"
                }

            }

        }
    }
    appendLine()
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source