JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.

Slides:



Advertisements
Similar presentations
Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Advertisements

JDBC – Java DataBase Connectivity CSE432 Object Oriented Software Engineering.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
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.
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.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
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.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
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.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
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.
Session 30 Basics of JDBC. Java Simplified / Session 30 / 2 of 33 Review A Swing menu consists of a menubar, menuitems and menus. Trees are used to depict.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
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.
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)
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
JDBC.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
JDBC – Java DataBase Connectivity
DEPTT. OF COMP. SC & APPLICATIONS
Interacting with Database
Lec - 14.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
JDBC Database Management Database connectivity
Web Technologies IT230 Dr Mohamed Habib.
Introduction to Programming with Java
JDBC – Java DataBase Connectivity
JDBC – Java DataBase Connectivity
Introduction to Server-Side Web Development using JSP and Databases
Objectives In this lesson, you will learn about:
Interacting with Database
Java Database Connectivity
CS3220 Web and Internet Programming Database Access with JDBC
JDBC – Java DataBase Connectivity
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE

2 Agenda Overview of Databases and Java Overview of JDBC JDBC APIs Types of JDBC Drivers Other Database Techniques

3 Introduction JDBC is Java application programming interface that allows the Java programmers to access database management system from Java code. It was developed by JavaSoft, a subsidiary of Sun Microsystems. It is a java API which enables the java programs to execute SQL statements. JDBC provides methods for querying and updating the data in Relational Database Management system such as SQL, Oracle etc.

4Introduction In short JDBC helps the programmers to write java applications that manage these three programming activities: 1. It helps us to connect to a data source, like a database. 2. It helps us in sending queries and updating statements to the database and 3. Retrieving and processing the results received from the database in terms of answering to your query.

5 Product Components of JDBC JDBC has four Components: 1. The JDBC API. The JDBC application programming interface provides the facility for accessing the relational database from the Java programming language 2. The JDBC Driver Manager.(Backbone) The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver 3. The JDBC Test Suite. The function of JDBC driver test suite is to make ensure that the JDBC drivers will run user's program or not 4. The JDBC-ODBC Bridge. The JDBC-ODBC bridge, also known as JDBC type 1 driver is a database driver that utilize the ODBC driver to connect the database. This driver translates JDBC method calls into ODBC function calls

6 JDBC Architecture

7 The JDBC API uses a Driver Manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver is used to access each data source. The Driver Manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases. The location of the driver manager with respect to the JDBC drivers and the servlet is shown in Figure

8 Layers of the JDBC Architecture A JDBC driver translates standard JDBC calls into a network or database protocol or into a database library API call that facilitates communication with the database

9 Type 1 JDBC-ODBC Bridge Type 1 drivers act as a "bridge" between JDBC and another database connectivity mechanism such as ODBC. The JDBC- ODBC bridge provides JDBC access using most standard ODBC drivers. In this driver the java statements are converted to a jdbc statements. JDBC statements calls the ODBC by using the JDBC- ODBC Bridge

10 Type 2 Java to Native API Type 2 drivers use the Java Native Interface (JNI) to make calls to a local database library API. This driver converts the JDBC calls into a database specific call for databases such as SQL, ORACLE etc. This driver communicates directly with the database server. It requires some native code to connect to the database. Type 2 drivers are usually faster than Type 1 drivers.

11 Type 3 Java to Network Protocol Or All- Java Driver Type 3 drivers are pure Java drivers that use a proprietary network protocol to communicate with JDBC middleware on the server. The middleware then translates the network protocol to database- specific function calls. Type 3 drivers are the most flexible JDBC solution because they do not require native database libraries on the client and can connect to many different databases on the back end

