Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc."— Presentation transcript:

1 Java + XML

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

3 Database Access - JDBC JDBC is a Java API for executing SQL statements JDBC is not an acronym but… “Java DataBase Connectivity” sounds good enough

4 What Does JDBC Do? Establish a connection with a database Send SQL statements Process the result

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

6 JDBC Two-tier Model DBMS Java Application JDBC Client Machine DBMS - protocol Database server

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

8 Using JDBC APIs Before Using JDBC: Install Java and JDBC on your machine Install a driver on your machine Install your DBMS

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

10 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”

11 Creating JDBC Statement Statement 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)");

12 Updating Tables String updateString = "UPDATE COFFEES SET SALES = 75 WHERE COF_NAME " + " LIKE 'Colombian'"; stmt.executeUpdate(updateString);

13 Using Queries String 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."); }

14 Writing XML with Java Create A new File OutputStream fout= new FileOutputStream("fibonacci.xml"); Add Buffering OutputStream bout= new BufferedOutputStream(fout); Create Writer OutputStreamWriter out=new OutputStreamWriter(bout, "8859_1");

15 Writing XML with Java out.write("<?xml version=\"1.0\" "); out.write("encoding=\"ISO-8859-1\"?>\r\n"); ….. out.flush(); out.close();

16 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 http://samples.gotdotnet.com/quickstart/

17 Using SAX SAX – Simple API for XML Based on interface implementations Parser class extends DefaultHandler Functions: startDocument endDocument startElement endElement characters

18 Parser SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.parse( new File(argv[0]), handler);


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

Similar presentations


Ads by Google