浏览 1692 次
锁定老帖子 主题:Java 与动态语言的一点东西。
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-23
最后修改:2010-01-23
推荐下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 manager ScriptEngineManager 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 stub JsDataSource 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 block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.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();
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |