Java Script Language call notes (Jython, jruby, groovy)

Source: Internet
Author: User

There are two methods
1. JSR 223 specifications are implemented after Java SE 6
Java code:

Scriptenginemanager factory = new scriptenginemanager (); scriptenginemanager scriptengine = factory. getenginebyname ("JavaScript"); // or "JS" scriptengine. eval (CODE); // execute a script. The code is JS Code.

Easy to call scripts

2. You can use the Java integration methods provided by the script language.

Jython Integration
Use jsr223:
To download the Jython package, jsr223 has been implemented.
(Recommended download on the official website, in the installation directory has Jython. jar, http://repo2.maven.org/maven2/org/python/jython/2.5.0/ here also has, but this package does not implement jsr223, see the package under the memory does not exist org. Python. jsr223)

Scriptenginemanager factory = new scriptenginemanager (); scriptenginemanager scriptengine = factory. getenginebyname ("Python"); // or "Jython" scriptengine. eval (CODE );

You can use pythoninterpreter to call the exec (string code) method:

PythonInterpreter interpreter = new PythonInterpreter();interpreter.exec(code);

Access Database
Use JDBC:

from oracle.jdbc.driver import OracleDriverfrom java.sql import DriverManagerusername = 'hr'password = '123456'url = 'jdbc:oracle:thin:@localhost:1521:XE'driver = OracleDriver()DriverManager.registerDriver(driver)conn = DriverManager.getConnection(url, username, password)stmt = conn.createStatement()sql = "select salary from EMPLOYEES t where t.salary<2300"rs = stmt.executeQuery(sql)while (rs.next()): print rs.getInt('salary')rs.close()stmt.close()

Result:
2200
2100
2200

Use zxjdbc:

from com.ziclix.python.sql import zxJDBCurl = 'jdbc:oracle:thin:@localhost:1521:XE'username = 'hr'password = '123456'driverName = 'oracle.jdbc.driver.OracleDriver'mysqlConn = zxJDBC.connect(url,username, password,driverName)cursor = mysqlConn.cursor()     cursor.execute("select last_name from EMPLOYEES t where t.salary<2300");#print cursor.fetchone()list = cursor.fetchall()for record in list:    print "name:"+record[0]#print cursor.description[0]#print cursor.description[1]

Result:
Name: Mike
Name: Olson
Name: philtanker

The Chinese content found from the database is normal.
All Chinese characters in the Code are garbled or thrown exceptions, which are not resolved.

Integration with jruby
Use jsr223: Java code

Scriptenginemanager factory = new scriptenginemanager (); scriptenginemanager scriptengine = factory. getenginebyname ("jruby"); // or "Ruby" scriptengine. eval (CODE );

 Access Database

Ruby code

Require 'javalang module include_package "Java. lang "endmodule semantic QL include_package 'java. SQL 'enabledegin username = 'hr' Password = '000000' url = 'jdbc: oracle: thin: @ localhost: 123456: XE 'drivername = 'oracle. JDBC. driver. oracledriver 'javalang: class. forname (drivername ). newinstance conn = javasql: drivermanager. getconnection (URL, username, password) stmt = Conn. createstatement SQL = "select last_name from employees t where T. salary <2300 "rs = stmt.exe cutequery (SQL) while (RS. next) do puts "name:" + Rs. getstring ("last_name") end Rs. close stmt. close Conn. close () rescue javalang: classnotfoundexception puts "classnotfoundexception" rescue effecql: sqlexception puts "sqlexception" End

 

Result:
Name: zookeeper
Name: Olson
Name: philtanker

 

The Chinese content found from the database is garbled.
The Chinese characters in the Code are normal.

 

Integration with groovy
Use jsr223:

Java code

Scriptenginemanager factory = new scriptenginemanager (); scriptenginemanager scriptengine = factory. getenginebyname ("groovy"); // or "groovy" scriptengine. eval (CODE );

Use policyshell:

Java code

GroovyShell shell = new GroovyShell();Script script = shell.parse(code);Object result = script.run();

Access Database

Groovy code

 

Import groovy. SQL. sqldef username = 'hr' def Password = '000000' def url = 'jdbc: oracle: thin: @ localhost: 123456: XE 'def drivername = 'oracle. JDBC. driver. oracledriver 'def SQL = SQL. newinstance (URL, username, password, drivername) SQL. eachrow ("select last_name from employees t where T. salary <2300 ") {println" Name: $ {It. last_name }"}

Result:
Name: Mike
Name: Olson
Name: philtanker

 

An exception occurred while using groovy.
Exception in thread "Main" Java. lang. verifyerror: (Class: groovy/runtime/metaclass/Java/util/arraylistmetaclass, method: Super $2 $ invokemethod Signature: (ljava/lang/class; ljava/lang/object; ljava/lang/string; [ljava/lang/object; zz) ljava/lang/object;) Illegal use of nonvirtual function call
It took a long time to solve this exception.
Because there is a json-lib-2.1.jar (possibly named json-lib-2.1-jdk15.jar) in the original project, this package is used to handle JSON, conflicts with policy1.7.5, update to json-lib-2.3.jar
(JSON-LIB contains some content processed during groovy runtime)

 

Turn from: http://yinxvxv.iteye.com/blog/811328 above there are bags very good

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.