18
Java + XML

Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Embed Size (px)

Citation preview

Page 1: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Java + XML

Page 2: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Java 2 Enterprise Edition

Server Side java Servlets JSP

JavaBeans Web Services Database

jdbc

Page 3: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Database Access - JDBC

JDBC is a Java API for executing SQL statements

JDBC is not an acronym but…“Java DataBase Connectivity” sounds good enough

Page 4: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

What Does JDBC Do?

Establish a connection with a database

Send SQL statements

Process the result

Page 5: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Basic Example

Connection con = DriverManager.getConnection(“jdbc:odbc:myDNS”,”myLogin”,”myPass”);

Statement stm = con.createStatement();ResultSet rs = stm.executeQuery(“SELECT * FROM

myTable”);while (rs.next()) {

int i = getInt(“id”);int n = getString(“name”);

}

Page 6: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

JDBC Two-tier Model

DBMS

Java Application

JDBCClient Machine

DBMS - protocol

Database server

Page 7: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

JDBC Three-tier Model

DBMS

Application Server

(Java)

JDBC

Server Machine

DBMS - proprientary protocol

Database server

Java Applet or HTML browser

HTTP,RMI,CORBA, or other calls

Client machine(GUI)

Page 8: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Using JDBC APIs

Before Using JDBC:

Install Java and JDBC on your machine

Install a driver on your machine

Install your DBMS

Page 9: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Loading Drivers

Loading the driver or drivers you want to use is very simple and involves just one line of code.

If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Page 10: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Making the Connection

The second step in establishing a connection is to have the appropriate driver connect to the DBMS. The following line of code illustrates the general idea:

Connection con = DriverManager.getConnection(“url”,"myLogin", "myPassword");

The Url is : “jdbc:odbc:yourdsn”

Page 11: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Creating JDBC StatementStatement stmt = con.createStatement();

At this point stmt exists, but it does not have an SQL statement to pass on to the DBMS. We need to supply that to the method we use to execute stmt .

stmt.executeUpdate("CREATE TABLE COFFEES "+"(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " +"SALES INTEGER, TOTAL INTEGER)");

Page 12: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Updating Tables

String updateString = "UPDATE COFFEES SET SALES = 75 WHERE COF_NAME" + "LIKE 'Colombian'";

stmt.executeUpdate(updateString);

Page 13: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Using QueriesString query = "SELECT COF_NAME, SALES FROM COFFEES "+"WHERE COF_NAME LIKE 'Colombian'";

ResultSet rs = stmt.executeQuery(query);while (rs.next()) {

String s = rs.getString("COF_NAME");

int n = rs.getInt("SALES");System.out.println(n + " pounds of

" + s + " sold this week.");}

Page 14: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Writing XML with Java Create A new FileOutputStream fout= new

FileOutputStream("fibonacci.xml"); Add BufferingOutputStream bout= new

BufferedOutputStream(fout); Create WriterOutputStreamWriter out=new

OutputStreamWriter(bout, "8859_1");

Page 15: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Writing XML with Java

out.write("<?xml version=\"1.0\" ");out.write("encoding=\"ISO-8859-1\"?>\r\

n");…..…..out.flush();out.close();

Page 16: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Using A Web Service

General Service Any Platform Based On HTTP (SOAP) Example: http

://samples.gotdotnet.com/quickstart/aspplus/samples/services/MathService/VB/MathService.asmx

Page 17: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Using SAX SAX – Simple API for XML Based on interface implementations Parser class extends DefaultHandler Functions:

startDocument endDocument startElement endElement characters

Page 18: Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc

Parser

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser = factory.newSAXParser();

saxParser.parse( new File(argv[0]), handler);