1 / 18

JDBC & Servlet

JDBC & Servlet. CSE 4504/6504 Lab. Outline. HTML Forms Tomcat Functions in JDBC & Servlet. HTML Forms. An interface controls to collect data from the user and transmit it to server. Element in Forms. TEXT CONTROLS: <INPUT TYPE="TEXT" NAME="NAME" VALUE="INIT"> PASSWORD FIELDS:

muncel
Download Presentation

JDBC & Servlet

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JDBC & Servlet CSE 4504/6504 Lab

  2. Outline • HTML Forms • Tomcat • Functions in JDBC & Servlet

  3. HTML Forms An interface controls to collect data from the user and transmit it to server.

  4. Element in Forms • TEXT CONTROLS: <INPUT TYPE="TEXT" NAME="NAME" VALUE="INIT"> • PASSWORD FIELDS: <INPUT TYPE="PASSWORD" NAME="PASSWORD"> • TEXT AREAS: <TEXTAREA NAME="RESUME" ROWS=5 COLS=30>INPUT YOUR RESUME HERE </TEXTAREA> • Checkbox <input type="checkbox" name="checkbox" checked><input type="checkbox" name="checkbox"> • Radio Button <input type="radio" name="radio" checked><input type="radio" name="radio">

  5. Cont. • List <select name="list"> <option value="Item 1">Item 1</option> <option value="Item 2">Item 2</option> <option value="Item 3">Item 3</option> </select> • Multilist <select name="multilist" size="3" multiple> <option value="Item 1">Item 1</option> <option value="Item 2">Item 2</option> <option value="Item 3">Item 3</option> </select>

  6. Cont. • Submit Button <input type="submit" name="submit" value="Submit"> • Reset Button <input type="reset" name="reset" value="Reset Fields"> • Image Button <input type="image" name="image" src="go.gif"> • File <input type="file" name="file">

  7. Tomcat • A light web server that supports servlet & JSP. • It can be integrated in Apache, IIS For installation process, please refer: CS III: Lab assignment, servlet http://www.cse.msstate.edu/~cs2324/spring03/

  8. What is JDBC & Servlet? • JDBC (Java DataBase Connectivity) provides functions to access database system. • Servlet enables java for CGI programs. • Setup JDBC environment: Please refer: CS III, Lab assignment, JDBC http://www.cse.msstate.edu/~cs2324/spring03/

  9. JDBC : Establishing a Connection • loading the driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); • making the connection String url = "jdbc:oracle:thin:@ra.msstate.edu:1521:ACAD"; Connection con = DriverManager.getConnection(url, “loginName", “Password");

  10. Statement • Create a statement Statement stmt = con.createStatement(); • Two methods of statement 1. executeUpdate() create, alter, drop a table Or insert, delete, update data 2. executeQuery() select

  11. Create Table • String createTableCoffees = "CREATE TABLE COFFEES " + "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " + "SALES INTEGER, TOTAL INTEGER)"; stmt.executeUpdate(createTableCoffees);

  12. Query Data from a Table • stmt.executeQuery (“select * from customer”); • ResultSet rs = stmt.executeQuery( "SELECT COF_NAME, PRICE FROM COFFEES");

  13. Display Result • Method next() Initially the cursor is above the first row of data. After call the method next(), the cursor is pointing to the first row of data. • A Sample while (rs.next()) { String s = rs.getString ("COF_NAME"); float n = rs.getFloat ("PRICE"); System.out.println (s + " " + n); } References: http://java.sun.com/docs/books/tutorial/jdbc/index.html

  14. Methods to Call a Servlet • GET In html: <A HREF="/servlet/dosearch?aa=12&bb=32">Return Home</A> In html forms: <FORM ACTION=“/servlet/dosearch” METHOD=“GET”> • POST In html forms: <FORM ACTION=“/servlet/dosearch” METHOD=“POST”>

  15. Interacting with Clients Handling GET and POST Requests public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException <a href="/servlet/getCustomers">List Customers</a> public void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException <FORM ACTION=\"/servlet/doInsert\" METHOD=post>

  16. Output of the Response • Set the content type of the output response.setContentType ("text/html") • Get parameter String bookId = request.getParameter ("bookId"); • Get the output stream to write to PrintWriter out = response.getWriter(); out.println(“<HTML>”);

  17. Servlet Program Structures • import javax.servlet.*; import javax.servlet.http.*; • Class must extend from class HttpServlet

  18. A Simple Application public class SimpleServlet extends HttpServlet { // Handle the HTTP GET method by building a simple web page. public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String title = "Simple Servlet Output"; // set content type and other response header fields response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println (title); out.println("</TITLE></HEAD><BODY>"); out.println("<H1>" + title + "</H1>"); out.println("<P>This is output from SimpleServlet."); out.println("</BODY></HTML>"); out.close(); } }

More Related