SQLite ResultSet.next()上的ArrayIndexOutOfBoundsException

时间:2015-02-16 13:14:06

标签: java sqlite odbc rhino

我想从表中获取单个值。但是当我在下一行使用Resultset.next()时:

if(rs.next()) {
    return rs.getString("value");
}

我得到java.lang.ArrayIndexOutOfBoundsException: 19567。谁能告诉我为什么?

public String getString(String key, String defaultValue) {
    Connection con  = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        con = connect();
        ps  = con.prepareStatement("SELECT `value` FROM `" + this.table  + "` WHERE `name`=? AND `app`=? LIMIT 1");
        ps.setString(1, key);
        ps.setString(2, "APP NAME");
        rs = ps.executeQuery();
        if(rs.next()) {
            return rs.getString("value");
        }
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
       if(con != null) {
           try {
               con.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
    }

    return defaultValue;
}

private Connection connect() {
    Connection connection = null;

    try {
      Class.forName("org.sqlite.JDBC");
      connection        = DriverManager.getConnection("jdbc:sqlite:.persistence");
      Statement stmt    = connection.createStatement();

      // AppPersistence
      stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `app` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `app` VARCHAR(255), `name` VARCHAR(255), `value` TEXT);");

      // UserPersistence
      stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_id` VARCHAR(255), `name` VARCHAR(255), `value` TEXT);");

      // Users
      stmt.executeUpdate("CREATE TABLE IF NOT EXISTS users (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `nickname` VARCHAR(255), `age` INT(11), `gender` VARCHAR(255) DEFAULT('Unknown'));");
    } catch(Exception e) {
        e.printStackTrace();
    }

    return connection;
}

栈跟踪

java.lang.ArrayIndexOutOfBoundsException: 19567
    at org.mozilla.javascript.net.sf.retrotranslator.runtime.asm.ClassReader.accept(ClassReader.java:342)
    at org.mozilla.javascript.net.sf.retrotranslator.runtime.asm.ClassReader.accept(ClassReader.java:300)
    at org.mozilla.javascript.net.sf.retrotranslator.runtime.impl.ClassDescriptor.<init>(ClassDescriptor.java:67)
    at org.mozilla.javascript.net.sf.retrotranslator.runtime.impl.ClassDescriptor.getInstance(ClassDescriptor.java:82)
    at org.mozilla.javascript.net.sf.retrotranslator.runtime.impl.MethodDescriptor.getInstance(MethodDescriptor.java:120)
    at org.mozilla.javascript.net.sf.retrotranslator.runtime.java.lang.reflect._Method.isVarArgs(_Method.java:96)
    at org.mozilla.javascript.jdk15.VMBridge_jdk15.isVarArgs(VMBridge_jdk15.java:33)
    at org.mozilla.javascript.MemberBox.init(MemberBox.java:44)
    at org.mozilla.javascript.MemberBox.<init>(MemberBox.java:32)
    at org.mozilla.javascript.JavaMembers.reflect(JavaMembers.java:455)
    at org.mozilla.javascript.JavaMembers.<init>(JavaMembers.java:43)
    at org.mozilla.javascript.JavaMembers.lookupClass(JavaMembers.java:807)
    at org.mozilla.javascript.NativeJavaObject.initMembers(NativeJavaObject.java:54)
    at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:44)
    at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:34)
    at org.mozilla.javascript.WrapFactory.wrapAsJavaObject(WrapFactory.java:115)
    at org.mozilla.javascript.WrapFactory.wrap(WrapFactory.java:72)
    at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:236)
    at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1473)
    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
    at com.knuddels.apps.core.RhinoApp.call(RhinoApp.java:150)
    at de.mca.core.Main.main(Main.java:83)

0 个答案:

没有答案