In this blog, I will talk to you about a new framework in Ktor(Kotlin). if you don't know about Ktor you can watch my previous blog about Ktor-Click Here
In KTOR, You can connect with the database through Exposed library of Kotlin. So for the making of connection through the MySql database, you have to create a database.
1. I have created data as below SQL query.
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
CREATE DATABASE IF NOT EXISTS mytestdb; | |
USE mytestdb; | |
create table user(id int,name varchar(50),email varchar(50)); | |
INSERT INTO `user` (`uid`, `name`, `email`) VALUES ('1', 'anuj', 'anuj@gmail.com') | |
INSERT INTO `user` (`uid`, `name`, `email`) VALUES ('2', 'anuj', 'anuj@gmail.com') |
Now you can now try to connect through DB with help of Kotlin and expose library.
first of all, you have to add some line in build.gradle file to add expose and MySQL connector etc in your project inside dependency-
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
compile "org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.4" | |
compile 'org.jetbrains.exposed:exposed:0.8.5' | |
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6' | |
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25' | |
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1' | |
testCompile group: 'junit', name: 'junit', version: '4.12' |
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
maven { url 'https://kotlin.bintray.com/ktor' } | |
maven { url "https://dl.bintray.com/kotlin/kotlinx" } | |
maven { url "https://dl.bintray.com/kotlin/exposed" } |
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
buildscript { | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} | |
apply plugin: 'kotlin' | |
apply plugin: 'application' | |
group 'com.example' | |
version '0.0.1' | |
mainClassName = "io.ktor.server.netty.EngineMain" | |
sourceSets { | |
main.kotlin.srcDirs = main.java.srcDirs = ['src'] | |
test.kotlin.srcDirs = test.java.srcDirs = ['test'] | |
main.resources.srcDirs = ['resources'] | |
test.resources.srcDirs = ['testresources'] | |
} | |
repositories { | |
mavenLocal() | |
jcenter() | |
maven { url 'https://kotlin.bintray.com/ktor' } | |
maven { url "https://dl.bintray.com/kotlin/kotlinx" } | |
maven { url "https://dl.bintray.com/kotlin/exposed" } | |
} | |
dependencies { | |
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" | |
compile "io.ktor:ktor-server-netty:$ktor_version" | |
compile "ch.qos.logback:logback-classic:$logback_version" | |
testCompile "io.ktor:ktor-server-tests:$ktor_version" | |
compile "org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.4" | |
compile 'org.jetbrains.exposed:exposed:0.8.5' | |
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6' | |
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25' | |
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1' | |
testCompile group: 'junit', name: 'junit', version: '4.12' | |
} |
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
object User : Table("user") { | |
val id = integer("id") | |
val name = varchar("name", length = 50) | |
val email = varchar("email",length=50) | |
} | |
data class Users(val id: Int, val name: String, val email: String) |
In this blog, I will tell you about how to make a connection in JDBC driver the same as we do in java.
I am not using any external library like the connectionPool library in Spring framework of java.
Add below lines for connection here root is my database username and web is password-
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
fun initDB() { | |
val url = "jdbc:mysql://root:web@localhost:3306/mytestdb?useUnicode=true&serverTimezone=UTC" | |
val driver = "com.mysql.cj.jdbc.Driver" | |
Database.connect(url, driver) | |
} |
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
fun getTopuserData(): String { | |
var json: String = "" | |
transaction { | |
val res = User.selectAll().orderBy(User.id, false).limit(5) | |
val c = ArrayList<Users>() | |
for (f in res) { | |
c.add(Users(id = f[User.id], name = f[User.name], email = f[User.email])) | |
} | |
json = Gson().toJson(c); | |
} | |
return json | |
} |
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
fun main(args: Array<String>) { | |
initDB() | |
embeddedServer(Netty, 8080) { | |
routing { | |
get("/") { | |
call.respondText(getTopuserData(), ContentType.Text.Plain) | |
} | |
} | |
}.start(wait = true) | |
} |
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
package com.example | |
import com.google.gson.Gson | |
import io.ktor.application.* | |
import io.ktor.http.ContentType | |
import io.ktor.response.* | |
import io.ktor.request.* | |
import io.ktor.routing.get | |
import io.ktor.routing.routing | |
import io.ktor.server.engine.embeddedServer | |
import io.ktor.server.netty.Netty | |
import org.jetbrains.exposed.sql.Database | |
import org.jetbrains.exposed.sql.Table | |
import org.jetbrains.exposed.sql.selectAll | |
import org.jetbrains.exposed.sql.transactions.transaction | |
object User : Table("user") { | |
val id = integer("id") | |
val name = varchar("name", length = 50) | |
val email = varchar("email",length=50) | |
} | |
data class Users(val id: Int, val name: String, val email: String) | |
fun initDB() { | |
val url = "jdbc:mysql://root:web@localhost:3306/mytestdb?useUnicode=true&serverTimezone=UTC" | |
val driver = "com.mysql.cj.jdbc.Driver" | |
Database.connect(url, driver) | |
} | |
fun getTopuserData(): String { | |
var json: String = "" | |
transaction { | |
val res = User.selectAll().orderBy(User.id, false).limit(5) | |
val c = ArrayList<Users>() | |
for (f in res) { | |
c.add(Users(id = f[User.id], name = f[User.name], email = f[User.email])) | |
} | |
json = Gson().toJson(c); | |
} | |
return json | |
} | |
fun main(args: Array<String>) { | |
initDB() | |
embeddedServer(Netty, 8080) { | |
routing { | |
get("/") { | |
call.respondText(getTopuserData(), ContentType.Text.Plain) | |
} | |
} | |
}.start(wait = true) | |
} | |
If you like this blog you can follow our blog for more awesome content.
To know more about how to create API in Ktor- ClickHere
project code-CLICK HERE
1 Comments
can i have a copy of your entire repo?
ReplyDelete