Service computing
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

Service Computing PowerPoint PPT Presentation


  • 39 Views
  • Uploaded on
  • Presentation posted in: General

Service Computing. Prof. Dr. Ramin Yahyapour IT & Medien Centrum 27. Oktober 2009. Initialization. Loading a driver Class.forName (“ sun.jdbc.odbc.JdbcOdbcDriver ”); Making a Connection Connection con = DriverManager.getConnection ( url , “ mylogin ”, “password”);

Download Presentation

Service Computing

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Service computing

Service Computing

Prof. Dr. Ramin YahyapourIT & Medien Centrum27. Oktober 2009


Initialization

Initialization

  • Loading a driver

    • Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

  • Making a Connection

    • Connection con = DriverManager.getConnection(url, “mylogin”, “password”);

    • url identifies the Data Source in Database. String url = “jdbc:oracle:oci8:@ccdb”


Jdbc statement

JDBC Statement

  • A JDBC statement object is used to send your SQL statement to the database server

  • A JDBC statement is associated with an open connection and not any single SQL statement

  • JDBC provides three classes of SQL statement

    • Statement

    • PreparedStatement

    • CallableStatement


Jdbc example statements for create insert select

JDBC ExampleStatements for Create, Insert, Select

Statement stmt = con.createStatement();

String createMyTable = “Create table book “+

“(Title VARCHAR(128), “+

“Author VARCHAR(64), “+

“ISBN VARCHAR(32) )”;

stmt.executeUpdate(createMyTable);

String insertBook = “Insert into book values “+

“(\“Java and XML\”,\”Brett McLaughlin\”, \”3-89721-280-3\”)”;

stmt.executeUpdate(insertBook);

String queryBook = “select * from book”;

ResultSetrs = stmt.executeQuery(queryBook);

While (rs.next()) {

String title = rs.getString(“Title”);

String author = rs.getString(“Author”);

String isbn = rs.getString(“ISBN”);

}


Jdbc example using prepared statements

JDBC ExampleUsing Prepared Statements

// Statement

String insertBook = “Insert into book values “+

“(\“Java and XML\”,\”Brett McLaughlin\”, \”3-89721-280-3\”)”;

stmt.executeUpdate(insertBook);

// Prepared Statements

String insert = “Insert into book (?,?,?)”;

PreparedStatement stmt2 = con.prepareStatement(insert);

stmt2.setString(1,”Java and XML”);

stmt2.setString(2,”Brett McLaugling”);

stmt2.setString(3,”3-89721-280-3”);

stmt2.executeUpdate();

String query =“SELECT Author from book where ISBN=?”;

PreparedStatement stmt2 = con.prepareStatement(query);

stmt2.setString(1,”3-89721-280-3”);

ResultSetrs = stmt2.executeUpdate();

While (rs.next())

System.out.println(rs.getString(“Author”));


Jdbc example using stored procedures

JDBC ExampleUsing Stored Procedures

// Stored Procedures

String createProcedure = “Create Procedure ShowAvailableBooks” + “as Select Name from Books where in_store > 0)”;

stmt.executeUpdate(createProcedure);

CallableStatementcs = con.prepareCall(“

(call ShowAvailableBooks)”);

ResultSetrs = cs.executeQuery();

  • Procedure is stored on server

  • Procedure can be used repeatedly.


Transactions with jdbc

Transactions with JDBC

  • JDBC allows SQL statements to be grouped together into a single transaction

  • Transaction control is performed by the Connection object, default mode is auto-commit, I.e., each sql statement is treated as a transaction

  • We can turn off the auto-commit mode with con.setAutoCommit(false);

  • And turn it back on with con.setAutoCommit(true);

  • Once auto-commit is off, no SQL statement will be committed until an explicit is invoked con.commit();

  • At this point all changes done by the SQL statements will be made permanent in the database.


Transactions with jdbc1

Transactions with JDBC

  • If we don’t want certain changes to be made permanent, we can issue con.rollback();

  • Any changes made since the last commit will be ignored – usually rollback is used in combination with Java’s exception handling ability to recover from unpredictable errors.

con.setAutocommit(false);

Statement stmt = con.createStatement();

stmt.executeUpdate(“INSERT INTO Book VALUES

(‘Java and XML’,’Brett Mc Lauglhin’,’3-89721-280-3’)”);

con.rollback();

stmt.executeUpdate(“INSERT INTO Book VALUES

(‘Harry Potter 5’,’J. Rowling’’,’ 3-55155-555-9’)”);

con.commit();

con.setAutoCommit(true);


Error handling

Error Handling

  • Errors in JDBC yield to exceptions of type SQLException

try{

stmt.executeUpdate(queryBooks);

} catch (SQLException e){

System.out.println(e.getMessage())

}


First servlet

First Servlet

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SomeServletextends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

// Use "request" to read incoming HTTP headers (e.g. cookies)

// and HTML form data (e.g. data the user entered and submitted)

// Use "response" to specify the HTTP response line and headers

// (e.g. specifying the content type, setting cookies).

PrintWriter out = response.getWriter();

// Use "out" to send content to browser

}

}


First servlet hello world example

First Servlet -Hello World Example

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

PrintWriter out = response.getWriter();

out.println("Hello World");

}

}


Servlet with html output

Servlet with HTML Output

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWWW extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +

"Transitional//EN\">\n" +

"<HTML>\n" +

"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +

"<BODY>\n" +

"<H1>Hello WWW</H1>\n" +

"</BODY></HTML>");

}

}


Accessing parameters

Accessing Parameters

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String title = "Reading Three Request Parameters";

out.println(ServletUtilities.headWithTitle(title) +

"<BODY>\n" +

"<H1 ALIGN=CENTER>" + title + "</H1>\n" +

"<UL>\n" +

" <LI>param1: "

+ request.getParameter("param1") + "\n" +

" <LI>param2: "

+ request.getParameter("param2") + "\n" +

" <LI>param3: "

+ request.getParameter("param3") + "\n" +

"</UL>\n" +

"</BODY></HTML>");

}

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}


Parameters in urls

Parameters in URLs

http://host/path?user=Marty+Hall&origin=bwi&dest=lax


Using cookies

Using cookies

  • Cookie stores information on client

  • access to cookie content on later requests

    • cookie is usually persistent on client if enabled

  • cookie is only accessible from issuing server

Cookie[] cookies = request.getCookies();

String searchString =

ServletUtilities.getCookieValue(cookies,

"searchString",

"Java Programming");

Cookie userCookie = new Cookie("user", "uid1234");

response.addCookie(userCookie);


Additional cookie methods

Additional Cookie Methods

Examples for cookie functions:

  • getMaxAge/setMaxAge

  • getName/setName

    • Gets/sets the name of the cookie. The name and the value are the two pieces you virtually always care about. Since the getCookies method of HttpServletRequest returns an array of Cookie objects, it is common to loop down this array until you have a particular name, then check the value with getValue. See the getCookieValue method shown below.

  • getPath/setPath

    • Gets/sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories. This method can be used to specify something more general. For example, someCookie.setPath("/") specifies that all pages on the server should receive the cookie. Note that the path specified must include the current directory.

  • getValue/setValue

    • Gets/sets the value associated with the cookie. Again, the name and the value are the two parts of a cookie that you almost always care about, although in a few cases a name is used as a boolean flag, and its value is ignored (i.e the existence of the name means true).


Session tracking

Session Tracking

HttpSession session = request.getSession(true);

ShoppingCartpreviousItems =

(ShoppingCart)session.getValue("previousItems");

if (previousItems != null) {

doSomethingWith(previousItems);

} else {

previousItems = new ShoppingCart(...);

doSomethingElseWith(previousItems);

}

HttpSession session = request.getSession(true);

session.putValue("referringPage", request.getHeader("Referer"));

session.putValue("previousItems", previousItems);


  • Login