如何在jOOQ中使用Scala的字符串插值?

时间:2015-11-30 16:17:23

标签: scala jooq

我想在Scala中使用jOOQ的字符串插值功能,例如resultQuery"SELECT * FROM objects"

// setup connection
val con = DriverManager.getConnection(url, userName, password)

// create DSLContext
val dsl = DSL.using(con, SQLDialect.POSTGRES_9_4)

// normal use of DSLContext
dsl.resultQuery("SELECT * FROM objects")

// intented use of string interpolation
val q = resultQuery"SELECT * FROM objects"

val result = q.fetch()
println(result)

运行此代码(没有任何外部配置等)会导致以下异常:

Exception in thread "main" org.jooq.exception.DetachedException: Cannot execute query. No Connection configured
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:312)
    at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:305)
    ...

似乎SQLInterpolation正在使用默认设置。我尝试通过将DSLContextConfiguration设置为隐含来提供这些设置,但我仍然收到相同的例外情况:

implicit val dsl = DSL.using(con, SQLDialect.POSTGRES_9_4)
implicit val config = new DefaultConfiguration().derive(con)
                                                .derive(SQLDialect.POSTGRES_9_4)

如何正确地将我的设置(连接,方言等)提供给字符串插值?

1 个答案:

答案 0 :(得分:3)

您使用字符串插值创建的public class start { public static void main(String[] args) throws IOException{ for (int i = 5935; i < 10000; i++){ new test(i); } PrintStream printStream = new PrintStream(new FileOutputStream("E:\\ports.txt")); System.setOut(printStream); printStream.flush(); } } class test implements Runnable{ static String host = "localhost"; int t; Thread y; public test(int t2){ t = t2; y = new Thread(this); y.start(); } public void run() { try { Socket socket = new Socket(host, t); System.out.println("Port is alive - " + t); } catch (IOException e){ System.out.println("Port is dead... - " + t); } } } 对象不是“附加”的,即它不能单独执行。

换句话说,您应该像这样运行查询:

ResultQuery