Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.

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)
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.
Distributed Application Development B. Ramamurthy.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
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.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
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.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
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.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
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.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
JDBC Establish a connection with a database or access any tabular data source Send SQL statements Process the results Two major sets of interfaces: JDBC.
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.
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
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.
Web Programming Assistant Professor Xiaozhong Liu
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
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.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
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 Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
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
Java Access to RDB Relational databases (RDBs) dominate today, due to:
Interacting with Database
Lec - 14.
JDBC Database Management Database connectivity
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Introduction to Server-Side Web Development using JSP and Databases
Interacting with Database
Java Database Connectivity
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1

Umair Javed©2005 JDBC2 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

Umair Javed©2005 JDBC3 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.

Umair Javed©2005 JDBC4 JDBC Introduction –API does not standardize SQL syntax JDBC is not embedded SQL –JDBC classes located in java.sql package

Umair Javed©2005 JDBC5 Connecting Microsoft Access Example Create PersonInfo database Create Person table

Umair Javed©2005 JDBC6 Connecting MS Access Example: Setup System DSN settings  ControlPanel  AdministrativeTools  data sources(ODBC)

Umair Javed©2005 JDBC7 Basic Steps in Using JDBC 1.Import required packages 2.Load driver 3.Define Connection URL 4.Establish Connection 5.Create a Statement object

Umair Javed©2005 JDBC8 Basic Steps in Using JDBC (cont.) 6.Execute query / DML 7.Process results 8.Close connection

Umair Javed©2005 JDBC9 JDBC: Details of Process 1.Import package  Import java.sql package  import java.sql.*;

Umair Javed©2005 JDBC10 JDBC: Details of Process 2.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 ");

Umair Javed©2005 JDBC11 JDBC: Details of Process 3.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”

Umair Javed©2005 JDBC12 JDBC: Details of Process, cont. 4.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 = “sa"; String pswd = sa; Connection con = null; con = DriverManager.getConnection (conURL,usr,pswd);

Umair Javed©2005 JDBC13 JDBC: Details of Process, cont. 5.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

Umair Javed©2005 JDBC14 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);

Umair Javed©2005 JDBC15 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);

Umair Javed©2005 JDBC16 JDBC: Details of Process, cont. 7. 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); }

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

Umair Javed©2005 JDBC18 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” );

Umair Javed©2005 JDBC19 JDBC Architecture Driver Manager Connection creates Statement ResultSet creates Driver Database SQL Data Establish Link To DB

Umair Javed©2005 JDBC20 Retrieving Data from ResultSet //Step 1: import package import java.sql.*; public class TestDbConnection { public static void main(String ar[]){ String db_connect_string, db_user_id, db_password, strQuery; Statement st; ResultSet rs; Connection conn; db_connect_string = "jdbc:odbc:MyConn"; db_user_id = "sa"; db_password = "sa"; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection(db_connect_string, db_user_id, db_password);

Umair Javed©2005 JDBC21 Retrieving Data from ResultSet st = conn.createStatement(); System.out.println("Connected"); strQuery = "Select * From Employee"; rs = st.executeQuery(strQuery); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString(2); int sal = rs.getInt("salary"); System.out.println("Id:" + id + ", Name: " + name + " and Salary: " + sal); System.out.println(""); } }

Umair Javed©2005 JDBC22 Retrieving Data from ResultSet catch(Exception e){ System.out.println(e); } } /** Creates a new instance of TestDbConnection */ public TestDbConnection() { } }

Umair Javed©2005 JDBC23 Retrieving Data from ResultSet Using Servlet import java.io.*; import java.net.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class DBServlet extends HttpServlet { String db_connect_string, db_user_id, db_password, strQuery; Statement st; ResultSet rs; Connection conn; int i;

Umair Javed©2005 JDBC24 Retrieving Data from ResultSet Using Servlet response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println(" Servlet DBServlet "); out.println(" "); out.println(" Servlet DBServlet at " + request.getContextPath () + " "); db_connect_string = new String("jdbc:odbc:MyConn"); db_user_id = new String("sa"); db_password = new String("sa");

Umair Javed©2005 JDBC25 Retrieving Data from ResultSet Using Servlet try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection(db_connect_string, db_user_id, db_password); System.out.println("Connected"); int d = 1; String n = new String("Farukh"); String sql = "Select * From Employee Where name = ? and id = ?"; PreparedStatement pStmt = conn.prepareStatement(sql); pStmt.setString(1, n); pStmt.setInt(2, d);

Umair Javed©2005 JDBC26 Retrieving Data from ResultSet Using Servlet rs = pStmt.executeQuery(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString(2); int sal = rs.getInt("salary"); out.println(id + " " + name + " " + sal); out.println(" "); } /* strQuery = "Update Employee Set name='Farukh', Salary = Where id = 2"; i = st.executeUpdate(strQuery); out.println(i + " rows updated "); */

Umair Javed©2005 JDBC27 Retrieving Data from ResultSet Using Servlet } catch(Exception e){ System.out.println(e); } out.println(" "); out.close(); }

Umair Javed©2005 JDBC28 Retrieving Data Through Procedure try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection(db_connect_string, db_user_id, db_password); System.out.println("Connected"); strQuery = "{ call Add_Emp(?, ?, ?) }"; CallableStatement cs = conn.prepareCall(strQuery); cs.setInt(1, 12); cs.setString(2, "Zahid"); cs.setInt(3, 27500); cs.execute();

Umair Javed©2005 JDBC29 Retrieving Data Through Procedure //strQuery = "{ call Emp_Search_ById(?) }"; //cs.setInt(1, 2); //rs = cs.executeQuery(); /* while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString(2); int sal = rs.getInt("salary"); System.out.println("Id:" + id + ", Name: " + name + " and Salary: " + sal); System.out.println(""); }*/ } catch(Exception e){ System.out.println(e); } }

Umair Javed©2005 JDBC30 Procedure Creation CREATE PROCEDURE int AS Select * from Employee where id