Brazil + JDBC Juin 2001, douin@cnam.fr http://jfod.cnam.fr/tp_cdi/douin/



Similar documents
Thursday, February 7, DOM via PHP

Java Server Pages and Java Beans

TP : Système de messagerie - Fichiers properties - PrepareStatement

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Remote Method Invocation

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

The JAVA Way: JDBC and SQLJ

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

POB-JAVA Documentation

Licence Informatique Année Exceptions

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

TP JSP : déployer chaque TP sous forme d'archive war

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

Apéndice C: Código Fuente del Programa DBConnection.java

JDBC (Java / SQL Programming) CS 377: Database Systems

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Real SQL Programming 1

SQL and Java. Database Systems Lecture 19 Natasha Alechina

LSINF1124 Projet de programmation

Langages Orientés Objet Java

TP N 10 : Gestion des fichiers Langage JAVA

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

2. Follow the installation directions and install the server on ccc

Applets, RMI, JDBC Exam Review

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Security Module: SQL Injection

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Note concernant votre accord de souscription au service «Trusted Certificate Service» (TCS)

CSS : petits compléments

Introduction au BIM. ESEB Seyssinet-Pariset Economie de la construction contact@eseb.fr

Mini Project Report ONLINE SHOPPING SYSTEM

Configuration Guide. SafeNet Authentication Service. SAS Agent for AD FS

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

1 SQL Data Types and Schemas

A table is a collection of related data entries and it consists of columns and rows.

Personnalisez votre intérieur avec les revêtements imprimés ALYOS design

Java and Microsoft Access SQL Tutorial

Novell Identity Manager

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Annexe - OAuth Introduction. Xavier de Rochefort xderoche@labri.fr - labri.fr/~xderoche 15 mai 2014

Memo bconsole. Memo bconsole

RAPPORT FINANCIER ANNUEL PORTANT SUR LES COMPTES 2014

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...


CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

Short Form Description / Sommaire: Carrying on a prescribed activity without or contrary to a licence

Archived Content. Contenu archivé

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

Solaris 10 Documentation README

Government Girls Polytechnic, Bilaspur

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

Introduction ToIP/Asterisk Quelques applications Trixbox/FOP Autres distributions Conclusion. Asterisk et la ToIP. Projet tuteuré

Tutorial for Spring DAO with JDBC

Programmation RMI Sécurisée

Administrer les solutions Citrix XenApp et XenDesktop 7.6 CXD-203

Tool & Asset Manager 2.0. User's guide 2015

Sun Management Center Change Manager Release Notes

Microsoft SQL Server Features that can be used with the IBM i

Web Development and Core Java Lab Manual V th Semester

Introduction Les failles les plus courantes Les injections SQL. Failles Web. Maxime Arthaud. net7. Jeudi 03 avril 2014.

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

CS346: Database Programming.

Computer Security - Tutorial Sheet 3: Network & Programming Security

Sample HP OO Web Application

STAGE YOGA & RANDONNEES à MADERE

CS/CE 2336 Computer Science II

A Brief Introduction to MySQL

Prof. Edwar Saliba Júnior

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";! SET time_zone = "+00:00";!

Corrigés des exercices SQL pour MySQL

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

Apache Derby Security. Jean T. Anderson

Java Programming. JDBC Spring Framework Web Services

Documentation technique

Chapter 2 Introduction to Java programming

Java MySQL Connector & Connection Pool

JWIG Yet Another Framework for Maintainable and Secure Web Applications

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Stockage distribué sous Linux

Security Code Review- Identifying Web Vulnerabilities

Aucune validation n a été faite sur l exemple.

Comparing the Effectiveness of Penetration Testing and Static Code Analysis

Oracle WebLogic Server

TP : Configuration de routeurs CISCO

Transcription:

Brazil + JDBC Juin 2001, douin@cnam.fr http://jfod.cnam.fr/tp_cdi/douin/ version du 26 Mai 2003 : JDBC-SQL et Brazil pré-requis : lecture de Tutorial JDBC de Sun

Bibliographie Brazil [Bra00]www.sun.com/research/brazil [Bra01]www.ExperimentalStuff.com, http://www.brazilhandlers.com:9090 JDBC Le tutorial JDBC de Sun http://java.sun.com/docs/books/tutorial/jdbc/index.html la base de données hypersonic http://sourceforge.net/projects/hsql/ Un tutorial SQL http://jfod.cnam.fr/tp_cdi/tp12_1/sqltutorial.htm

Sommaire JDBC introduction succincte hypersonic Une base de données entièrement écrite en Java Sa mise en œuvre Brazil+JDBC HTTP <-> SQL SqlHandler SqlTemplate

Java/JDBC : principes Le tutorial JDBC de Sun, un pré-requis

Java/JDBC package java.sql Chargement du "driver" associé à la base classe Connection 1) établir une connexion avec la base classe Statement 2) engendre une instruction : syntaxe SQL classe ResultSet 3) exécution ResultSet rs = stmt.executequery("select * from.. ") puis rs.next() int res = stmt.executeupdate("insert into. ")

Hypersonic version 1.6 http://www.hsqldb.org Chargement du "driver" Class.forName("org.hsqldb.jdbcDriver"); établir une connexion : Connection con = DriverManager.getConnection( String url, String user, String password) quelles sont les url?, où est la base? Mémoire url = jdbc.hsqldb:. Fichier local url = jdbc.hsqldb:test Socket url = jdbc.hsqldb://localhost Web / HTTP url = jdbc.hsqldb:http//localhost/

Hypersonic,java/JDBC, suite Un exemple : Class.forName("org.hsqldb.jdbcDriver"); // driver jdbc con = DriverManager.getConnection( "jdbc:hsqldb:http://localhost:8181/la_base", // url //ou "jdbc:hsqldb:la_base", "sa", // user ""); // password stmt = con.createstatement(); int res=stmt.executeupdate("create table lundi "); ResultSet rs =stmt.executequery("select * from lundi"); int res=stmt.executeupdate("insert into lundi ");

Commandes SQL : syntaxe CREATE TABLE name ( columndefinition [,...] ) columndefinition: column DataType [ [NOT] NULL] [PRIMARY KEY] DataType: { INTEGER DOUBLE VARCHAR DATE TIME... } SELECT [DISTINCT] { selectexpression table.* * } [,... ] [INTO newtable] FROM tablelist [WHERE Expression] [ORDER BY selectexpression [{ASC DESC}] [,...] ] [GROUP BY Expression [,...] ] [UNION [ALL] selectstatement] INSERT INTO table [ (column [,...] ) ] { VALUES(Expression [,...]) SelectStatement }

Hypersonic 1.6 Serveur Web : intranet/internet java cp hsqldb.jar org.hsqldb.webserver -port 8181 -silent false -database LA_BASE Serveur socket : intranet java cp hsqldb.jar org.hsqldb.server -port 899 -silent false -database LA_BASE

Sources Java : 3 applications create : création d une table : nommée lundi insert : Insertion dans cette table select : lecture de cette table en commun : le chargement du Driver : String url ="jdbc:hsqldb:la_base"; //ou String url ="jdbc:hsqldb:http://localhost:8181/la_base"; Connection con; Statement stmt; try { Class.forName("org.hsqldb.jdbcDriver"); } catch(java.lang.classnotfoundexception e) { System.err.print("ClassNotFoundException: " + e.getmessage()); } con = DriverManager.getConnection(url,"sa",""); stmt = con.createstatement();

Exemple : create table try { String sql = "CREATE TABLE lundi ( " + "nom VARCHAR, " + "prenom VARCHAR, " + "e_mail VARCHAR, " + "sujet INTEGER) "; stmt.executeupdate(sql); stmt.close(); con.close(); } catch(sqlexception ex) {...

CreateLundi.java, au complet import java.sql.*; public class CreateLundi { public static void main(string[] args) { try { Class.forName("org.hsqldb.jdbcDriver"); } } Connection con = DriverManager.getConnection("jdbc:hsqldb:LA_BASE","sa",""); // ou "jdbc:hsqldb:http://localhost:8181/la_base" Statement stmt = con.createstatement(); String sql = "CREATE TABLE Lundi ( " + "nom VARCHAR, " + "prenom VARCHAR, " + "e_mail VARCHAR, " + "sujet integer ) "; stmt.executeupdate(sql); stmt.close(); con.close(); } catch (Exception e){ System.err.println("Exception in createtables()"); System.err.println(e); }

À faire chez soi Copier/coller/compiler/exécuter le texte du transparent précedent : CreateLundi.java Démarrer le serveur web/hsql sur une autre machine ou dans un autre répertoire Modifier/compiler CreateLundi.java en conséquence Exécuter CreateLundi

Exemple de source Java : insert String sql = "insert into LUNDI " + "values('lambert', 'michel', " + " '<A HREF=\"mailto:mlambert@noos.fr\">mlambert@noos.fr</A>', 1)"); int res = stmt.executeupdate(sql); stmt.close(); con.close(); } catch(sqlexception ex) {...

InsertLundi.java import java.sql.*; public class InsertLundi{ public static void main(string[] args) { try {Class.forName("org.hsqldb.jdbcDriver"); Connection con = DriverManager.getConnection("jdbc:hsqldb:LA_BASE","sa",""); // ou "jdbc:hsqldb:http://localhost:8181/la_base" Statement stmt = con.createstatement(); String sql= "insert into lundi values( " + " Lambert', michel', " + " <A HREF=\"mailto:mlambert@noos.fr\">mlambert@noos.fr</A>',1)"; stmt.executeupdate(sql); sql = "insert into Vendredi values( " + " Robleda', bruno', " + " <A HREF=\"mailto:brunodb@club-internet.fr \">brunodb@club-internet.fr</a>',1)"; stmt.executeupdate(sql); } } stmt.close(); con.close(); } catch (Exception e){ System.err.println("Exception in creattables()"); System.err.println(e); }

À faire chez soi Copier/coller/compiler/exécuter le texte du transparent précedent : InsertLundi.java Démarrer le serveur web/hsql sur une autre machine ou dans un autre répertoire Modifier/compiler InsertLundi.java en conséquence Exécuter InsertLundi

Exemple de source Java : select String sql = "select * from lundi"; ResultSet rs = stmt.executequery(sql); ResultSetMetaData meta = rs.getmetadata(); int columns = meta.getcolumncount(); while (rs.next()) { for(int i=1;i<=columns;i++){ System.out.println(rs.getString(meta.getColumnLabel(i))); } // for } // while stmt.close(); con.close(); } catch(sqlexception ex) {...

import java.sql.*; public class SelectLundi { public static void main(string[] args) { try { Class.forName("org.hsqldb.jdbcDriver"); SelectLundi.java Connection con = DriverManager.getConnection("jdbc:hsqldb:LA_BASE","sa",""); // ou "jdbc:hsqldb:http://localhost:8181/la_base" Statement stmt = con.createstatement(); String sql = "select * from lundi"; ResultSet rs = stmt.executequery(sql); ResultSetMetaData meta = rs.getmetadata(); int columns = meta.getcolumncount(); } while (rs.next()) { for(int i=1;i<=columns;i++){ System.out.print(rs.getString(i) + " "); } System.out.println(); } // while stmt.close(); con.close(); } catch (Exception e){ System.err.println(e);}

À faire chez soi Copier/coller/compiler/exécuter le texte du transparent précedent : SelectLundi.java Démarrer le serveur web/hsql sur une autre machine ou dans un autre répertoire Modifier/compiler SelectLundi.java en conséquence Exécuter SelectLundi

Brazil Interface HTTP <-> SQL-JDBC http://lmi80.cnam.fr:9090/hypersonic/?query=select+*+from+lundi http://lmi80.cnam.fr:9090/hypersonic/?update=insert+into+lundi..

Serveur Brazil + SqlHandler HTTP1.1 1 request SqlHandler HTTP1.1 request ChainHandler 2 request FileHandler Serveur de requêtes

Le fichier de configuration main.class=sunlabs.brazil.server.chainhandler main.handlers=sql file # sql.class=cnam.sql.sqlhandler sql.name=/hypersonic/ # les paramètres jdbc du handler sql.driver=org.hsqldb.jdbcdriver sql.url=jdbc:hsqldb:la_base # ou sql.url=jdbc:hsqldb:http://localhost:8181/la_base sql.user=sa sql.password=

Structure de SqlHandler méthode init lecture des paramètres du fichier de configuration méthode respond paramètres query et update

Structure de SqlHandler.java,init public boolean init(server server, String prefix){ this.server = server; this.propsprefix = prefix; try { name = server.props.getproperty(propsprefix + "name"); driver = server.props.getproperty(propsprefix + "driver"); url = server.props.getproperty(propsprefix + "url"); user = server.props.getproperty(propsprefix + "user"); password = server.props.getproperty(propsprefix + "password"); } catch (Exception e1) {...return false;} try { Class.forName(driver); } catch(exception e) {...return false;} } return true;

SqlHandler respond public boolean respond(request request) throws IOException{ String urlprefix = request.props.getproperty(propsprefix, name); if (!request.url.startswith(urlprefix)) { server.log(server.log_informational, null,"not " + name + " prefix: " + request.url); return false; } Hashtable t = request.getquerydata(); String result; String query=null; Connection con=null; Statement stmt=null; try{ con = DriverManager.getConnection(url,user,password); stmt = con.createstatement(); // lecture des paramètres HTTP

SqlHandler respond suite // lecture des paramètres HTTP query = (String)t.get("query"); if (query!= null){ ResultSet rs = stmt.executequery(query); result = query + formattable(rs); } else { String update = (String)t.get("update"); if( update!= null){ int res = stmt.executeupdate(update); result = update + res; } else result = "unknown, only (query or update)"; } stmt.close(); con.close();.

SqlHandler : formattable public static String formattable(resultset rs) throws SQLException{ ResultSetMetaData meta = rs.getmetadata(); StringBuffer html = new StringBuffer(); html.append("<table border=2 BGCOLOR=\"#ffb0d8\">\r\n"); html.append("<caption><big><b>résultat</b><big></caption>\r\n"); int columns = meta.getcolumncount(); html.append("<tr BGCOLOR=\"#00fd00\">\r\n"); for(int i=1;i<=columns;i++){ html.append("<td><big><b>"+meta.getcolumnlabel(i)+ "</b></big></td>\r\n"); } // for html.append("</tr>\r\n");

SqlHandler : formattable suite... while (rs.next()) { html.append("<tr>\r\n"); for(int i=1;i<=columns;i++){ html.append("<td>\r\n"); html.append(" " + rs.getstring(meta.getcolumnlabel(i)) + "\r\n"); html.append("</td>\r\n"); } // for html.append("</tr>\r\n"); } // while html.append("</table>\r\n"); return html.tostring(); }

SqlHandler : le résultat

Serveur Brazil avec authentification HTTP1.1 1 request BasicAuthHandler HTTP1.1 request ChainHandler 2 request SqlHandler 3 request Serveur de requêtes FileHandler

Le fichier de configuration main.class=sunlabs.brazil.server.chainhandler main.handlers=auth sql file # auth.class=sunlabs.brazil.handler.basicauthhandler auth.prefix=/hypersonic/ auth.mapfile=credentials auth.session=account auth.realm=this server requires authentication auth.message=pas de SQL possible pour vous!!! # sql.class=sql.sqlhandler sql.name=/hypersonic/ sql.driver=org.hsql.jdbcdriver sql.url=jdbc:hsqldb:la_base sql.user=sa sql.password=

BasicAuthHandler : le résultat

Architecture possible FormHandler insertion en base de données SqlHandler interrogation, modification

FormHandler HTTP1.1 1 request HTTP1.1 request ChainHandler 2 request BasicAuthHandler 3 SqlHandler Serveur de requêtes request 4 FileHandler

Formulaire : FormHandler <FORM ACTION="http://lmi80.cnam.fr:9090/insert/"> <INPUT TYPE=hidden name= "table" value= "Lundi"> <TABLE BORDER CELLPADDING="2">...

Structure de FormHandler méthode init : chargement du jdbcdriver méthode respond : Hashtable t = request.getquerydata(); String sql = "insert into " + (String)t.get("table") + " values(" + " " + (String)t.get("nom") + "," + " " + (String)t.get("prenom") + "," + " " + (String)t.get("e_mail") + ")"; int res = stmt.executeupdate(sql);

le fichier de configuration form.class=cnam.sql.formhandler form.name=/insert/ form.driver=org.hsqldb.jdbcdriver form.url=jdbc:hsqldb:la_base #ou form.url=jdbc:hsqldb:http://localhost:8181/la_base form.user=sa form.password=

Brazil + BSL Brazil et les Templates balises HTML, interprétées avant d être transmises <sql SqlTemplate <foreach et <if BSLTemplate <property PropsTemplate

Rappel BSL <! Le fichier TestSql.html <table border> <foreach name=a glob=*> <tr> <td><property a.name> </td> <td><property a.value> </td> <tr> </foreach> </table> Une table HTML : visualisation de server.props BSL : <if> <elseif> <else> </if> <foreach> </foreach> <property name=xxx> est remplacé par xxx

Exemple Accès aux propriétés du serveur (server.props) <html> la valeur log : <property log> la racine root : <property root> <tag>a href=<property name=test.url></tag> un_lien</a> <if name=liste.cnam_url.${field}.${index} match=http:+>

Usage de BSL : le résultat

SqlHandler / SqlTemplate <sql prefix=inscrit> select * from lundi</sql> #le fichier de configuration sql.class=sunlabs.brazil.template.templatehandler sql.templates= sunlabs.brazil.template.debugtemplate \ sunlabs.brazil.template.propstemplate \ sunlabs.brazil.sql.sqltemplate \ sunlabs.brazil.template.bsltemplate sql.driver=org.hsqldb.jdbcdriver sql.url=jdbc:hsqldb:la_base sql.sqlprefix=param param.user=sa param.password=

SqlTemplate + BSLTemplate <sql prefix=inscrit> SELECT * FROM lundi</sql> <TABLE border=2> <foreach name=index property=inscrit.rows > <tr> <foreach name=field list="nom PRENOM SUJET E_MAIL"> <td> <property name=inscrit.lundi.${field}.${index}> </td> </foreach> </tr> </foreach> </TABLE>

SqlTemplate : le résultat

<HTML><HEAD> <TITLE>SqlTemplate</TITLE></HEAD> <BODY><P>Test SqlTemplate, serveur Brazil, le 26 Mai 2003<P> <sql prefix=inscrit> SELECT * FROM lundi</sql> <debug apres la requete SQL: > <TABLE border=2> <tr> <foreach name=field list=" NOM PRENOM SUJET E_MAIL"> <td> <b><property name=field></b> </td> </foreach> </tr> <foreach name=index property=inscrit.rows > <tr> <foreach name=field list="nom PRENOM SUJET E_MAIL"> <td><property name=inscrit.lundi.${field}.${index}></td> </foreach> </tr> </foreach> </TABLE> <p> </BODY></HTML>

Conclusion