12 Type 4 Java to Database Protocol Type 4 drivers are pure Java drivers that implement a proprietary database protocol (like Oracle's SQL*Net) to communicate directly with the database. Like Type 3 drivers, they do not require native database libraries and can be deployed over the Internet without client installation. One drawback to Type 4 drivers is that they are database specific.

13 JDBC Statements A JDBC statement object is used to send your SQL statement to the database server A JDBC statement is associated with an open connection and not any single SQL statement JDBC provides three classes of SQL statement – Statement – PreparedStatement – CallableStatement

14 Creating JDBC Statement Statement stmt = con.createStatement(); String createCS4400 = “Create table CS4400 “+ “(SSN Integer not null, Name VARCHAR(32), “+ “Marks Integer)”; stmt.executeUpdate(createCS4400); String insertCS4400 = “Insert into CS4400 values “+“( ,abc,100)”; stmt.executeUpdate(insertCS4400);

15 Create JDBC Statement (contd..) String queryCS4400 = “select * from CS4400”; ResultSet rs = Stmt.executeQuery(queryCS4400); While (rs.next()) { int ssn = rs.getInt(“SSN”); String name = rs.getString(“NAME”); int marks = rs.getInt(“MARKS”); } Note: column number can also be used in place of column name.Refer to java.sql.ResulSet API for more details

16 Prepared Statement Unlike “Statement” it is given a SQL statement when it is created. Used when you want to execute “Statement” object many times E.g String insert = “Insert into CS4400 (?,?,?)”; PreparedStatement stmt2 = con.prepareStatement(insert); stmt2.setInt(1, ); stmt2.setString(2,abc); stmt2.setInt(3,100); stmt2.executeUpdate();

17 Prepared Statement (cont..) Executing Select Statement e.g String query =“SELECT Name from CS4400 where SSN=?”; PreparedStatement stmt2 = con.prepareStatement(query); Stmt2.setInt(1,SSN); ResultSet rs = stmt2.executeUpdate(); While (rs.next()) System.out.println(rs.getString(Name);

18 Callable Statement Used for executing stored procedures Example String createProcedure = “Create Procedure ShowGoodStudents” + “as Select Name from CS4400 where Marks > 90)”; Stmt.executeUpdate(createProcedure); CallableStatement cs = con.prepareCall(“(call ShowGoodStudents)”); ResultSet rs = cs.executeQuery();

19 Basic steps in writing a JDBC Application JDBC Steps to be followed while writing JDBC program: * Loading Driver * Establishing Connection * Executing Statements * Getting Results * Closing Database Connection

20 Loading Driver Loading Database driver is very first step towards making JDBC connectivity with the database. It is necessary to load the JDBC drivers before attempting to connect to the database. The JDBC drivers automatically register themselves with the JDBC system when loaded. Here is the code for loading the JDBC driver: Class.forName(driver).newInstance();

21 Establishing Connection In the above step we have loaded the database driver to be used. Now its time to make the connection with the database server. In the Establishing Connection step we will logon to the database with user name and password. Following code we have used to make the connection with the database: con = DriverManager.getConnection(url+db, user, pass);

22 Executing Statements In the previous step we established the connection with the database, now its time to execute query against database. You can run any type of query against database to perform database operations. In this example we will select all the rows from employee table. Here is the code that actually execute the statements against database: ResultSet res = st.executeQuery( "SELECT * FROM employee" );

23 Getting Results In this step we receives the result of execute statement. In this case we will fetch the employees records from the recordset object and show on the console. Here is the code: while (res.next()) { String employeeName = res.getInt( " employee_name " ); System.out.println( employeeName ); }

24 Closing Database Connection Finally it is necessary to disconnect from the database and release resources being used. If you don’t close the connection then in the production environment your application will fail due to hanging database connections. Here is the code for disconnecting the application from database: con.close();

25 Creating a Database Table import java.sql.*; public class CreateTable{ public static void main(String[] args) { System.out.println("Table Creation Example!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "jdbctutorial"; String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; try{ Class.forName(driverName).newInstance(); con = DriverManager.getConnection(url+dbName, userName, password); try{ Statement st = con.createStatement(); String table = "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))"; st.executeUpdate(table); System.out.println("Table creation process successfully!"); } catch(SQLException s){ System.out.println("Table all ready exists!"); } con.close(); } catch (Exception e){ e.printStackTrace(); } } }

26 Transactions and JDBC JDBC allows SQL statements to be grouped together into a single transaction Transaction control is performed by the Connection object, default mode is auto-commit, I.e., each sql statement is treated as a transaction We can turn off the auto-commit mode with con.setAutoCommit(false); And turn it back on with con.setAutoCommit(true); Once auto-commit is off, no SQL statement will be committed until an explicit is invoked con.commit(); At this point all changes done by the SQL statements will be made permanent in the database.

27 Handling Errors with Exceptions Programs should recover and leave the database in a consistent state. If a statement in the try block throws an exception or warning, it can be caught in one of the corresponding catch statements How might a finally {…} block be helpful here? E.g., you could rollback your transaction in a catch { …} block or close database connection and free database related resources in finally {…} block

28 JDBC references JDBC Data Access API – JDBC Technology Homepage – ml JDBC Database Access – The Java Tutorial – index.html JDBC Documentation – bc/index.html java.sql package – sql/package-summary.html JDBC Technology Guide: Getting Started – bc/getstart/GettingStartedTOC.fm.html JDBC API Tutorial and Reference (book)

29