JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.

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

Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
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.
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.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
JDBC CS 122. JDBC zJava Database Connectivity zDatabase Access Interface Õprovides access to a relational database (by allowing SQL statements to be sent.
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.
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.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
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.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
© 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.
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.
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. 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.
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 (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.
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.
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.
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
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
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.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Interacting with Database
Lec - 14.
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC 21-Aug-18.
Introduction to Programming with Java
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
JDBC – ODBC DRIVERS.
Java Database Connectivity
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

JDBC CS 124

JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent and executed through a Java program) JDBC package: set of Java classes that facilitate this access (java.sql.*) Comes with JDK (since 1.1)

JDBC Driver Need a driver, specific to the DB product, to mediate between JDBC and the database the driver is a Java class that needs to be loaded first Relational DBMS Java Program - load driver - establish connection - send SQL statements

JDBC-ODBC Bridge Driver that interfaces with ODBC (Object Database Connectivity--also an access interface) Easiest way to access databases created by Microsoft products register database as an ODBC data source use JDBC-ODBC bridge as the JDBC driver (included in JDK distribution)

Key Classes in JDBC Connection need to create an instance of this class when establishing a connection to the database Statement for issuing SQL statements ResultSet (interface) a ResultSet object represents the table returned by an SQL select statement

Establishing a Connection Use the getConnection() method under the DriverManager class String argument: "jdbc:driver:name” returns a Connection object Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); // above line loads the jdbc-odbc driver String dbname = “jdbc:odbc:MyDB”; Connection c = DriverManager.getConnection(dbname);

Creating a Statement Object Execute the createStatement() method on the Connection object returns a Statement object afterwards, run methods on the Statement object to execute an SQL statement Statement s = c.createStatement();

Methods of the Statement Class Methods of the Statement class require a string parameter containing the SQL statement executeQuery() requires a String argument (a SELECT statement) returns a ResultSet object representing the table returned executeUpdate() requires a String argument (an INSERT, UPDATE, or DELETE statement) returns an int (row count, in most cases)

The ResultSet Interface A ResultSet object represents the table returned by the select statement sent Navigation/retrieval methods next(): moves to the next row (first row if called for the first time), returns false if no rows remain getXXX() methods return the value of a field for the current row

ResulSet example ResultSet rs; rs = s.executeQuery(“SELECT * FROM [ORDER]”); rs.next(); // gets the first row (use in a loop for multiple rows) // suppose the ORDER table has an integer field // called quantity int myvar = rs.getInt(“quantity”); // if you knew that quantity is the 2nd field in the table myvar = rs.getInt(2); Need braces because ORDER is a reserved word in SQL

Exercise Create a Microsoft Access table insert sample rows Add an ODBC data source use the Microsoft Access driver associate with the created database Create a Java program use JDBC-ODBC bridge create a loop that lists all rows of the table

executeQuery( ) example … Statement s = con.createStatement(); ResultSet rs = s.executeQuery( “SELECT * FROM STUDENT WHERE QPI > 3.0” ); while ( rs.next() ) { String name = rs.getString(“LastName”); int y = rs.getInt(“Year”); double qpi = rs.getDouble(“QPI”); System.out.println( name + “ ” + y + “ ” + qpi); }

executeUpdate( ) example … Statement s = con.createStatement(); int result; result = s.executeUpdate( “DELETE FROM EMPLOYEE WHERE DeptCode=‘CS’” ); System.out.println( result + “ rows deleted.” );

The PreparedStatement class PreparedStatement: a Statement that specifies parameters through Java code The SQL statements take different forms when you specify different parameter values Useful when query is performed repeatedly Formatting of literal values is easier

Version 1 (Statement) // suppose lastName is a String variable Statement s = con.createStatement(); ResultSet rs = s.executeQuery( “SELECT * FROM STUDENT WHERE LastName = ‘” + lastName +”’” ); while ( rs.next() ) { String name = rs.getString(“LastName”) + rs.getString(“FirstName”); int y = rs.getInt(“Year”); double qpi = rs.getDouble(“QPI”); System.out.println( name + “ ” + y + “ ” + qpi); } Query string is built manually

Version 2 (PreparedStatement) // suppose lastName is a String variable PreparedStatement s = con.prepareStatement( “SELECT * FROM STUDENT WHERE LastName = ?” ); s.setString( 1, lastName ); ResultSet rs = s.executeQuery(); while ( rs.next() ) { String name = rs.getString(“LastName”) + rs.getString(“FirstName”); int y = rs.getInt(“Year”); double qpi = rs.getDouble(“QPI”); System.out.println( name + “ ” + y + “ ” + qpi); } the appropriate literal is “inserted” in the query

Summary JDBC allows you to write Java programs that manipulate a database A driver (often a separate product) is required that facilitates access Key classes: Connection, Statement, PreparedStatement, and ResultSet Other features: metadata and stored-proc invocation