dimanche 17 janvier 2016

SQLException: unknown database with SQLite and Scala

I have object DictionarySqlLiteDao which should create table of it not exist during its initialization. But I have exception despite the fact that the database is created in file system. Help me to figure out the reason of it. Here is my code:

object DictionarySqlLiteDao extends Dao[Word] {

private val CREATE_WORD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS thelper.WORD " +
        "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
        "WORD TEXT NOT NULL, " +
        "UKR TEXT NOT NULL, " +
        "ENG TEXT NOT NULL, " +
        "EXAMPLE TEXT)"
private val SAVE_WORD_SQL = "INSERT INTO WORD(WORD, UKR, ENG, EXAMPLE" +
        "VALUES(?, ?, ?, ?)"
private val connection: Connection = {
    Class.forName("org.sqlite.JDBC")
    DriverManager.getConnection("jdbc:sqlite:thelper")
}

{
    connection.createStatement().executeUpdate(CREATE_WORD_TABLE_SQL)
}

override def save(word: Word): Word = {
    val statement = connection.prepareStatement(SAVE_WORD_SQL, Statement.RETURN_GENERATED_KEYS)
    statement.setString(1, word.word)
    statement.setString(2, word.translation("ukr"))
    statement.setString(3, word.translation("eng"))
    statement.setString(4, word.example.orNull)
    statement.executeUpdate()
    val id = statement.getGeneratedKeys.getInt(1)
    word.copy(id = id)
}

def main(args: Array[String]) {
    println(connection == null) // here I get false
    println(connection.createStatement().execute("select date('now')")) // here I get true
    val w = save(Word(-1, "germanWord", Map("ukr" -> "ukrTranslation", "eng" -> "engTranslation"), Option.empty[String])) // and here I get an Exception
    println(w)
}

}

And here is an exception I get:

Exception in thread "main" java.lang.ExceptionInInitializerError
at thelper.Dictionary.dao.DictionarySqlLiteDao.main(DictionarySqlLiteDao.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.sql.SQLException: unknown database thelper
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at thelper.Dictionary.dao.DictionarySqlLiteDao$.<init>(DictionarySqlLiteDao.scala:23)
at thelper.Dictionary.dao.DictionarySqlLiteDao$.<clinit>(DictionarySqlLiteDao.scala)
... 6 more

Aucun commentaire:

Enregistrer un commentaire