日期:2014-05-16 浏览次数:20464 次
整理以前写的东西的时候找到的,一个小Demo,不过现在看来也就这一小段值得整理出来,也许会有人能用到。
展示了用Jruby调用Java连接ODBC的方法,以及使用excel当数据源时sql的写法。
include Java #演示登录,即从Excel中select,req里面提交了uid和password两个字段。 class Login < HTTPServlet::AbstractServlet def do_POST (req, res) res.status = 200 res['Content-Type'] = 'text/html; charset=gbk' java.lang.Class.forName 'sun.jdbc.odbc.JdbcOdbcDriver' con = java.sql.DriverManager.getConnection 'jdbc:odbc:excel_db','','' stmt = con.createStatement rs = stmt.executeQuery "select * from [sheet1$] where uid='#{req.query['uid']}'" if rs.next and rs.getString('password') == req.query['password'] res.body=ERB.new( IO.read(Dir::pwd+'/AfterLogin.erb') ).result(binding) else res.body='Excel里没您老的注册信息,要么就是您老打错密码了。' end ###演示获得所有列的名字### meta_data = rs.getMetaData column_labels = [] meta_data.getColumnCount.times { |i| column_labels.push(meta_data.getColumnLabel i+1) } ########################## rs.close stmt.close con.close end end #演示更新Excel中的所谓Message,req里提交了msg_name字段。 class UpdateMsg < HTTPServlet::AbstractServlet def do_POST (req,res) java.lang.Class.forName 'sun.jdbc.odbc.JdbcOdbcDriver' con = java.sql.DriverManager.getConnection 'jdbc:odbc:excel_db','','' stmt = con.createStatement msg_name = req.query['msg_name'] stmt.executeUpdate "update [sheet1$] set #{msg_name}='#{req.query[msg_name]}' where uid='#{req.query['uid']}'" stmt.close con.close res.set_redirect(HTTPStatus::MovedPermanently, '/ShowUpdateResult') end end?
?