Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 33 Advanced Java.

Slides:



Advertisements
Similar presentations
CC SQL Utilities.
Advertisements

Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
1 JDBC Java Database Connectivity. 2 c.pdf
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Introduction To Form Builder
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
1 Chapter 5: Introduction To Form Builder. 2 Forms  Why Do We Use Form Builder?  Why Don’t We Use SQL Only?!
Concepts of Database Management Sixth Edition
With Microsoft Access 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Microsoft Access 2010 Chapter 7 Using SQL.
Access Tutorial 10 Automating Tasks with Macros
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 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Unit J: Creating a Database Microsoft Office Illustrated Fundamentals.
Introduction to Access By Mary Ann Chaney and Alicia Harkleroad.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Concepts of Database Management Seventh Edition
Microsoft Office Access 2003 Số tiết: 30 tiết lý thuyết 60 tiết thực hành Giáo viên: Từ thị Xuân Hiền.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
Concepts of Database Management Seventh Edition
A Skills Approach Access 2010 Getting Started with Access 2010 © 2012 The McGraw-Hill Companies, Inc. All rights reserved Chapter 1.
1 Chapter 25 Java Database Programming. 2 Objectives F To understand the concept of database and database management systems (§25.2). F To understand.
JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.
® Microsoft Office 2013 Access Creating a Database.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
DAT602 Database Application Development Lecture 9 Advanced JDBC 2.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 41 JavaServer Face.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
1 Session 2 Module 3: Scrollable Resultset and Rowsets.
Chapter 25 Java Database Programming
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Chapter 5 Introduction To Form Builder. Lesson A Objectives  Display Forms Builder forms in a Web browser  Use a data block form to view, insert, update,
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Chapter 5 Introduction To Form Builder. Lesson C Objectives  Use sequences to automatically generate primary key values in a form  Create lists of values.
1 JDBC – Java Database Connectivity CS , Spring 2010.
COMPREHENSIVE Access Tutorial 1 Creating a Database.
Computers Are Your Future Tenth Edition Spotlight 5: Microsoft Office Copyright © 2009 Pearson Education, Inc. Publishing as Prentice Hall1.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
1 Chapter 6: Creating Oracle Data Block Forms. 2 Forms  Application with a graphical user interface that looks like a paper form  Used to insert, update,
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Chapter 38 Advanced Java Database Programming
Chapter 41 Advanced Java Database Programming
Access Creating a Database
Access Creating a Database
Objectives In this lesson, you will learn about:
Microsoft Office Access 2003
Unit J: Creating a Database
JDBC II IS
Presentation transcript:

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java Database Programming

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Objectives F To execute SQL statements in a batch mode (§33.8 Optional). F To process updateable and scrollable result sets (§33.9 Optional). F To store and retrieve images in JDBC (§33.10 Optional).

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Creating an Interactive SQL Client Connect to any JDBC data source. SQL Client Run Optional Entering and executing SQL commands interactively The execution result is displayed for the SELECT queries, and the execution status is displayed for the non-SELECT commands.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Batch Updates To improve performance, JDBC 2 introduced the batch update for processing nonselect SQL commands. A batch update consists of a sequence of nonselect SQL commands. These commands are collected in a batch and submitted to the database all together. Statement statement = conn.createStatement(); // Add SQL commands to the batch statement.addBatch("create table T (C1 integer, C2 varchar(15))"); statement.addBatch("insert into T values (100, 'Smith')"); statement.addBatch("insert into T values (200, 'Jones')"); // Execute the batch int count[] = statement.executeBatch(); The executeBatch() method returns an array of counts, each of which counts the number of the rows affected by the SQL command. The first count returns 0 because it is a DDL command. The rest of the commands return 1 because only one row is affected. Optional

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Copying Text Files to Table Write a program that gets data from a text file and copies the data to a table. The text file consists of the lines, each of which corresponds to a row in the table. The fields in a row are separated by commas. The string values in a row are enclosed in single quotes. You can view the text file by clicking the View File button and copy the text to the table by clicking the Copy button. The table must already be defined in the database. CopyFileToTable Run

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Scrollable and Updateable Result Set The result sets used in the preceding examples are read sequentially. A result set maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next() method moves the cursor forward to the next row. This is known as sequential forward reading. It is the only way of processing the rows in a result set that is supported by JDBC 1. With JDBC 2, you can scroll the rows both forward and backward and move the cursor to a desired location using the first, last, next, previous, absolute, or relative method. Additionally, you can insert, delete, or update a row in the result set and have the changes automatically reflected in the database. Optional

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Creating Scrollable Statements To obtain a scrollable or updateable result set, you must first create a statement with an appropriate type and concurrency mode. For a static statement, use Statement statement = connection.createStatement (int resultSetType, int resultSetConcurrency); For a prepared statement, use PreparedStatement statement = connection.prepareStatement (String sql, int resultSetType, int resultSetConcurrency); The resulting set is scrollable ResultSet resultSet = statement.executeQuery(query); Optional TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE CONCUR_READ_ONLY CONCUR_UPDATABLE

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Scrolling and Updating Table Develop a useful utility that displays all the rows of a database table in a JTable and uses a scrollable and updateable result set to navigate the table and modify its contents. defined in the database. TestTableEditor Run Insert a new row TableEditorNewRecordDialog

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved SQL BLOB and CLOB Types Database can store not only numbers and strings, but also images. SQL3 introduced a new data type BLOB (Binary Large OBject) for storing binary data, which can be used to store images. Another new SQL3 type is CLOB (Character Large OBject) for storing a large text in the character format. JDBC 2 introduced the interfaces java.sql.Blob and java.sql.Clob to support mapping for these new SQL types. JBDC 2 also added new methods, such as getBlob, setBinaryStream, getClob, setBlob, and setClob, in the interfaces ResultSet and PreparedStatement to access SQL BLOB, and CLOB values. To store an image into a cell in a table, the corresponding column for the cell must be of the BLOB type. For example, the following SQL statement creates a table whose type for the flag column is BLOB. create table Country(name varchar(30), flag blob, description varchar(255)); Optional BLOB CLOB

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Storing and Retrieving Images in JDBC To insert a record with images to a table, define a prepared statement like this one: PreparedStatement pstmt = connection.prepareStatement( "insert into Country values(?, ?, ?)"); Images are usually stored in files. You may first get an instance of InputStream for an image file and then use the setBinaryStream method to associate the input stream with a cell in the table, as follows: // Store image to the table cell File file = new File(imageFilenames[i]); InputStream inputImage = new FileInputStream(file); pstmt.setBinaryStream(2, inputImage, (int)(file.length())); To retrieve an image from a table, use the getBlob method, as shown below: // Store image to the table cell Blob blob = rs.getBlob(1); ImageIcon imageIcon = new ImageIcon( blob.getBytes(1, (int)blob.length())); Optional Store image Retrieve image

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Scrolling and Updating Table In this example, you will create a table, populate it with data, including images, and retrieve and display images. The table is named Country. Each record in the table consists of three fields: name, flag, and description. Flag is an image field. The program first creates the table and stores data to it. Then the program retrieves the country names from the table and adds them to a combo box. When the user selects a name from the combo box, the country’s flag and description are displayed. StoreAndRetrieveImage Run