Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.

Slides:



Advertisements
Similar presentations
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Advertisements

Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Servlets and JDBC. Servlets A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
Distributed Application Development B. Ramamurthy.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
Murali Mani Web Interface. Murali Mani Options PHP with mySQL JSP/Servlets with JDBC on mySQL/Oracle Others …
The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Java Servlets and JSP.
SERVLETS.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
TUTORIAL (1) Software installation Written by: Eng. Ahmed Mohamed Abdel Ghafar, Edited by Eng. Muhammed Hammad, Eng. Hamdy Soltan & Eng. Osama Talaat.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
CSCI 6962: Server-side Design and Programming
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
CSE 305 Theory of Database Tutorial on Connecting with Sybase from Java program and Developing GUI Jalal Mahmud, TA, CSE 305.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
MDCFUG Is Java in Your Future? Tyler Williams Principal dataTerrace
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
 What software components are required?  How do I install the Oracle JDBC driver?  How do I connect to the database?  What form is the data in and.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
JSP program that interacts with HTML form & Access Data Base.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
JDBC Tutorial CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
EXAMPLE I An application showing JDBC access to Cloudscape.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
Basics of JDBC.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
Java Servlets and Java Server Pages
Web Programming Assistant Professor Xiaozhong Liu
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
Settings MySQL Database and JDBC configuration Instructor: Sergey Goldman.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
1. Writing a Java program to connect to SQL Server 2008 and Create a table Populate the table (insert data) Perform queries to retrieve information from.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlets Hits Counter 20-Jul-18.
HW#4 Making Simple BBS Using JDBC
MSIS 655 Advanced Business Applications Programming
JAVA DATABaSE CONNECTIVITY
JDBC Example.
Presentation transcript:

Servlets Database Access

Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a Table using Servlet

Setup Java Environment By now, you might have installed JDK 5.0 or later version. Make sure following environment variables are set as described below: JAVA_HOME: This environment variable should point to the directory where you installed the JDK, e.g. C:\Program Files\Java\jdk1.5.0 CLASSPATH: This environment variable should has appropriate paths set, e.g. C:\Program Files\Java\jdk1.5.0_20\jre\lib PATH: This environment variable should point to appropriate JRE bin, e.g. C:\Program Files\Java\jre1.5.0_20\bin. It is possible you have these variable set already, but just to make sure here's how to check. Go to the control panel and check System. Go to the Advanced tab and click on Environment Variables. Now check all the above mentioned variables are set properly. You automatically get both JDBC packages java.sql and javax.sql when you install J2SE Development Kit 5.0 (JDK 5.0)

Install Database: The most important thing you will need, of course is an actual running database with a table that you can query and modify. Install a database that is most suitable for you. You can have plenty of choices like MySQL, Oracle, PostgreSQL etc:  MySQL DB: MySQL is an open source database. You can download it from MySQL Official Site. We recommend downloading the full Windows installation. In addition, download and install MySQL Administrator as well as MySQL Query Browser. These are GUI based tools that will make your development much easier.MySQL Official Site MySQL AdministratorMySQL Query Browser. Finally, download and unzip MySQL Connector/J (the MySQL JDBC driver) in a convenient directory. For the purpose of this tutorial we will assume that you have installed the driver at C:\Program Files\MySQL\mysql-connector-java Accordingly set CLASSPATH variable to C:\Program Files\MySQL\mysql-connector-java-5.1.8\mysql-connector-java bin.jar. Your driver version may vary based on your installation.MySQL Connector/J  Oracle DB: Oracle DB is an commercial database sold by Oracle. We assume that you have the necessary distribution media to install it.Oracle installation includes a GUI based administrative tool called Enterprise Manager. JDBC drivers are also included as part of the installation.

Install Database Drivers:  Copy ojdbc14.jar and ojdbc14_g.jar to %CATALINA_HOME%\common\lib folder  Also make sure that servlet_api.jar is there in the same folder.

Create Table and add records: To create the EMP database, use the following steps: Step 1:  Open a SQL Command Prompt and type conn system and enter the password as manager to login into oracle. Note :You can also check documentation on how to work with oracle database.  Create the EMP table by executing the following command: CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), SAL NUMBER(7, 2),  Finally you create few data records in Emp table as follows: INSERT INTO Employees VALUES (1111, ‘Arun Shorie‘, 17000); etc…

Accessing a Table using Servlet  Here is an example which shows how to access EMP table using Servlet. // Loading required libraries import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class DatabaseAccess extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // JDBC driver name and database URL final String JDBC_DRIVER="oracle.jdbc.driver.OracleDriver"; // final String DB_URL="jdbc:mysql://localhost/TEST"; final String

Accessing a Table using Servlet..contd 1 // Database credentials final String USER = "system"; final String PASS = "manager"; Connection conn=null; Statement stmt=null; // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Database Result"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + " \n" + " " + title + " \n" + " \n" + " " + title + " \n"); try{ // Register JDBC driver Class.forName("oracle.jdbc.driver.OracleDriver");

Accessing a Table using Servlet..contd 2 // Open a connection conn = DriverManager.getConnection(DB_URL,USER,PASS); // Execute SQL query stmt = conn.createStatement(); String sql; sql = "SELECT empno,ename,sal FROM Emp"; ResultSet rs = stmt.executeQuery(sql); // Extract data from result set out.println("EMP ID NAME SALARY"+" "); out.println(" "+" "); while(rs.next()){ //Retrieve by column name int eno = rs.getInt("empno"); int sal = rs.getInt("sal"); String name = rs.getString("ename"); //String last = rs.getString("last");

Accessing a Table using Servlet..contd 3 //Display values out.println( eno + " "); out.println( name + " "); out.println( sal + " "); } out.println(" "); // Clean-up environment rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }

Accessing a Table using Servlet..contd 4 finally{ //finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try } //end try }

Edit web.xml Now let us compile above servlet and create following entries in web.xml.... DatabaseAccess DatabaseAccess /DatabaseAccess....

Database Result EMP ID NAME SALARY Arun Shorie Meena Mathur Atul Sen Now call this servlet using URL which would display following response:

THE END