CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular.

Slides:



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

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
© Marty Hall, Larry Brown Web core programming 1 Introduction to JDBC.
1 JDBC: Part I Attribution These slides are based on three primary sources: –Sun JDBC Tutorial URL: jdbc/TOC.htmlhttp://java.sun.com/docs/books/tutorial/
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
JDBC1 JDBC Introduction JDBC provides a standard library for accessing relational databases –API standardizes Way to establish.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 37 Java Database Programming.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Accessing Databases with JDBC. Introduction to JDBC JDBC provides a standard library for accessing databases by using the JDBC API. JDBC standardizes.
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
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.
Think Possibility Integrating Web Applications With Databases.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Java: JDBC INE2720 Web Application Software Development Essential Materials.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
JDBC1 Course Material Usage Rules PowerPoint slides for use only in for-credit courses at degree-granting institutions –Slides not.
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.
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.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
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.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, All Rights Reserved.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
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.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Lec - 14.
JDBC 15-Apr-18.
CS320 Web and Internet Programming Database Access with JDBC
JDBC 21-Aug-18.
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC Example.
Presentation transcript:

CSC 2720 Building Web Applications JDBC

JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular data source (e.g. relational databases) from Java code.  For examples, it offers  Methods to establish connection to databases  Methods to initiating queries  Methods to create stored (parameterized) queries  Data structures to store queried results  Independent of SQL  JDBC class is located in java.sql package

JDBC Drivers JDBC consists of two parts:  JDBC API – a purely Java-based API  JDBC Driver Manager which communicates with vendor- specific drivers that perform the real communication with the database.  Translation to vendor format is performed on the client  No changes needed to server  Driver (translator) needed on client

Seven Steps in Using JDBC 1.Load the driver 2.Define the Connection URL 3.Establish the Connection 4.Create a Statement object 5.Execute a query 6.Process the results 7.Close the connection

