MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, 2001-2008. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
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.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
Objectives Describe JDBC Describe JDBC Driver Types
Advanced OOP MCS-3 OOP BSCS-3 Lecture # 11. DATABASE CONNECTIVITY IN JAVA JDBC Programming JDBC (Java Database Connectivity) is the specification of a.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Dale Roberts 8/24/ Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
Think Possibility Integrating Web Applications With Databases.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
© 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.
Database Programming in Java Corresponds with Chapter 32, 33.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
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.
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 (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
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.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
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.
225 City Avenue, Suite 106 Bala Cynwyd, PA , phone , fax Connecting to a Database.
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.
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 Session 14.
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 8: Applications Programming for Relational Databases Instructor’s.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC.
DEPTT. OF COMP. SC & APPLICATIONS
Interacting with Database
Lec - 14.
JDBC 15-Apr-18.
ODBC, OCCI and JDBC overview
JDBC Database Management Database connectivity
JDBC 21-Aug-18.
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Interacting with Database
Java Database Connectivity
JAVA DATABaSE CONNECTIVITY
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, All Rights Reserved.

Goals for Today To understand the programs we wrote in the JDBC lab To understand the basic JDBC API

Advantages of JDBC JDBC = Java Database Connectivity (popular belief though not true) The API (Application Programming Interface) is meant to allow java programs to connect to a database, to retrieve, insert, update or delete data in the database, and to process the data in the java program. Some advantages of this: The java program will be largely unchanged across different platforms & databases. The only changes will be in the SQL part, in case we move across databases, that have slightly different SQL syntax. The bulk of the java program (the processing part) will be unchanged. This means potentially large increases in application programmer productivity and also in maintenance costs of the application.

JDBC Drivers JDBC connections to a database are done using a JDBC driver. Almost all vendors now offer JDBC drivers. We are using a jdbc-odbc bridge driver to talk to a MS Access Database or to a MYSL database. In case we want to run our programs on another database, like Oracle, we need to install the JDBC driver for that DBMS on our machine. has a searchable database of available drivers for different DBMSs. In addition, we can also check the sites for the DBMS vendors, like

Loading the Driver The JDBC API is in the java.sql package. We imported that in all the code that we wrote in the labs. The driver is loaded into a program with the following code: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); If we are using another driver, then it’s documentation will tell us its class name. E.g., A driver whose class name is jdbc.DriverXYZ will be loaded with: Class.forName(“jdbc.DriverXYZ”); Here, the java.lang.ClassLoader object locates and loads the driver class from the CLASSPATH.

Connecting to the Database java.sql.DriverManager is a static class that provides methods to obtain connections. The advantage is that this class provides a uniform method of accessing all the drivers, so we write the same code for all drivers, except that the driver class name is different for different drivers. The DriverManager.getConnection(url,user,password) method provides an easy way to access all databases. URL: Basic syntax: protocol: : Protocol is always jdbc. Subprotocol is supplied by the maker of that particular JDBC driver. In our case, it’s odbc. Subname is the way to identify the database. In our case,it’s simply the name of the database: MyGarage. If the database is on another machine, then the subname is where the address will be, e.g., //hostname:port/subsubname

Connection Interface The Connection interface offers 3 methods for sending SQL statements to the underlying database: createStatement(): For a simple, parameterless SQL statement prepareStatement(): For a prepared statement (with parameters) prepareCall(): For a stored procedure. We won’t be doing this method, because stored procedures are inherently DB specific and hence against the principle of portability of an application. In CreateGarage.java & DropGarage.java, we used the createStatement() method. This method gives a Statement Object. In InsertGarage.java, we used the prepareStatement() method. This method gives a PreparedStatement object.

Statement Interface A Statement object is created using the Connection method createStatement(). Statement objects are used to send SQL statements without parameters. The method we used in this interface is: int executeUpdate(String sql): Performs an Update, Insert or Delete on the database, and is also used for DDL statements (like create table). It returns the number of rows affected by the statement.

PreparedStatement Interface This is an extension of the Statement interface. Used for parametrized SQL queries. we wrote these in InsertGarage.java. The new methods here include setInt(int parameterIndex, int x): sets the given parameter to x. Similarly, we have setDouble, setString, SetDate, etc.

SQLException Class Is derived from java.lang.Exception. It contains a description of the error, and a chain to the next SQL Exception object, in case there was more than one exception. In our labs, we printed out only the current SQL exception. Below is useful code that prints out the chain of exceptions: catch(SQLException ex) { System.err.println(“\n---SQL EXCEPTION CAUGHT---\n”); while(ex!=null){ System.err.println("SQLException: "+ex.getMessage()); ex.printStackTrace(System.err); ex=ex.getNextException(); System.err.println( “ ” ); }//while }//catch

Conclusion We made sense of the lab we have done, and also got an overview of the portion of the JDBC API,that is used most often. For further reading, we can look at: 1. JDBC: Database Access with Java, by Hamilton, Cattell & Fisher Addison Wesley has a lot of useful information on JDBC, including features on new versions of the API.