1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.

Slides:



Advertisements
Similar presentations
Introduction to JDBC Standard framework for dealing with tabular and generally, relational data SQL (Structured Query Language) is standardized language.
Advertisements

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.
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.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Introduction to JDBC (Java Database Connectivity).
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.
JDBC Vs. Java Blend Presentation by Gopal Manchikanti Shivakumar Balasubramanyam.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
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.
Think Possibility Integrating Web Applications With Databases.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
© 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 ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CS 405G: Introduction to Database Systems Database programming.
Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc.
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 Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
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.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
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.
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.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
JDBC By 朱志興. Four driver types of JDBC (1) JDBC-ODBC bridge plus ODBC driver: Java Database Client JDBC- ODBC bridge ODBC driver Database Server A B C.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
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.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
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.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
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.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
DEPTT. OF COMP. SC & APPLICATIONS
Lec - 14.
JDBC Database Management Database connectivity
Database JDBC Overview CS Programming Languages for Web Applications
Objectives In this lesson, you will learn about:
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Interacting with Database
JDBC – ODBC DRIVERS.
Lecture 11 Database Connection
Presentation transcript:

1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a Database (Java’s Statement class)  Obtaining Query Results (Java’s ResultSet class)   Preview: Introduction to Data Communications

2 Lecture 29 Objectives of the Lecture  To have an acquintance of JDBC drivers  To know the basic issues required to connect to a database from a Java application  To be able to write simple queries and receive their results using methods in Java programs

3 Lecture 29 JDBC Basics  The JDBC (short for Java Database Connectivity) interface is a pure Java API used to execute SQL statements.  The JDBC provides a set of classes and interfaces that can be used by developers to write database applications.  Basic JDBC interaction, in its simplest form, can be broken down into four steps:  1. Open a connection to the database  2. Execute a SQL statement  3. Process the SQL statement’s results  4. Close the connection to the database.

4 Lecture 29 JDBC Drivers  As mentioned in Lecture 28, a Java application can communicate to a DBMS using specialized programs called Drivers.  The JDBC supports two database access models: two-tier and three- tier.  Using a two-tier model, a Java application talks directly to the database. This is accomplished through the use of a JDBC driver, which sends commands directly to the database.  With a two-tier database access model, the JDBC driver and the database communicate through a middle-tier system.  Sun Microsystems has defined four JDBC driver types:  JDBC-ODBC bridge plus ODBC Driver  Native-API Partly-Java Driver  JDBC-Net Pure Java Driver  Native-Protocol Pure Java Driver

5 Lecture 29 JDBC Drivers (cont.)

6 Lecture 29 Connecting to DBMS from Java  As mentioned earlier writing a Java application to communicate with a database involves: 1.Establishing a connection with a data source 2.Sending queries and update statements to the data source 3.Processing the results 4.Closing the database  Establishing a Connection: Establishing a connection involves loading the driver and then making the connection. Loading the driver or drivers you want to use is very simple and involves just one line of code as follows: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Note: The method forName() generates ClassNotFoundException if JDBC is not installed. This is a checked exception, so we must handle it before the above statement can compile.

7 Lecture 29 Establishing a Connection  Making the Connection involves creating a Connection object that links the driver to the DBMS. Connection con = DriverManager.getConnection(url, "myLogin", "myPassword"); or Connection con = DriverManager.getConnection(url); where url = “jdbc:odbc:DataSetName”  Since we are using the JDBC-ODBC Bridge driver, the JDBC URL will start with jdbc:odbc:. The rest of the URL is generally the data source name defined in the configuration of ODBC  The second version assumes you do not need to login to the computer hosting the database file. Connection con = DriverManager.getConnection(url);

8 Lecture 29 Creating JDBC Statements and Sending SQL statements to the data source  The next step after establishing a connection is to create a Statement object using the Connection object as follows: Statement statement = con.createStatement();  The Statement object created above has methods which are used to send SQL statement to the DBMS. Which method is used, depends on the SQL statement being sent. For a SELECT statement, the method to use is executeQuery(). Whereas, for statements that create or modify tables, the method to use is executeUpdate(). String query = "select * from table1"; ResultSet result = statement.executeQuery(query);  Notice that executeQuery() method returns an object of ResultSet which contains the result of the query.

9 Lecture 29 Creating JDBC Statements and Sending SQL statements to the data source  The executeUpdate() is similar, except that we are not expecting result from the data source. String query2 = "INSERT INTO table1 VALUES (‘a', ‘b', ‘c', 1)"; statement.executeUpdate(query2);

10 Lecture 29 Processing Results obtained from data source  The variable result, which is an instance of ResultSet, contains all the rows of the table1 parameter. In order to process each row, we need to go to the row and retrieve its values.  The method next() moves what is called a cursor to the next row and makes that row (called the current row) the one upon which we can operate.  The cursor is initially positioned just above the first row of a ResultSet object, the first call to the method next() moves the cursor to the first row and makes it the current row.  Successive invocations of the method next() move the cursor down one row at a time from top to bottom.

11 Lecture 29 Processing Results obtained from data source  The ResultSet object has a get method for each type ( getString, getInt, getDouble, getLong, getFloat, etc) which can be used to retrieve the value of a field depending on its type.  For example, the following loop is use to retrieve and print the bookNumber, author and title fields of a books table. while(result.next()) { System.out.println(result.getString(“ColumnName1")+ "\t"+ result.getString(" ColumnName2")+ “\t"+ result.getString(" ColumnName3")); }  Note: Most of the methods of the ResultSet object (e.g next()) generate SQLException which is a checked exception, so again we must handle this exception before our program can compile.