Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.

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 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 PostgreSQL JDBC interface documentation
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Java Database Connectivity By: Abe Marji CS616. Agenda 1.Quick Review of Databases 2.What is SQL? 3.What is JDBC? 4.Advanced Functions of JDBC 5.Summary.
CSE470 Software Engineering Fall Database Access through Java.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
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.
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.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© 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.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
1 Java DataBase Connectivity JDBC java.sql.*. 2 Java DataBase Connectivity Draft release of JDBC spec (3/96) Java API for executing SQL statements (Since.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
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.
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.
Chapter 8 Databases.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
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.
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.
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.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
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 Session 14.
JDBC (Java Database Connectivity)
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
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.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)
JDBC – Java DataBase Connectivity
DEPTT. OF COMP. SC & APPLICATIONS
Interacting with Database
Lec - 14.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
JDBC – Java Database Connectivity
HW#4 Making Simple BBS Using JDBC
JDBC – Java DataBase Connectivity
JDBC – Java DataBase Connectivity
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
JDBC – Java DataBase Connectivity
Presentation transcript:

Java and database

3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are fields The rows are records A field or set of fields that uniquely identifies a record is known as a primary key

4 eg Products & Customer

JDBC Introduction JDBC : Java database connectivity (JDBC) is the Java Soft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

JDBC Introduction JDBC provides a standard library for accessing relational databases – API standardizes Way to establish connection to database Approach to initiating queries Method to create stored (parameterized) queries The data structure of query result (table) – Determining the number of columns – Looking up metadata, etc. – API does not standardize SQL syntax – JDBC is not embedded SQL – JDBC classes are in the java.sql package

JDBC classes Interface/class/exceptionDescription Interfaces: java.sql.Connection Interface used to establish a connection to a database. SQL statements run within the context of a connection. java.sql.DatabaseMetaData Interface used to return information about the database. java.sql.Driver Interface used to locate the driver for a particular database management system. java.sql.PreparedStatement Interface used to send precompiled SQL statements to the database server and obtain results. java.sql.ResultSet Interface used to process the results returned from executing an SQL statement. java.sql.ResultSetMetaData Interface used to return information about the columns in a ResultSet object. java.sql.Statement Interface used to send static SQL statements to the database server and obtain results. javax.sql.ConnectionEventListener Receives events that a PooledConnection object generates. javax.sql.ConnectionPoolDataSource Factory for PooledConnection objects. A ConnectionPoolDataSource object is usually registered with a JNDI service. javax.sql.DataSource A factory for Connection objects. A DataSource object is usually registered with a JNDI service provider. javax.sql.PooledConnection A PooledConnection object represents a physical connection to a data source. Classes: java.sql.Date Subclass of java.util.Date used for the SQL DATE data type. java.lang.DriverManager Class used to manage a set of JDBC drivers. java.sql.DriverPropertyInfo Class used to discover and supply properties to a connection. java.sql.Time Subclass of java.util.Date used for the SQL TIME data type. java.sql.TimeStamp Subclass of java.util.Date used for the SQL TIMESTAMP data type. java.sql.Types Class used to define constants that are used to identify standard SQL data types, such as CHAR, INTEGER, and DECIMAL. java.sql.String Class used to identify text data types such as CHAR. Exception classes: java.sql.SQLException Exception that provides information about a database error. java.sql.SQLWarning Exception that provides information about a database warning.

JDBC Drivers A JDBC driver provides database connection for a java program. Java uses the connection to interact with database. Before using a driver, the driver must be registered with the JDBC DriverManager.

JDBC Drivers 4 types of JDBC drivers Type 1 : JDBC-ODBC bridge It is used for local connection. ex) 32bit ODBC in windows Type 2 : Native API connection driver It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

JDBC Drivers Type 3 : Network connection driver Type 4 : Database Protocol driver It is independent from h/w because this driver is in Java. ex) thin driver for local/global connection to Oracle

Type 1: JDBC-ODBC Bridge Driver

Type 2 : Native API connection driver

Type 3 : Network connection driver

Type 4 : Database Protocol driver

Let consider the 2 types type 1 and type 4

Seven Basic 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

JDBC: Details of Process 1. Load the driver try { Class.forName("connect.microsoft.MicrosoftDriver"); Class.forName("oracle.jdbc.driver.OracleDriver"); } catch { ClassNotFoundException cnfe) { System.out.println("Error loading driver: " cnfe); }

JDBC: Details of Process 2. Define the Connection URL String host = "dbhost.yourcompany.com"; String dbName = "someName"; int port = 1234; String oracleURL = + host + ":" + port + ":" + dbName; String sybaseURL = "jdbc:sybase:Tds:" + host + ":" + port + ":" + "?SERVICENAME=" + dbName;

JDBC: Details of Process(Continued) 3. Establish the Connection String username = "jay_debesee"; String password = "secret"; Connection connection = DriverManager.getConnection(oracleURL, username, password);

JDBC: Details of Process(Continued) 4. Create a Statement Statement statement = connection.createStatement(); 5. Execute a Query String query = "SELECT col1, col2, col3 FROM sometable"; ResultSet resultSet = statement.executeQuery(query); – To modify the database, use executeUpdate, supplying a string that uses UPDATE, INSERT, or DELETE – Use setQueryTimeout to specify a maximum delay to wait for results

JDBC: Details of Process (Continued) 6. Process the Result while(resultSet.next()) { System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3));} – First column has index 1, not 0 – ResultSet provides various getXxx methods that take a colu index or column name and returns the data – You can also access result meta data (column names, etc.) 7. Close the Connection connection.close(); – Since opening a connection is expensive, postpone this step if additional database operations are expected

HOW TO MAKE CONNECTION IN TYPE 1 AND 4

Type 1: JDBC-ODBC Bridge Drivers JDBC-ODBC driver uses a bridge technology to connect a java client to an ODBC database system. The JDBC-ODBC driver from SUN The driver is platform dependent and other non-java software need to be installed in the machine running the code.

Type 1: JDBC-ODBC Bridge Drivers Use sun.jdbc.odbc.JdbcOdbcDriver as the class name of the JDBC driver. – Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Use "jdbc:odbc:Northwind" as the database address, and use empty strings for the username and password. – Connection connection = DriverManager.getConnection "jdbc:odbc:my_database”,”user name”,”password”);

Type 4 : Database Protocol driver Load the vendor specific driver – Class.forName("oracle.jdbc.driver.OracleDriver"); Make the connection – Connection con = DriverManager.getConnection( username, passwd);

After the connection is established

28 2. Create JDBC statement(s) Statement stmt = con.createStatement() ; Creates a Statement object for sending SQL statements to the database

29 Executing SQL Statements String createLehigh = " Create table Lehigh " + " (SSN Integer not null, Name VARCHAR(32), " + " Marks Integer) " ; stmt.executeUpdate(createLehigh); //What does this statement do? String insertLehigh = " Insert into Lehigh values “ + " ( ,abc,100) " ; stmt.executeUpdate(insertLehigh);

30 Get ResultSet String queryLehigh = " select * from Lehigh " ; ResultSet rs = Stmt.executeQuery(queryLehigh); //What does this statement do? while (rs.next()) { int ssn = rs.getInt( " SSN " ); String name = rs.getString( " NAME " ); int marks = rs.getInt( " MARKS " ); }

31 Close connection stmt.close(); con.close();