Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.

Slides:



Advertisements
Similar presentations
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Server-Side Scripting with Java Server Page, JSP.
Introduction to Structured Query Language, SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
JDBC. JDBC (Java Database Connectivity): JDBC is an API for the Java programming language that defines how a client may access a database. It provides.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Structured Query Language SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
CSCI 6962: Server-side Design and Programming
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Server-Side Scripting with Java Server Page, JSP.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
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.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
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.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Database Programming Basic JDBC Programming Concepts.
JSP and DB.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
CS3220 Web and Internet Programming Database Access with JDBC
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Chapter 5 Introduction to SQL.
CS320 Web and Internet Programming Database Access with JDBC
Database JDBC Overview CS Programming Languages for Web Applications
AJAX and Java Servlet ISYS 350.
HW#4 Making Simple BBS Using JDBC
JDBC.
Database Processing with JSP
Using a Database with JDBC
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Database Processing with JSP ISYS 350

Database Applications Applications Database Server Queries/Updates Results

Database: Customer table Fields: CID, Cname, City, Rating Example: Enter CID in a box and retrieve the customer record

Create a CID ListBox

SQL Select Command 1. Select all records from a table: SELECT * FROM tableName; Ex. SELECT * FROM customer; 2. Select records meeting criteria: SELECT * FROM tableName WHERE criteria; Ex. SELECT * FROM customer WHERE city=‘SF’; 3. Select one field from a table: Ex,CID of the Customer table SELECT CID FROM CUSTOMER; 3. Select a few fields from a table: Ex,CID, Cname of the Customer table Ex. SELECT CID, Cname FROM customer;

Creating a String Containing SQL Select Command Assuming the CID is entered in a HTML textbox named “cid” or selected from a HTML listbox named “cid”: String myCIDS, strSQL; myCid=request.getParameter("cid"); strSQL="select * from customer where cid='" + myCid + "'";

Creating a Java DB (Derby) Database with NetBeans Tutorial: – db.html#starting db.html#starting

1. Creating a Database: Click Services tab; open databases node; right click Java DB and select Start Server; then select Create Database Note: You may leave username and password blank or assign a name and password.

Difference between with Username and No username No username: Create table in the App folder With username: create table in the default username’s folder

2. Connect to the database: Right click database name

Connected Database

3. Creating a New Table: Open the App folder, and: 1. Right click Tables and select create table 2. Enter Table name 3. Click Add column to define field

Create a New Table Table name: Customer Fields: – CID: Type – CHAR with size 5; primary key – Cname: Type – CHAR with size 20 – City:Type – CHAR with size 20 – Rating: Type – CHAR with size 1

To insert records to a table: Right-click the table name and select View Data From the record view window, click the Insert Record button: Note: Click Add Row to continue adding new row; click OK after entering the last row.

Database Processing with JSP Java Database Connectivity (JDBC) is an application program interface (API) specification for connecting programs written in Java to the data in popular databases. Must import java.sql using page directive: – Need a database driver: – Java Derby database: jdbc:derby

Define a connection string A connection string is a string that specifies information about a data source and the means of connecting to it. It is passed in code to an underlying driver or provider in order to initiate the connection. Example: – DBUrl="jdbc:derby://localhost:1527/CRM";

Define Database Objects to Run SQL Select Command Define a connection object using the connection string: – No password: connection = DriverManager.getConnection(DBUrl); – With password: connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Define a SQL Statement object: – Statement SQLStatement = connection.createStatement(); SQL Statement object’s executeQuery method: – executeQuery: This method executes SQL Select statement and create a resultset object: – Example: strSQL="select * from customer where cid='" + myCid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL);

JDBC ResultSet The rows that satisfy a particular query are called the result set. The number of rows returned in a result set can be zero or more. A user can access the data in a result set using a cursor one row at a time from top to bottom. A cursor can be thought of as a pointer to the rows of the result set that has the ability to keep track of which row is currently being accessed.

ResultSet Methods Next() : Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row. previous() first() last()

Example 1: Result set contains at most one record (selecting record based on primary key) Insert title here Enter CID:

<% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; try { String myCid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("mycid"); strSQL="select * from customer where cid='" + myCid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); rs.close(); out.println("Cname: "); out.println("City: "); out.println("Rating: "); } else { out.println("Customer not exist!"); } rs.close(); } catch(SQLException e) { out.println("Something wrong"); out.println(e.getMessage()); } %>

<% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; try { String myCid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("mycid"); strSQL="select * from customer where cid='" + myCid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); rs.close(); %> Cname: "> City: "> Rating: "> <% } else { out.println("Customer not exist!"); rs.close(); } catch(SQLException e) { out.println("Something wrong"); out.println(e.getMessage()); } %> Jdbc:derby demoUsing JSPexpression

Select CID: <% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; try { String myCid, strSQL; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); strSQL="select cid from customer"; ResultSet rs = SQLStatement.executeQuery(strSQL); while (rs.next()) { myCid=rs.getString("cid"); out.println(" " + myCid + " "); } catch(SQLException e) { out.println(e.getMessage()); } %> Example 2: Result set contains many keys Populate a Listbox using a ResultSet

Example 3: Result set contains many records Show Records in a Table <% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; try { String Cid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); strSQL="select * from customer"; ResultSet rs = SQLStatement.executeQuery(strSQL); out.println(" "); out.println(" CID Cname City Rating "); out.println(" "); while (rs.next()) { Cid=rs.getString("CID"); Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); out.println(" "); out.println(" " + Cid + " "); out.println(" " + Cname + " "); out.println(" " + City + " "); out.println(" " + Rating + " "); out.println(" "); } rs.close(); } catch(SQLException e) { out.println(e.getMessage()); } out.println(" "); %>

Derby demo:Show Records in a Table using JSP Expression <% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; try { String Cid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); strSQL="select * from customer"; ResultSet rs = SQLStatement.executeQuery(strSQL); %> CID Cname City Rating <% while (rs.next()) { Cid=rs.getString("CID"); Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); %> <% } rs.close(); } catch(SQLException e) { out.println(e.getMessage()); } %>

New Customer Data Entry Customer Data Entry Form Enter CID: Enter Name: Enter City: Enter rating:

SQL Insert Command INSERT INTO tableName VALUES (field values separated by commas); Ex 1. Customer table with CID, CNAME, CITY, RATING. INSERT INTO CUSTOMER VALUES (‘C1’, ‘SMITH’, ‘SF’, ‘A’); Ex 2. Orders table with OID, OrderDate, CID, SalesPerson INSERT INTO ORDERS VALUES (‘O11’, ‘9/28/02’, ‘C1’, ‘Peter’);

Creating A String Containing SQL Insert Command myCid=request.getParameter("cid"); myCname=request.getParameter("cname"); myCity=request.getParameter("city"); myRating=request.getParameter("rating"); strSQL = "Insert into Customer values ('"; strSQL += myCid + "','" + myCname + "','"; strSQL += myCity + "','" + myRating + "')";

SQL Statement Object’s executeUpdate Method to Run SQL Insert Statement executeUpdate: This method executes SQL Insert, Delete and Update statement and returns the number of records affected by the statement. Example: strSQL = "Insert into Customer values ('"; strSQL += myCid + "','" + myCname + "','"; strSQL += myCity + "','" + myRating + "')"; int Count; Count=SQLStatement.executeUpdate(strSQL);

Insert a New Record <% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; try { String myCid, strSQL, myCname, myCity, myRating; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("mycid"); myCname=request.getParameter("cname"); myCity=request.getParameter("city"); myRating=request.getParameter("rating"); strSQL = "Insert into Customer values ('"; strSQL += myCid + "','" + myCname + "','"; strSQL += myCity + "','" + myRating + "')"; int Count; Count=SQLStatement.executeUpdate(strSQL); if (Count==1) out.println("Insertion sucessful"); } catch(SQLException e) { out.println(e.getMessage()); } %>

Cookie Cookie is a small data file added by a website to reside in user’s system. Define a cookie: – new Cookie(“Key”, “value”); – Ex. Cookie cookieCID = new Cookie ("cookieCID",CID); Write a cookie: – response.addCookie(cookieCID);

Example: <% String CID="C1"; Cookie cookieCID = new Cookie ("cookieCID",CID); response.addCookie(cookieCID); out.println("CID cookie= " + CID + "added"); %>

Reading Cookies <% Cookie[] cookies = request.getCookies(); out.println(cookies[0].getName() + cookies[0].getValue() + " "); %> Use request.getCookies() method. This method retrieve cookies and return them in an array.

Reading Cookie and Customer Record <% try{ String myCid, strSQL, Cname, City, Rating; Cookie[] cookies = request.getCookies(); myCid=cookies[0].getValue(); Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRM5"; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); strSQL="select * from customer where cid='" + myCid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); rs.close(); %> Cname: "> City: "> Rating: "> <% } else { out.println("Customer not exist!"); rs.close(); } catch(SQLException e) { out.println(e.getMessage()); } %>