java + xml. java 2 enterprise edition server side java servlets jsp javabeans web services database...

Post on 16-Jan-2016

232 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

What Does JDBC Do?

Establish a connection with a database

Send SQL statements

Process the result

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”);

}

JDBC Two-tier Model

DBMS

Java Application

JDBCClient Machine

DBMS - protocol

Database server

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)

Using JDBC APIs

Before Using JDBC:

Install Java and JDBC on your machine

Install a driver on your machine

Install your DBMS

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");

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”

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)");

Updating Tables

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

stmt.executeUpdate(updateString);

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.");}

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");

Writing XML with Java

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

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

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

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

startDocument endDocument startElement endElement characters

Parser

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser = factory.newSAXParser();

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

top related