Skip to content

Instantly share code, notes, and snippets.

@greghelton
Last active July 30, 2018 06:42
Show Gist options
  • Save greghelton/6f3146a2a2f89b0a85e3 to your computer and use it in GitHub Desktop.
Save greghelton/6f3146a2a2f89b0a85e3 to your computer and use it in GitHub Desktop.
DataTables.net plus JRuby and Sinatra ... for instructions, see http://opensourcetips.blogspot.com/2014/09/datatablesnet-jruby-sinatra.html
require 'sinatra'
require 'sinatra/reloader' if development?
require 'lib/jt400.jar'
require 'yaml'
require 'json'
require 'logger'
$log = Logger.new('logs/log.txt','weekly')
$log.level = Logger::DEBUG
set :public_folder, 'public'
db = YAML.load_file '/config/db.yml'
Java::JavaClass.for_name db['driver']
connection = java.sql.DriverManager.get_connection(db['dburl'], db['username'], db['password'])
get "/" do
redirect 'examples/index.html'
end
get '/employees' do
rows = []
query = %q{SELECT NAME, TITLE, OFFICE, EXTENSION, STARTDATE, SALARY from EMPLOYEE}
statement = connection.prepare_statement(query)
results = statement.execute_query
meta = results.meta_data
while results.next
row = []
(1..meta.column_count).each do |i|
$log.debug meta.column_type(i)
row[i - 1] = case meta.column_type(i)
when -6, -5, 5, 4
results.get_int(i).to_s
when 91
results.get_date(i).to_s
when 92
results.get_time(i)
when 93
results.get_timestamp(i)
when 2, 3, 6
BigDecimal.new(results.get_string(i).to_s)
when 1, -15, -9, 12
results.get_string(i).to_s
else
results.get_string(i)
end
end
rows << row
$log.debug rows
end
doc = {}
doc['data'] = rows
$log.debug doc
results.close
statement.close
content_type 'application/json'
doc.to_json
end
dburl: jdbc:as400://SEWDEV
driver: com.ibm.as400.access.AS400JDBCDriver
username: myuserid
password: mypassword
In public/examples/data_sources/ajax.html, change the ready function from this:
$(document).ready(function() {
$('#example').dataTable( {
"ajax": '../ajax/data/arrays.txt'
} );
} );
to this:
$(document).ready(function() {
$('#example').dataTable( {
"ajax": '/employees'
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment