Lec - 14.

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

Database programming in Java An introduction to Java Database Connectivity (JDBC)
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
CSE470 Software Engineering Fall Database Access through Java.
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.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
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.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
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. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
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.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
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.
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
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.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
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.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
JDBC – Java DataBase Connectivity
Introduction to SQL Programming Techniques
Course Outcomes of Advanced Java Programming AJP (17625, C603)
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
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
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
JDBC – Java DataBase Connectivity
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Lec - 14

Java Database Connectivity (JDBC)

Agenda Overview of JDBC technology JDBC drivers Seven basic steps in using JDBC Example of Retrieving data from ResultSet Example of Executing DML statements Using prepared Statement JDBC

JDBC Introduction JDBC provides a standard library for accessing relational databases API standardizes Way to establish connection to database Approach to initiate queries Method to create stored (parameterized) queries The data structure of query result (table) Determining the number of columns Looking up metadata, etc. JDBC

JDBC Introduction API does not standardize SQL syntax JDBC is not embedded SQL JDBC classes located in java.sql package JDBC

Connecting Microsoft Access Example Create PersonInfo database Create Person table JDBC

Connecting MS Access Example: Setup System DSN settingsControlPanelAdministrativeTools data sources(ODBC) JDBC

Basic Steps in Using JDBC

Basic Steps in Using JDBC Import required packages Load driver Define Connection URL Establish Connection Create a Statement object JDBC

Basic Steps in Using JDBC (cont.) Execute query / DML Process results Close connection JDBC

JDBC Details of Process

JDBC: Details of Process Import package Import java.sql package import java.sql.*; JDBC

JDBC: Details of Process Loading driver Need to load suitable driver for underlying database Different drivers for different databases are available For MS Access Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver"); For Oracle Class.forName(“oracle.jdbc.driver.OracleDriver "); JDBC

JDBC: Details of Process Define Connection URL To get a connection, we need to specify URL of database. If you are using a JDBC-ODBC driver you need to create a DSN. DSN is the name of your DataSource If the name of your DSN is “personDSN” then the url of the database will be String conURL = “jdbc:odbc:personDSN” JDBC

JDBC: Details of Process, cont. Establish Connection Connection con = null; Use driver manager to get the connection object con = DriverManager.getConnection(conURL); If the Db requires username and password you can use overloaded version String usr = “umair"; String pswd = “vu"; con = DriverManager.getConnection(conURL,usr,pswd); JDBC

JDBC: Details of Process, cont. Create Statement A statement is obtained from a Connection object. Statement statement = con.createStatement(); Once you have a statement, you can use it for various kind of SQL queries JDBC

JDBC: Details of Process, cont. 6(a) Execute Query / DML executeQuery(sql) method Used for SQL SELECT queries Returns the ResultSet object which is used to access the rows of the query results String sql = "SELECT * FROM sometable"; ResultSet rs = statement.executeQuery(sql); JDBC

JDBC: Details of Process, cont. 6(b) Execute Query / DML executeUpdate(sql) method Used for an update statement ( INSERT, UPDATE or DELETE) Returns an integer value representing the number of rows updated. String sql = “INSERT INTO tableName “ + “(columnNames) Values (values)”; int count = statement.executeUpdate(sql); JDBC

JDBC: Details of Process, cont. Process Results ResultSet provides various getXxx methods that take a column index or name and returns the data First column has index 1, not 0 while(resultSet.next()) { //by using column name String name = rs.getString(“columnName”); //or by using index String name = rs.getString(1); } JDBC

JDBC: Details of Process, cont. Close Connection connection.close(); As opening a connection is expensive, postpone this step if additional database operations are expected JDBC

In a nut shell Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = null; con = DriverManager.getConnection(url, usr, pwd); Statement st = con.createStatement(); ResultSet rs = st.exectuteQuery(“Select * from Person” ); JDBC

JDBC Architecture 1 3 6 4 5 2 Driver Database Driver Manager creates Connection ResultSet creates Statement creates 4 5 2 SQL Data Establish Link To DB Driver Database JDBC

Retrieving Data from ResultSet Example Code Retrieving Data from ResultSet

Example Code 14.1 Retrieving Data from ResultSet //Step 1: import package import java.sql.*; public class JdbcEx { public static void main (String args [ ]){ try { //Step 2: Load driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Step 3: Defie the connection URL String url = "jdbc:odbc:personDSN"; //Step 4: Establish the connection Connection con = null; con = DriverManager.getConnection (url , "“ ,""); JDBC

Example Code 14.1 Retrieving Data from ResultSet (cont.) //Step 5: create the statement Statement st = con.createStatement(); //Step 6: Execute the query String sql = "SELECT * FROM Person"; ResultSet rs = st.executeQuery(sql); //Step 7: Process the results while ( rs.next() ) { String name = rs.getString("name"); String add = rs.getString("address"); String pNum = rs.getString("phoneNum"); System.out.println(name + " " +add +" "+pNum); } // end while JDBC

Example Code 14.1 Retrieving Data from ResultSet (cont.) //Step 8: close the connection con.close(); }catch (Exception sqlEx) { System.out.println(sqlEx); } } //end main }//end class JDBC

Compile & Execute JDBC