Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.

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.
Distributed Application Development B. Ramamurthy.
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.
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.
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
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
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.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
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.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
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.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
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.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
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.
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.
JDBC (Java Database Connectivity)
Server-Side Scripting with Java Server Page, JSP.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
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.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
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.
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.
Lec - 14.
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.
Client Access, Queries, Stored Procedures, JDBC
Database Processing with JSP
Java Database Connectivity
JDBC Example.
Lecture 11 Database Connection
Presentation transcript:

Database Processing with JSP ISYS 350

Example: Enter CID in a box and retrieve the customer record

Create a CID ListBox

SQL Select Command Select all records from a table: SELECT * FROM tableName; Select records meeting criteria: SELECT * FROM tableName WHERE criteria;

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 + "'";

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 + "')";

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

Creating a Database: Right click Java DB and select Create Database Note: Username and password.

Connect or Delete a database: Right click database name

Connected Database

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

Database Processing with JSP Must import java.sql using page directive: – Need a driver: – JDBC-ODBC Bridge: The JDBC-ODBC Bridge allows applications written in the Java programming language to use with many existing ODBC drivers. – Note: JDBC-ODBC does not work if you have a Windows 7 64-bit system and 32-bit MS Access. – Example: String DBUrl="jdbc:odbc:MySalesDB"; – Java Derby database: – Example: String DBUrl="jdbc:derby://localhost:1527/SalesDBJava";

Define Database Objects Define connection object: – connection = DriverManager.getConnection(DBUrl); Define SQL Statement object: – Statement SQLStatement = connection.createStatement(); SQL Statement methods: – 1. 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); – 2. 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);

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

HTML Form Enter CID: Insert title here Enter CID:

<% Connection connection = null; String DBUrl="jdbc:odbc:MyCustomer"; try { String myCid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("cid"); 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:odbc demo: A DSN MyCustomer is defined for a SQL Server database.

Select CID: <% Connection connection = null; String DBUrl="jdbc:odbc:MyCustomer"; try { String myCid, strSQL; connection = DriverManager.getConnection(DBUrl); 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()); } %> Populate a Listbox using a ResultSet

Show Records in a Table <% Connection connection = null; String DBUrl="jdbc:odbc:MyCustomer"; try { String Cid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl); 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()); } %>

Insert a New Record <% Connection connection = null; String DBUrl="jdbc:odbc:MyCustomer"; try { String myCid, strSQL, myCname, myCity, myRating; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); 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 + "')"; int Count; Count=SQLStatement.executeUpdate(strSQL); if (Count==1) out.println("Insertion sucessful"); } catch(SQLException e) { out.println(e.getMessage()); } %>

<% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/SalesDBJava"; try { String myCid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl,"dchao","dchao"); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("cid"); 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 demo: Accessing a Derby database SalesDBJava with username and password. Note: Using Derby, SQL stastement cannot end with “;”

Select CID: <% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/SalesDBJava"; 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()); } %> Derby demo:Populate a Listbox using a ResultSet

Derby demo:Show Records in a Table <% Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/SalesDBJava"; 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()); } %>

Jdbc:odbc: 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:odbc:MyCustomer"; connection = DriverManager.getConnection(DBUrl); 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()); } %>

Show Customer/Orders 1. A HTML form with CID listbox 2. Show selected customer’s record and related records.

<% Connection connection = null; String DBUrl="jdbc:odbc:MyCustomer"; try { String myCid, strSQL, Cname, City, Rating; String OID, SalesPerson; Date OrderDate; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("cid"); 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: ">

OID ODate SalesPerson <% Statement SQLStatementOrder = connection.createStatement(); strSQL="select * from orders where cid='" + myCid + "'"; ResultSet rsOrder = SQLStatementOrder.executeQuery(strSQL); while (rsOrder.next()) { OID=rsOrder.getString("OID"); OrderDate= rsOrder.getDate("ODate"); SalesPerson=rsOrder.getString("SalesPerson"); %> <% } rsOrder.close(); } else { out.println("Customer not exist!"); rs.close(); } catch(SQLException e) { out.println(e.getMessage());} %>