Step 1: Loading Driver try { // e.g.: Loading a driver written for MySql // Only needs to perform once. Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException cnfe) { System.out.println("Error loading driver: " + cnfe); }  The driver is a Java class provided by the database vendor or by third party developers.  Typically packaged into a.jar file  Different drivers have different names (refer to the documentation of the driver)  You must include the JAR file containing the driver in the CLASSPATH (or in the proper folder in NetBeans project)

Step 2: Defining Connection URL String host = "localhost"; String dbName = "someName"; String mySqlUrl = "jdbc:mysql://" + host + "/" + dBName; String jdbcOdbcUrl = "jdbc:odbc:" + dbName;  URL format is driver specific  Typically contains  Protocol and subprotocol  Hostname (+ port number if there is any)  Database name

Establishing Connection String dbUrl = "jdbc:mysql://localhost/Person"; String username = "root"; String password = "secret"; try { Connection connection = DriverManager.getConnection(dbUrl, username, password); } catch (SQLException sqle) { // … print error messages }  getConnection can throw SQLException (Most methods in the java.sql package throws SQLException )

Creating SQL statements // connection has to be established successfully first Statement statement = connection.createStatement();  A Statement object is what sends your SQL statement to the DBMS.  You can use the same Statement object to execute all your SQL statements.

Executing SQL Statements  First, prepare an SQL statement as a String (does not need statement terminator)  Call executeQuery(sqlStatement) to execute SELECT statement.  Call executeUpdate(sqlStatement) to execute statements that would modify the database (e.g.: UPDATE, INSERT, DELETE)  Use setQueryTimeout to specify a maximum delay to wait for results String query = "SELECT col1, col2, col3 FROM sometable"; ResultSet resultSet = statement.executeQuery(query); String updateQuery = "INSERT INTO Messages" + "VALUES ('CJ', '')"; int count = statement.executeUpdate(updateQuery);

Processing ResultSet  executeQuery() always returns an instance of ResultSet  ResultSet is forward-only and read-only.  Resultset has a "cursor" that points to the "current rows".  Initially it is not pointing to any row ResultSet rs = statement.executeQuery( "SELECT name, , message FROM Messages" ); name messages msg 1 msg 2 Column 1 Column 2 Column 3 Row Pointer (cursor)

Processing ResultSet  Calling next() the first time makes the cursor points to the first row (If there is one).  next() returns false if there is no more row in the result set. Otherwise it returns true. ResultSet rs = statement.executeQuery( "SELECT name, , message FROM Messages" ); rs.next(); name messages msg 1 msg 2 Column 1 Column 2 Column 3 Row Pointer (cursor)

Processing ResultSet  First column has index 1, not 0  ResultSet provides various getXxx(int idx) and getXxx(String name) methods that take a column index or column name and returns the data of appropriate type.  If no records found, executeQuery() returns an empty ResultSet instead of null. // Typical way to retrieve data from a result set while(rs.next()) { System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3)); }

Closing Connection connection.close();  When you are done using the database, call close() to close the connection.  As opening a connection is expensive, postpone this step if additional database operations are expected.

Microsoft Access Example  Northwind sample database Northwind.mdb located in C:\Program Files\Microsoft Office\Office\Samples

MS Access Example: Create a data source  Create System (Data Source Name) DSN through ODBC data source (Run "odbcad32.exe")

JDBC-ODBC Bridge ODBC (Open DataBase Connectivity) –A set of APIs for Database access –Originally, designed for windows platforms –Now, it extends to non-windows platforms –Using C interfaces More information: –

import java.sql.*; public class OdbcAccessDemo { public static void main(String[] args) { try { // This driver comes with JDK Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // No user and password required Connection connection = DriverManager.getConnection("jdbc:odbc:Northwind","",""); Statement statement = connection.createStatement(); String query = "SELECT FirstName, LastName FROM Employees"; ResultSet rs = statement.executeQuery(query); while (rs.next()) { System.out.println(rs.getString(1) + " " + rs.getString(2)); } connection.close(); } catch (ClassNotFoundException cnfe) { System.err.println("Couldn't find class file" + cnfe); } catch (SQLException sqle) { System.err.println("SQL Exception: " + sqle); } MS Access Example (Continued)

ResultSet  Overview  A ResultSet contains the results of the SQL query  Represented by a table with rows and columns  In JDBC 1.0 you can only proceed forward through the rows using next Useful Methods  All methods can throw a SQLException  close  Releases the JDBC and database resources  The result set is automatically closed when the associated Statement object executes a new query

ResultSet (Useful Methods)  getMetaDataObject  Returns a ResultSetMetaData object containing information about the columns in the ResultSet  next  Attempts to move to the next row in the ResultSet  If successful true is returned; otherwise, false  The first call to next positions the cursor a the first row  Calling next clears the SQLWarning chain  getWarnings  Returns the first SQLWarning or null if no warnings occurred

ResultSet (Useful Methods)  findColumn  Returns the corresponding integer value corresponding to the specified column name  Column numbers in the result set do not necessarily map to the same column numbers in the database   getXxx  Returns the value from the column specified by column name or column index as an Xxx Java type  Returns 0 or null, if the value is a SQL NULL  Legal getXxx types:  wasNull  Used to check if the last getXxx read was a SQL NULL double byte int Date String float short long Time Object

Using Statement (Overview)  Through the Statement object, SQL statements are sent to the database.  Two types of statement objects are available:  Statement  for executing a simple SQL statements  PreparedStatement  for executing a precompiled SQL statement passing in parameters

Useful Statement Methods  executeQuery  Executes the SQL query and returns the data in a table (ResultSet)  The resulting table may be empty but never null ResultSet results = statement.executeQuery("SELECT a, b FROM table");  executeUpdate  Used to execute for INSERT, UPDATE, or DELETE SQL statements  The return is the number of rows that were affected in the database  Also supports Data Definition Language (DDL) statements CREATE TABLE, DROP TABLE and ALTER TABLE int rows = statement.executeUpdate( "DELETE FROM employees WHERE status=0");

Useful Statement Methods (Continued)  execute  Generic method for executing stored procedures and prepared statements  Rarely used (for multiple return result sets)  The statement execution may or may not return a ResultSet (use statement.getResultSet). If the return value is true, two or more result sets were produced  getMaxRows/setMaxRows  Determines the number of rows a ResultSet may contain  Unless explicitly set, the number of rows are unlimited  getQueryTimeout/setQueryTimeout  Specifies the amount of a time a driver will wait for a STATEMENT to complete before throwing a SQLException

Prepared Statements (Precompiled Queries)  Idea  If you are going to execute similar SQL statements multiple times, using “prepared” (parameterized) statements is more efficient  Create a statement in standard form that is sent to the database for compilation before actually being used  Each time you use it, you simply replace some of the marked parameters using the setXxx methods  To execute a "prepared statement", use the following methods (without parameters)  execute()  executeQuery()  executeUpdate()

Prepared Statement, Example Connection connection = DriverManager.getConnection(url, user, password); PreparedStatement statement = connection.prepareStatement("UPDATE employees " + "SET salary = ? " + "WHERE id = ?"); int[] newSalaries = getSalaries(); int[] employeeIDs = getIDs(); for (int i = 0; i < employeeIDs.length; i++) { statement.setInt(1, newSalaries[i]); statement.setInt(2, employeeIDs[i]); statement.executeUpdate(); }

Useful Prepared Statement Methods  setXxx  Sets the indicated parameter (?) in the SQL statement to the specified value  clearParameters  Clears all set parameter values in the statement  Handling Servlet Data  Query data obtained from a user through an HTML form may have SQL or special characters that may require escape sequences  To handle the special characters, pass the string to the PreparedStatement setString method which will automatically escape the special characters in the string

Exception Handling  SQL Exceptions  Nearly every JDBC method can throw a SQLException in response to a data access error  If more than one error occurs, they are chained together  SQL exceptions contain:  Description of the error, getMessage  The SQLState (Open Group SQL specification) identifying the exception, getSQLState  A vendor-specific integer error code, getErrorCode  A chain to the next SQLException, getNextException

SQL Exception Example try {... // JDBC statement. } catch (SQLException sqle) { while (sqle != null) { System.out.println("Message: " + sqle.getMessage()); System.out.println("SQLState: " + sqle.getSQLState()); System.out.println("Vendor Error: " + sqle.getErrorCode()); sqle.printStrackTrace(System.out); sqle = sqle.getNextException(); }

SQL Warnings  SQLWarnings are rare, but provide information about the database access warnings  Chained to object whose method produced the warning  The following objects can receive a warning:  Connection  Statement  ResultSet  Call getWarning to obtain the warning object, and getNextWarning (on the warning object) for any additional warnings  Warnings are cleared on the object each time the statement is executed

SQL Warning, Example ResultSet results = statement.executeQuery(someQuery); SQLWarning warning = statement.getWarnings(); while (warning != null) { System.out.println("Message: " + warning.getMessage()); System.out.println("SQLState: " + warning.getSQLState()); System.out.println("Vendor Error: " + warning.getErrorCode()); warning = warning.getNextWarning(); } while (results.next()) { int value = rs.getInt(1);... // Call additonal methods on result set. SQLWarning warning = results.getWarnings(); while (warning != null) { System.out.println("Message: " + warning.getMessage()); System.out.println("SQLState: " + warning.getSQLState()); System.out.println("Vendor Error: " + warning.getErrorCode()); warning = warning.getNextWarning(); }

Summary  Why use database?  How to use JDBC?  In JDBC 1.0, ResultSet is forward-only and read-only.  Be sure to handle the situation where getXxx returns a NULL  Understand the relationship among:  Connection object  Statement object  ResultSet object  By default, a connection is auto-commit  SQL Exceptions and Warnings are chained together

On-line Resources  Sun’s JDBC Site   JDBC Tutorial   List of Available JDBC Drivers   API for java.sql (See JDK Documentation)  MySQL 