CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.

Slides:



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

Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
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.
1 JDBC Java Database Connectivity. 2 c.pdf
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
1 Oracle Database Applications Database Connectivity.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
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.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
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.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
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.
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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
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. 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.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
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.
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.
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
JDBC (Java Database Connectivity)
CS122B: Projects in Databases and Web Applications Winter 2016
JDBC - Resultset The java.sql.ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the.
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Database Access with JDBC
Interacting with Database
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
Web Technologies IT230 Dr Mohamed Habib.
CS320 Web and Internet Programming Database Access with JDBC
Database JDBC Overview CS Programming Languages for Web Applications
JDBC 21-Aug-18.
JDBC.
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

CONTROLPANEL

Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational database by using the java programming language. It includes a framework where we different drivers can be installed dynamically to access different databases especially relational databases.

This java.sql package contains API for the following : 1 Making a connection with a database with the help of DriverManager class a) DriverManager class: It helps to make a connection with the driver. b) SQLPermission class: It provides a permission when the code is running within a Security Manager, such as an applet. It attempts to set up a logging stream through the DriverManager class. c) Driver interface : This interface is mainly used by the DriverManager class for registering and connecting drivers based on JDBC technology. d). DriverPropertyInfo class : This class is generally not used by the general user.

2). Sending SQL Parameters to a database : a). Statement interface: It is used to send basic SQL statements. b). PreparedStatement interface: It is used to send prepared statements or derived SQL statements from the Statement object. c). CallableStatement interface : This interface is used to call database stored procedures. d). Connection interface : It provides methods for creating statements and managing their connections and properties. e). Savepoint : It helps to make the savepoints in a transaction.

3). Updating and retrieving the results of a query: a). ResultSet interface: This object maintains a cursor pointing to its current row of data. The cursor is initially positioned before the first row. The next method of the resultset interface moves the cursor to the next row and it will return false if there are no more rows in the ResultSet object. By default ResultSet object is not updatable and has a cursor that moves forward only.

4.) Providing Standard mappings for SQL types to classes and interfaces in Java Programming language. a). Array interface: It provides the mapping for SQL Array. b). Blob interface : It provides the mapping for SQL Blob. c). Clob interface: It provides the mapping for SQL Clob. d). Date class: It provides the mapping for SQL Date. e). Ref interface: It provides the mapping for SQL Ref. f). Struct interface: It provides the mapping for SQL Struct. g). Time class: It provides the mapping for SQL Time. h). Timestamp: It provides the mapping for SQL Timestamp. i). Types: It provides the mapping for SQL types.

5). Metadata a). DatabaseMetaData interface: It keeps the data about the data. It provides information about the database. b). ResultSetMetaData: It gives the information about the columns of a ResultSet object. c). ParameterMetaData: It gives the information about the parameters to the PreparedStatement commands

6). Exceptions a). SQLException: It is thrown by the mehods whenever there is a problem while accessing the data or any other things. b). SQLWarning: This exception is thrown to indicate the warning. c). BatchUpdateException: This exception is thrown to indicate that all commands in a batch update are not executed successfully. d). DataTruncation: It is thrown to indicate that the data may have been truncated.

Custom mapping an SQL user- defined type (UDT) to a class in the java programming language. a). SQLData interface: It gives the mapping of a UDT to an intance of this class. b). SQLInput interface: It gives the methods for reading UDT attributes from a stream. c). SQLOutput: It gives the methods for writing UDT attributes back to a stream.

JDBC Classes DriverManager Manages JDBC Drivers Used to Obtain a connection to a Database Types Defines constants which identify SQL types Date Used to Map between java.util.Date and the SQL DATE type Time Used to Map between java.util.Date and the SQL TIME type TimeStamp Used to Map between java.util.Date and the SQL TIMESTAMP type

JDBC Interfaces Driver All JDBC Drivers must implement the Driver interface. Used to obtain a connection to a specific database type Connection Represents a connection to a specific database Used for creating statements Used for managing database transactions Used for accessing stored procedures Used for creating callable statements Statement Used for executing SQL statements against the database

JDBC Interfaces ResultSet Represents the result of an SQL statement Provides methods for navigating through the resulting data PreparedStatement Similar to a stored procedure An SQL statement (which can contain parameters) is compiled and stored in the database CallableStatement Used for executing stored procedures DatabaseMetaData Provides access to a database's system catalogue ResultSetMetaData Provides information about the data contained within a ResultSet

