CSCI 6962: Server-side Design and Programming JDBC Database Programming.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
1 JDBC Java Database Connectivity. 2 c.pdf
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CSCI 6962: Server-side Design and Programming Course Introduction and Overview.
CSCI 6962: Server-side Design and Programming
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
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.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
CSCI 6962: Server-side Design and Programming Secure Web Programming.
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CSCI 6962: Server-side Design and Programming Support Classes and Shopping Carts.
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.
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Lec_6 Manipulating MySQL Databases with PHP PHP Programming with MySQL.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Analysis of SQL injection prevention using a filtering proxy server By: David Rowe Supervisor: Barry Irwin.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Chapter 8 Databases.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
DT228/3 Web Development Databases. Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database.
© 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.
Data Base Connectivity From JAVA Creating a Java program that access mySQL is not difficult. Your Java program must can a number of functions to facilitate.
CSCI 6962: Server-side Design and Programming JSF DataTables and Shopping Carts.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tasks Needed for MissionMapEditor Martin Q. Zhao September 18, 2010.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Web Programming MySql JDBC Web Programming.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
1 JDBC – Java Database Connectivity CS , Spring 2010.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL Injection By Wenonah Abadilla. Topics What is SQL What is SQL Injection Damn Vulnerable Web App SQLI Demo Prepared Statements.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
ASP.NET Programming with C# and SQL Server First Edition
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.
Unit 9.1 Learning Objectives Data Access in Code
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
Introduction to Server-Side Web Development using JSP and Databases
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Super Market Management
Bolat Azamat, Kim Dongmin
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

CSCI 6962: Server-side Design and Programming JDBC Database Programming

Outline Introduction to JDBC Connecting to a database server Executing queries and reading result sets Prepared statements Executing update statements Synchronized database access

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 JSF page Managed bean database database driver DBMS database server 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 database Statement object select * from widgets ResultSet object ID name price ID name price ID name 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 provider (see their documentation) –Derby: org.apache.derby.jdbc.ClientDriver –MySQL: com.mysql.jdbc.Driver

Connecting to the Database Server Need to provide url of database Form: jdbc:servertype:serverURL:port/databasename –Derby: jdbc:derby://localhost:1527/DBname –MySQL: jdbc:mysql://localhost:3306/Dbname

Connecting to the Database Server Syntax: connectionobject = DriverManager.getConnection("databaseURL", "username", "password"); Derby example: Should close connection when done connectionobject.close();

Exception Handling in JDBC Any database-related statement may throw 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

Connecting to the Database Server

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 field name used in database Specify type data is to be read in as varChar  getString int  getInt double  getDouble

Widget Example Goal: Display all Widgets in datatable –Same as before, but no longer hardwired Database access in Widget class getAllWidgets: –Queries for all widgets, extracts ID of each widget –Constructs widget with that ID, adds to list Constructor: –Queries for widget with given ID –Extracts name, price to set its properties

getAllWidgets Code Execute SQL query for all widgets Loop through all results Get the value of the ID field Close connection and return list Use it to construct a widget and add to the list

Inserting Parameter Values Queries often based on variables –Example: finding widget with given ID Must insert values into query –If value is string, must make sure quote marks ‘ ‘ surround the value! Insert given ID into the query

Constructor Code Execute SQL query for widgets with given ID Advance to first (and only) result Extract name as string and price as double

Prepared Statements Tell database server basic form of statements in advance –Database server can do all work for that type of statement once “Fill in blanks” for actual values when actually execute statement –Easier syntax than inserting manually –More secure against SQL injection attacks! Example: Extracting widget with given ID –All statements of form: "SELECT * FROM widgets WHERE ID = ____“

Prepared Statements Declare as PreparedStatement PreparedStatement lookup = null; Define prepared statement using connection.prepareStatement(template); Place ‘?’ where actual values will be inserted lookup = connection.prepareStatement("SELECT * FROM widgets WHERE ID = ?");

Prepared Statements Use setType (index, value) to insert value into statement lookup.setString(1, ID); Execute query on the prepared statement resultsConstructor = lookup.executeQuery(); Type of field (like get method in ResultSet) Which ‘?’ to insert the value into Insert ISBN into first (and only) ‘?’ in lookup

Executing Update Statements Syntax: int chng = statement.executeUpdate(SQL) or int chng = preparedstatement.executeUpdate() Returns number of records changed by statement –Often used for validation

Updating Price in Widget Class

Performing Update from Bean Call static Widget method with ID, price If price changed, display new inventory If no change, display error message in JSF page

Synchronized Database Access Database updates occur “simultaneously” on busy sites Can interfere with one another Example: Quantity update after purchase –Query for previous quantity –Subtract 1 –Update database with new quantity

Synchronized Database Access Java runs separate clients as “parallel” threads which execute “simultaneously” –Processor swaps back and forth between threads Problem if following sequence occurs: –Current quantity = 100 –Client 1 code to get current quantity executes (value = 100) –Processor swaps to client 2 thread –Client 2 code to get current quantity (value still = 100) –Client 2 code sets new quantity to 99 and stores in database –Processor swaps back to client 1 thread –Client 1 code also sets new quantity to 99 and stores in database!

Synchronized Database Access Get quantity Quantity = 100 Client 1 thread Get quantity Quantity = 100 Client 2 thread Set quantity = 99 Store 99 in database Set quantity = 99 Store 99 in database Problem: this code should not be interrupted!

Synchronized Database Access Can declare sections of code to be synchronized –Only one thread may execute it at a time –Another thread cannot start the code until the first has finished it Syntax: synchronized(object) { code } Only one thread at a time should be able to execute this code on this object

Synchronized Database Access