Server-side Web Programming

Similar documents
Server-side Web Programming

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

PERSİSTENCE OBJECT RELATİON MAPPİNG

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

Server-side Web Programming

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

Accessing databases in Java using JDBC

13 Creation and Manipulation of Tables and Databases

SQL and Java. Database Systems Lecture 20 Natasha Alechina

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Lab # 9. Java to Database Connection

Database Application Development

Intoduction to JDBC (Java Database Connectivity-- Connecting to DBMS)

Lecture 2. Introduction to JDBC

Running SQL in Java and PHP

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

Database Application Development

Database Access with JDBC. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Running SQL in Java and PHP

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

! An organized collection of data. ! Can easily be accessed, managed, and updated. ! Data are organized as a set of tables.

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

Working with Databases and Java

Servlet 5.1 JDBC 5.2 JDBC

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

JAVA AND DATABASES. Summer 2018

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Chapter 16: Databases

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

CMPUT 391 Database Management Systems. JDBC in Review. - Lab 2 -

CS221 Lecture: Java Database Connectivity (JDBC)

Transactions & Concurrency Control

DB I. 1 Dr. Ahmed ElShafee, Java course

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

SQL: Programming Midterm in class next Thursday (October 5)

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Database Application Development

INTRODUCTION TO JDBC - Revised spring

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

Programming in Java

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

SQL DML and DB Applications, JDBC

Chapter 13 Introduction to SQL Programming Techniques

INTRODUCTION TO JDBC - Revised Spring

Enterprise Systems. Lecture 02: JDBC. Behzad BORDBAR

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Types of Databases. Types of Databases. Types of Databases. Databases and Web. Databases and Web. Relational databases may also have indexes

Database Application Programs PL/SQL, Java and the Web

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Departamento de Lenguajes y Sistemas Informáticos

WEB SERVICES EXAMPLE 2

Environment Settings on SIT

Java application using JDBC to connect to a database.

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

Databases and SQL Lab EECS 448

CS 161 Computer Security

SQream Connector JDBC SQream Technologies Version 2.9.3

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

Oracle Exam 1z0-809 Java SE 8 Programmer II Version: 6.0 [ Total Questions: 128 ]

MTAT Introduction to Databases

CHAPTER 44. Java Stored Procedures

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

SQL in a Server Environment

Instructor: Jinze Liu. Fall 2008

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

Java Database Connectivity (JDBC) 25.1 What is JDBC?

Tiers (or layers) Separation of concerns

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL in a Server Environment (ii)

Distributed Systems Project 5 Assigned: Friday, April 6, 2012 Due: Friday, April 20, 11:59:59 PM

COMP102: Introduction to Databases, 23

Non-interactive SQL. EECS Introduction to Database Management Systems

Database Applications. SQL/PSM Embedded SQL JDBC

MIS2502: Data Analytics MySQL and SQL Workbench. Jing Gong

JDBC. Sun Microsystems has included JDBC API as a part of J2SDK to develop Java applications that can communicate with databases.

JDBC Java Database Connectivity is a Java feature that lets you connect

JDBC [Java DataBase Connectivity]

Voyager Database Developer s Guide Version 1.0 for Voyager 8.0

Scheme G Sample Question Paper Unit Test 2

Web Applications and Database Connectivity using JDBC (Part II)

Lecture 27 10/30/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Chapter 1 An introduction to relational databases and SQL

JDBC Installation Transactions Metadata

Chapter 9 SQL in a server environment

1. PhP Project. Create a new PhP Project as shown below and click next

Group A: Assignment No 2

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Ghislain Fourny. Information Systems for Engineers 7. The ecosystem around SQL

After completing this course, participants will be able to:

Transcription:

Server-side Web Programming Lecture 13: JDBC Database Programming JDBC Definition Java Database Connectivity (JDBC): set of classes that provide methods to Connect to a database through a database server (using a driver) Query database using SQL syntax, getting list of records that match query Manipulate database by executing SQL commands to modify, insert, and delete records web container database server control servlet JDBC database driver DBMS database JSP JDBC

JDBC Components Major objects involved: Connection: represents connection to a database through a server Statement: represents SQL statement executed on database via that connection ResultSet: represents list of records matching a query Database server Statement object select * from books database ResultSet object productcode title price productcode title price productcode title price Connecting to the Database Server Load the database driver Not necessary in most recent version, but safe thing to do Syntax: Class.forName("driver class").newinstance(); Name of driver class based on url of provider Example: com.mysql.jdbc.driver

Connecting to the Database Server Connect to the database Need to provide username and password Need to provide url of database Usual form: jdbc:server type:url of server/database name Example: jdbc:mysql://localhost/testdb Syntax: connectionobject = DriverManager.getConnection("databaseURL", "username", "password"); Exception Handling in JDBC Any database-related statement may throw an SQLException Your code must put in try/catch block May also need to catch other exceptions ClassNotFoundException for missing database driver Diagnostic message displayed Better idea: Redirect to an error page

Executing Queries Create new statement object using the connection Execute an SQL query using that statement Store results in a ResultSet object Syntax: statement = connection.createstatement(); statement.executequery( SQL query ); Reading ResultSets Can only do simple access: Read in field values from current record Move to next record Syntax to move to next record: ResultSetObject.next(); Returns false if no next record, true otherwise Must execute once before reading first record Usually while loop to read until no more records while(resultsetobject.next()) { code to read in current record }

Reading ResultSets Syntax to read field from current record: value = ResultSetObject.getType(fieldname); Specify type data is to be read in as varchar getstring int getint double getdouble Specify field name used in database Reading ResultSets

Reading ResultSets Once ResultSet read in, can use in own code Display in JSP Store in array for future use, etc. Reading ResultSets Display title and price in next table row Create form that passes productcode of selected book to servlet if button on this row is pressed

Executing Update Statements Syntax: Statement statement = connection.createstatement(); statement.executeupdate( SQL statement ); Example: statement.executeupdate( INSERT INTO books (productcode, title, price) VALUES ( 0004, Green Eggs and Ham, 9.95) ); Inserting Parameter Values User often decides how database is updated Enters parameter on form Parameters used for update

Inserting Parameter Values Read in parameter values Validate if necessary Inserting Parameter Values Insert into SQL statement Will need to use + to append values into SQL statement string Note that string values must be inside to avoid syntax errors 0004 9.95 Green Eggs and Ham Creates string of form: INSERT INTO books (productcode, title, price) VALUES ( 0004, Green Eggs and Ham, 9.95)

Validation and Updates Usually need to validate update with database to avoid problems Don t add item if already in database Don t update or remove if not in database Won t cause database error, but probably want to inform user Checking whether item in database involves query Create query fro item in question If no results then not in database Key idea: use statement of form if(resultsetobject.next()) True if at least one result False if no matches Validation and Updates Example: Validating that no book with given productcode exists in database before adding it Query for book with that productcode Redirect to error page if books.next() is true