首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java 与动态语言的1点东西

2012-11-15 
Java 与动态语言的一点东西。推荐下Js实现Sql 语句??package com.ctaoyu.framework.module_all.util.jsimp

Java 与动态语言的一点东西。

推荐下Js实现Sql 语句?

?

package com.ctaoyu.framework.module_all.util.js;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;/** *  * Js編譯引擎 *  * @author 彭秦進 rudys.eva@gmail.com *  * @version 1.0 *  */public class JSUtil {public static ScriptEngine jsEngine;static {initEngine();}/** * 初始化引擎 *  * @return */private static void initEngine() {// create a script engine managerScriptEngineManager factory = new ScriptEngineManager();/** * create engine by name ScriptEngine engine = factory.getEngineByName * ("JavaScript"); // create engine by name ScriptEngine engine = * factory.getEngineByExtension ("js"); // create engine by name * ScriptEngine engine = factory.getEngineByMimeType * ("application/javascript"); *  */jsEngine = factory.getEngineByName("JavaScript");// return engine;}/** * 解释代码 *  * @param jsString * @throws Exception  */public static void runEngine(String jsString) throws Exception {// TODO Auto-generated method stubJsDataSource sqltool = new JsDataSource();jsEngine.put("sqltool", sqltool);jsEngine.eval(jsString);}/** *  * @param args */public static void main(String[] args) {try {runEngine("");} catch (ScriptException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

?

?

package com.ctaoyu.framework.module_all.util.js;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import com.ctaoyu.framework.module_all.util.ds.ConnectFactory;/** *  * DataSource 供Js使用或其他腳本語言使用 *  * @author 彭秦进 rudys.eva@gmail.com *  * @version 1.0 *  */public class JsDataSource {/** * 數據庫連接 */private Connection conn;private ResultSet resultSet;/** * @return the resultSet */public ResultSet getResultSet() {return resultSet;}/** * @param resultSet *            the resultSet to set */public void setResultSet(ResultSet resultSet) {this.resultSet = resultSet;}/** * @return the conn */public Connection getConn() {return conn;}/** * @param conn *            the conn to set */public void setConn(Connection conn) {this.conn = conn;}/** * open connection *  * @throws Exception */private void openConnect() throws Exception {this.conn = ConnectFactory.currentConnect();}/** * close connection *  * @throws Exception */public void closeConnect() throws Exception {this.resultSet.close();this.conn.close();ConnectFactory.closeConnect();}/** * 執行Sql語句 *  * @param sql * @throws Exception */public void doSql(String sql) throws Exception {this.setResultSet(getConn().prepareStatement(sql).executeQuery());}/** *  * 取下一條記錄 *  * @throws SQLException *  */public int resultSetNext() throws SQLException {int flag;boolean nextB = getResultSet().next();if (nextB) {flag = 1;} else {flag = 2;}return flag;}/** * 去行記錄 *  * @param columnName * @return * @throws SQLException */public String getString(String columnName) throws SQLException {return getResultSet().getString(columnName);}/** * JsDataSource構造函數 *  * @throws Exception */public JsDataSource() throws Exception {openConnect();}}

?

?

JavaScript 脚本:

sqltool.dosql("select name from person");while(sqltool.resultSetNext()==1){println(sqltool.getString(name));}sqltool.closeConnect();

?

热点排行