I tried few different things as the one below but ...
# scripting
r
I tried few different things as the one below but no luck From the SB app
Copy code
val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
engine.put("jdbcTemplate",jdbcTemplate)
engine.eval("""@file:Import("/test.kts")""".trimIndent())
From the test.kts script
Copy code
val template = bindings.get("jdbcTemplate")
i
I guess that you may have problems with import. It is a bit tricky and experimental. In particular you need to use same filename extension for the main and imported scripts, in this case the file should be named "test.main.kts". Otherwise it should work without using
bindings
too. See e.g. tests and examples here: https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/jsr223/jsr223-simple/src/test/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/simple/test/simpleJsr223Test.kt#L32
r
Thank you @ilya.chernikov. Renaming to main.kts solved the problem to pass a variable but still don't work. eg : this works in the kt but not kts Thank you @ilya.chernikov. Renaming to main.kts solved the problem to pass a variable but still don't work. eg : this works in the kt but not kts
Copy code
val count = jdbcTemplate.queryForObject("Select count(*) from billionaires", Int::class.java)
println(count)
kt no issues, kts gives
Copy code
Caused by: javax.script.ScriptException: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun <reified T> JdbcOperations.queryForObject(sql: String): TypeVariable(T) defined in org.springframework.jdbc.core
public inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<out Any>): TypeVariable(T)? defined in org.springframework.jdbc.core
public inline fun <reified T> JdbcOperations.queryForObject(sql: String, vararg args: Any, crossinline function: (ResultSet, Int) -> TypeVariable(T)): TypeVariable(T) defined in org.springframework.jdbc.core
public inline fun <reified T> JdbcOperations.queryForObject(sql: String, args: Array<out Any>, argTypes: IntArray): TypeVariable(T)? defined in org.springframework.jdbc.core
Interesting enough, it works fine with plain jdbc (using kt and kts files). For some reason in the previous example the same code worked with the kt but not kts
Copy code
val selectStmt: Statement = con.createStatement()
val rs: ResultSet = selectStmt
    .executeQuery("Select name, id from billionaires")
while (rs.next()){
    println(rs.getString(1))
    println(rs.getLong(2))
}