Last active
September 4, 2018 11:46
-
-
Save oshai/0f75dc35d33b9904eb1bd82ee4461220 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.jasync.sql.db.Configuration | |
import com.github.jasync.sql.db.Connection | |
import com.github.jasync.sql.db.QueryResult | |
import com.github.jasync.sql.db.RowData | |
import com.github.jasync.sql.db.mysql.MySQLConnection | |
import com.github.jasync.sql.db.mysql.util.URLParser | |
import com.github.jasync.sql.db.util.head | |
import com.github.jasync.sql.db.util.map | |
import java.util.concurrent.CompletableFuture | |
import java.util.concurrent.TimeUnit | |
object BasicExample { | |
fun main(args: Array<String>) { | |
val configuration = URLParser.parse("jdbc:mysql://localhost:3306/my_database?username=mysql&password=somepassword") | |
val connection: Connection = MySQLConnection(configuration) | |
connection.connect().get(5, TimeUnit.SECONDS) | |
val future: CompletableFuture<QueryResult> = connection.sendQuery("SELECT 0") | |
val mapResult: CompletableFuture<Any?> = future.map { queryResult -> | |
val resultSet = queryResult.rows | |
when { | |
resultSet != null -> { | |
val row: RowData = resultSet.head | |
row(0) | |
} | |
else -> -1 | |
} | |
} | |
val result = mapResult.get(5, TimeUnit.SECONDS) | |
println(result) | |
connection.disconnect() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment