(servlet (cmd pw name age query) ;; ;; dbdemo.servlet ;; author: Tim Hickey ;; date: 19 July 2002 ;; ;; This is a demo of a database servlet. ;; It shows how to ;; * reset a database ;; * insert values from a form into a database ;; * query a database and present the results ;; * run a general query from the user (this is dangerous!) ;; Note: when you insert text into a database you should quote it appropriatedly using toSQL ;; (define lib (tryCatch dbquery (lambda(e) (load "webapps/jscheme/lib/db.scm")))) ;; load library if dbquery not defined... (define servletpassword "abc777*") ;; the user must enter this to execute general queries... (define isLocalUser #t) ;(equal? (.getRemoteAddr request) "127.0.0.1")) ;; next we generate the header for all pages { Database demo
[ ;; we use a case to determine which kind of page to generate, ;; we enclose it in a tryCatch to catch any errors that may occur while generating the page (tryCatch (case cmd ((#null) {Welcome to the Database Demo Page}) (("reset") {Are you sure you want to reset the database?
You will lose all data currently in the database,
and there is no way to get it back!
password


}) (("doreset") (if (equal? pw servletpassword) (begin (tryCatch (dbquery {drop table test}) (lambda(e) #t)) ;; ignore errors while dropping the table. (dbquery { create table test(name varchar, age integer); insert into test values('jojo',32); insert into test values('gus',26); insert into test values('kayla',7); insert into test values('simon',3);}) {database has been reset to original state}) {Wrong password})) (("show") {

The Test Database

[(trs "" (dbquery "select * from test"))]

as of [(Date.)] }) (("restartdb") (if isLocalUser (begin (.close dbconnection) {OK. The database has been restarted.}) {Database can only be reset locally!})) (("eval") (load "jlib/JLIB.scm") (load "jlib/demo/Demorunner.scm") (main (list->array String.class ())) {OK}) (("insert") {

Insert New Entry in table

name
age

}) (("doinsert") (dbquery {insert into test values([(toSQL name)],[(.intValue age)])}) {(name:[name], age:[age]) inserted into database
}) (("query") {

Query the database

SQL query
password

}) (("doquery") (let ((results (cond ((and #f (not(equal? pw servletpassword))) (list (list {Wrong Password}))) ((not isLocalUser) (list (list {Access forbidden for [(.getRemoteAddr request)]}))) (else (dbquery query))))) {The query:

[query]

has been evaluated and has returned with

[(trs "" results)]
})) (else {unknown command: [cmd]}) ); this closes the case expression, and we can now generate the footer for all pages (lambda(e) ; here is where we handle errors generated in the case expression {ERROR in the evaluation of command "[cmd]"
[e]})) ]



Select an action:
} ) ; finally we close the servlet!