Giter Site home page Giter Site logo

personal-jdbc-utils's Introduction

This project is dead and no longer maintained! Users should use medusa instead!

Download MIT Licence

Personal-JDBC-Utils

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.

Note

Version 0.4 and 0.4.1 IS BUGGY and SHOULD NOT BE USED.

Gradle

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>'
}

How to use?

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])
}
  1. 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
}
  1. 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
})
  1. Query Single Result.
/* Example */
fun getPersonByName(name:String) = 
    query("SELECT * FROM person WHERE name = ?", { it[1] = name }, ::Person)
  1. Query Multiple Results. Multiple results returns and arraylist of said object
/* Example */
fun getPersons() = queryMulti("SELECT * FROM person", ::Person)

Transaction Support

    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")

Experimental

All implementations in experimental packages have not been tested for performance

Set all

query("SELECT * FROM person WHERE email = ? AND name = ?",{
        it.setAllValues(data.email,data.name)
    } ,::Person)

personal-jdbc-utils's People

Contributors

zeon256 avatar euwbah avatar

Stargazers

 avatar He Shuimei avatar

Watchers

 avatar

Forkers

euwbah

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.