使用Nashorn在java程序中执行java代码

时间:2017-04-14 07:57:14

标签: java nashorn

我编写了一个java代码,它将从表中获取数据并显示输出。这基本上是我想通过Nashorn运行的脚本。

以下是我尝试的代码

public static void main(String args[]) throws ScriptException{

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

    String script3 = "count = 0; "
            + "keyList = new java.util.ArrayList; "
            + "valueList = new java.util.ArrayList;"
            + "keyList.add('username'); "
            + "keyList.add('password'); "
            + "valueList.add('admin'); "
            + "valueList.add('admin');"
            + "java.lang.System.out.println('keyList :: ' + keyList);   "
            + "java.lang.System.out.println('KeyValue :: ' + valueList);"
            + "sql = \" select * from credentials where 'isActive'='1'\";"  
            + "java.lang.System.out.println('keyList.size() :: ' + keyList.size());"
            + "for (i = 0; i < keyList.size(); i++){"
            + " sql += and \\' + keyList.get(i) + \\' = \\' + valueList.get(i) + \\';}"                                                     
            + "java.lang.System.out.println('Search SQL : ' + sql); "
            + "con = GetConnected.connectToDatabase('my_db');"
            + "ps = con.prepareStatement(sql); "
            + "ps.setString(1, \"1\");"
            + "rs = ps.executeQuery(); "
            + "if (rs.isBeforeFirst()) {"
            + " count=1; "
            + " java.lang.System.out.println('Data is present in database'); "
            + "}"
            + "else "
            + " java.lang.System.out.println('Data is not present in database'); "
            + " rs.close(); " + " st.close(); " + " con.close(); ";

    engine.eval(script3);
}

我得到的错误是

Exception in thread "main" javax.script.ScriptException: <eval>:1:904 Missing close quote
count = 0; keyList = new java.util.ArrayList; valueList = new java.util.ArrayList;keyList.add('username'); keyList.add('password'); valueList.add('admin'); valueList.add('admin');java.lang.System.out.println('keyList :: ' + keyList);   java.lang.System.out.println('KeyValue :: ' + valueList);sql = " select * from credentials where 'isActive'='1'";java.lang.System.out.println('keyList.size() :: ' + keyList.size());for (i = 0; i < keyList.size(); i++){  sql += and \' + keyList.get(i) + \' = \' + valueList.get(i) + \';}java.lang.System.out.println('Search SQL : ' + sql); con = GetConnected.connectToDatabase('my_db');ps = con.prepareStatement(sql); ps.setString(1, "1");rs = ps.executeQuery(); if (rs.isBeforeFirst()) { count=1;  java.lang.System.out.println('Data is present in database'); }else  java.lang.System.out.println('Data is not present in database');  rs.close();  st.close();  con.close(); 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ^ in <eval> at line number 1 at column number 904
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:467)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:534)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:521)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:399)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at com.engine.nashorn.Test.main(Test.java:87)
Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:904 Missing close quote
count = 0; keyList = new java.util.ArrayList; valueList = new java.util.ArrayList;keyList.add('username'); keyList.add('password'); valueList.add('admin'); valueList.add('admin');java.lang.System.out.println('keyList :: ' + keyList);   java.lang.System.out.println('KeyValue :: ' + valueList);sql = " select * from credentials where 'isActive'='1'";java.lang.System.out.println('keyList.size() :: ' + keyList.size());for (i = 0; i < keyList.size(); i++){  sql += and \' + keyList.get(i) + \' = \' + valueList.get(i) + \';}java.lang.System.out.println('Search SQL : ' + sql); con = GetConnected.connectToDatabase('my_db');ps = con.prepareStatement(sql); ps.setString(1, "1");rs = ps.executeQuery(); if (rs.isBeforeFirst()) { count=1;  java.lang.System.out.println('Data is present in database'); }else  java.lang.System.out.println('Data is not present in database');  rs.close();  st.close();  con.close(); 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ^
at jdk.nashorn.internal.parser.Lexer.error(Lexer.java:1706)
at jdk.nashorn.internal.parser.Lexer.scanString(Lexer.java:988)
at jdk.nashorn.internal.parser.Lexer.lexify(Lexer.java:1615)
at jdk.nashorn.internal.parser.AbstractParser.getToken(AbstractParser.java:132)
at jdk.nashorn.internal.parser.AbstractParser.nextToken(AbstractParser.java:211)
at jdk.nashorn.internal.parser.AbstractParser.nextOrEOL(AbstractParser.java:170)
at jdk.nashorn.internal.parser.AbstractParser.next(AbstractParser.java:157)
at jdk.nashorn.internal.parser.Parser.parse(Parser.java:281)
at jdk.nashorn.internal.parser.Parser.parse(Parser.java:249)
at jdk.nashorn.internal.runtime.Context.compile(Context.java:1286)
at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1253)
at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:625)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:532)
... 5 more

我认为将keyList和valueList值添加到sql查询的地方就是问题。

sql += and \\' + keyList.get(i) + \\' = \\' + valueList.get(i) + \\';}"

+将继续添加,直到脚本结束。

0 个答案:

没有答案