This project is dead and no longer maintained! Users should use medusa instead!
This is my small personal jdbc utilities library to help me reduce retyping stuff mainly (ie. Creating connection, closing connection)
.
The user is still responsible for setting the query parameters.
Version 0.4 and 0.4.1 IS BUGGY and SHOULD NOT BE USED.
repositories {
jcenter()
}
dependencies {
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6' //depends on the driver you need
compile 'com.budinverse.utils:Personal-JDBC-Utils:<latest version>'
}
Assume there is this in your codebase (all examples)
data class Person(val email: String, val name: String){
constructor(resultSet: ResultSet):
this(resultSet[1], resultSet[2])
}
- Create a dbConfig.properties file with the following details inside and put it at root directory of project. You should be able to use the library already.
databaseUser = yourDatabaseUser
databasePassword = yourDatabasePassword
databaseUrl = yourDatbaseUrl
driver = yourDatabaseDriver
fun main(args: Array<String>) {
setConfigFile("filename.properties")
//can only be set once, setting more than once will
//result in error
}
- INSERT/UPDATE/DELETE Statements Assuming you have a Person data class and you want to insert into the person table.
/* Example */
fun insertPerson(person:Person) = manipulate("INSERT INTO person VALUES (?,?)",{
it[1] = person.email
it[2] = person.name
})
- Query Single Result.
/* Example */
fun getPersonByName(name:String) =
query("SELECT * FROM person WHERE name = ?", { it[1] = name }, ::Person)
- Query Multiple Results. Multiple results returns and arraylist of said object
/* Example */
fun getPersons() = queryMulti("SELECT * FROM person", ::Person)
val data = Person("[email protected]","budi")
val data2 = Person("[email protected]","budixd")
val txn = transaction {
manipulateTxn("INSERT INTO Person (email,name) VALUES (?,?)",it,{
it[1] = data.email
it[2] = data.name
})
manipulateTxn("UPDATE Person set email = ?, name = ?",it,{
it[1] = data2.email
it[2] = data2.name
})
}
//second run of this will result in rollback
if(txn == null)
println("Fail")
else
println("!Fail")
All implementations in experimental packages have not been tested for performance
query("SELECT * FROM person WHERE email = ? AND name = ?",{
it.setAllValues(data.email,data.name)
} ,::Person)