Using JDBC To execute a statement against a database, the following flow is observed Load the driver (Only performed once) Obtain a Connection to the database (Save for later use) Obtain a Statement object from the Connection Use the Statement object to execute SQL. Updates, inserts and deletes return Boolean. Selects return a ResultSet Navigate ResultSet, using data as required Close ResultSet Close Statement Do NOT close the connection The same connection object can be used to create further statements A Connection may only have one active Statement at a time. Do not forget to close the statement when it is no longer needed. Close the connection when you no longer need to access the database

Connecting to a Database Once a Driver is loaded, a connection can be made to the database The connection is defined by URL The URL has the following form: jdbc:driver:databasename Examples: jdbc:odbc:MyOdbcDatabase jdbc:postgres:WebsiteDatabase jdbc:oracle:CustomerInfo A connection is obtained in the following manner: Connection aConnection = DriverManager.getConnection("jdbc:odbc:myDatabase"); Overloaded versions of the getConnection method allow the specification of a username and password for authentication with the database.

Using a Connection The Connection interface defines many methods for managing and using a connection to the database public Statement createStatement() public PreparedStatement prepareStatement(String sql) public void setAutoCommit(boolean) public void commit() public void rollback() public void close() The most commonly used method is createStatement() When an SQL statement is to be issued against the database, a Statement object must be created through the Connection

Using a Statement The Statement interface defines two methods for executing SQL against the database public ResultSet executeQuery(String sql) public int executeUpdate(String sql) executeQuery returns a ResultSet All rows and columns which match the query are contained within the ResultSet The developer navigates through the ResultSet and uses the data as required. executeUpdate returns the number of rows changed by the update statement This is used for insert statements, update statements and delete statements

Using a ResultSet The ResultSet interface defines many navigation methods public boolean first() public boolean last() public boolean next() public boolean previous() The ResultSet interface also defines data access methods public int getInt(int columnNumber)-- Note: Columns are numbered public int getInt(String columnName)-- from 1 (not 0) public long getLong(int columnNumber) public long getLong(String columnName) public String getString(int columnNumber) public String getString(String columnName) There are MANY more methods. Check the API documentation for a complete list

SQL Types/Java Types Mapping SQL TypeJava Type CHARString VARCHARString LONGVARCHARString NUMERICjava.Math.BigDecimal DECIMALjava.Math.BigDecimal BITboolean TINYINTint SMALLINTint INTEGERint BIGINTlong REALfloat FLOATdouble DOUBLEdouble BINARYbyte[] VARBINARYbyte[] DATEjava.sql.Date TIMEjava.sql.Time TIMESTAMPjava.sql.Timestamp

Difference between Statement & PreparedStatement There are four steps for the execution of query : Query is parsed, Query is compile, Query is optimized and Query is executed. In case of statement interface these four steps are performed,each time when query is submitted for execution. But in case of prepared statement first three steps are performed only once, when the query in initially submitted. Only the last step is performed each time query is submitted(in subsequence submissions), i.e if same query is submitted to be execute with different values multiple times then prepared statement interface provides better perfromance as compared to statement interface.*

Comparing with the execution control of Statement – 1)executeXXX() method is invoked on the SQL statement executeUpdate(), executeQuery() 2) The Statement object submits SQL Statement to DB 3) The DB compiles the given SQL statement 4) An execution plan is prepared by DB to execute the statement 5) Execution plan is then executed. If SQL contains SELECT, the results are cached in buffer–Results are sent to the Statement object 6) Finally, response is sent to java application executeUpdate() methods

Normal statement execution –Compilation includes syntax check, name validation etc.–After validation, query optimizer prepares execution plan Returns best execution plan SQL statement goes thru above every time it is executed If same query is executed multiple times, – Then execution plan can be saved and reuse it– This stored execution plan is known as pre-compiled statement The PreparedStatement interface is designed for it –Hence, their execution is much faster compared to statement

when using PreparedStatement –Must be associated with one connection –It represents the execution plan, need to pass parameters –On connection close, it implicitly gets closed –Execution of query is processed as follows The connection object submits the SQL statement todatabase Database compiles the given statement Execution plan is prepared by the database Database returns the execution plan with unique id–Connection object– The setXXX() methods are used to set the parameters of SQL– executeXXX () method is invoked to execute the SQL –Database executes the execution plan with supplied parameters– Finall the result is sent to the java application

Advantages –Improves the application performance compared to Statement() –Inserts or updates SQL99 data types CLOB, BLOB –Provides a programming approach to set the values Disadvantages –Can represent only one SQL statement at a time –Can not execute more than one SQL statement in a single PreparedStatement Situations when it is useful to use –A single query is executed multiple times –When a query consists of numerous parameters and complex types