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.

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.
JDBC CS-328. JDBC Java API for accessing RDBMS Allows use of SQL for RDBMS programming Can be used for: –embedded SQL –execution of stored queries.
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.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
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.
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
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.
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.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
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 – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
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.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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.
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.
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.
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
Basics of JDBC.
Basics of JDBC Session 14.
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.
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.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 8: Applications Programming for Relational Databases Instructor’s.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
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.
Interacting with Database
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Introduction to Programming with Java
JDBC 15-Nov-18.
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
Bolat Azamat, Kim Dongmin
JDBC Example.
Presentation transcript:

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 JDK 1.1) Primary focus is to link with RDBMS and OORDBMS systems JDBC certification requires compliance with ANSI SQL-2 spec. Initial support for access to ODBC through JDBC-ODBC bridge

3 What does JDBC do? Establish a connection with a database Send SQL statements to the database Process the results.

4 Client Server Models

5 JDBC Architecture Implemented in 2 specific layers Driver layer –Product / Vendor specific driver implementations that convert JDBC syntax. Business Layer –JDBC DriverManager and associated classes that connect Java Applications to appropriate drivers.

6 JavaSoft Framework

7 JDBC Drivers JDBC-ODBC Bridge –A “quick startup” was needed, and there were many ODBC compatible DBMSs in use. Native-API Driver –Relies on client access to database-access API. Only partially implemented in Java, and uses API binary code. JDBC-Net Driver –Uses JDK network protocols to connect to server, which then translated request into DBMS. Native Protocol - All Java Drivers –Uses DBMS network protocol to connect to database.

8 JDBC Interfaces (java.sql.*) java.sql.Driver –acceptsURL, connect, getPropertyInfo java.sql.Connection –createStatement, commit, prepareStatement, rollback java.sql.Statement –execute, executeUpdate, getResultSet, getMoreResults java.sql.PreparedStatement –SetDate, setFloat, executeQuery, executeUpdate

9 JDBC Interfaces (java.sql.*) java.sql.CallableStatement –getByte, getTime, registerOutParameter java.sql.ResultSet –getBytes, getMetaData, next java.sql.ResultSetMetaData –getColumnName, getPrecision, isReadOnly java.sql.DatabaseMetaData –allTablesAreSelectable, getColumns, getDatabaseProductName, supportsMultipleTransactions

10 Company table from zoo.mdb

11 SQL Queries SELECT * FROM zoo.company; –Will select all fields for all records from the “company” table in “zoo” database SELECT * FROM company WHERE state_cd = OR; –Will select all fields for records that have a state code of “OR” SELECT company_name, state_cd, postal_cd FROM company where postal_cd > 60000; –Will select 3 fields for all records that have a zip code larger than 60000

12 SQL Queries SELECT company_name, phone_nbr FROM company where company_name LIKE ‘%Artist%’; –Will select 2 fields for all company names that contain the string “Artist” preceded or followed by 0 or more char. String sql1 = “SELECT company_name, phone_nbr FROM company where company_name LIKE ‘%” + inputVar + “ %’ ”; –Above query changed to allow dynamic definition of comparator. INSERT INTO state_codes (state_cd, state_desc) VALUES ('MO', 'Missouri'); –Will insert into the state_codes table a new record, with two fields - state_cd, and state_desc, with values “MO”, and “Missouri”

13 JDBC Statement Types Statement (createStatement method) –Used for simple SQL statements Connection con = DriverManager.getConnection(newurl); Statement stmt = con.createStatement( ); String stmt = “Select * from retail_item where retail_item_nbr = “ + somejavavariable; ResultSet rs = stmt.executeQuery(stmt);

14 JDBC Statement Types PreparedStatement (prepareStatement method) –Used for SQL statements that require 1 or more input arguments. –Precompiled and stored for future use. PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pstmt.setBigDecimal(1, ); pstmt.setInt(2, );

15 JDBC Statement Types CallableStatement (prepareCall method) –Extension of PreparedStatement that can also execute SQL stored procedures and process results.

16 JDBC Connection process Get Connection implementation from DriverManager Get Statement from connection Execute Statement Process ResultSet returned from query (For other statements check return value for number of rows affected) Repeat for as many Statements as needed. Commit changes (if necessary) Close statement

17 Company table from zoo.mdb

18 JDBC Application import java.lang.*; import java.util.*; import java.sql.*; import sun.jdbc.odbc.*; import java.io.*; public class sample1b { public static void main(String arg[]) { int id, zip, nextRecord; float amount; java.sql.Date dt; String status, name; String result;

19 JDBC Application try { //connect to ODBC database Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Zoo"; // connect Properties p = new Properties(); p.put("user", "student"); p.put("password","sql"); Connection con = DriverManager.getConnection(url,"student","sql"); // create Statement object Statement stmt = con.createStatement(); String sqlselect = "Select company_id, company_name, postal_cd" + " from company"; // run query ResultSet rs = stmt.executeQuery(sqlselect);

20 JDBC Application // process results while(rs.next()) { result = ""; id = rs.getInt(1); zip = rs.getInt(3); name = rs.getString(2); result = result.valueOf(id) + " "; result += result.valueOf(zip) + " "; result += name; System.out.println("Values are: " + result); } System.out.println("\n\n"); //create next (insert)query String sqlSelect2 = "INSERT INTO company (company_id,company_name,address_1,city,state_cd,country_cd, postal_cd,phone_nbr) "+ "VALUES (9,'Bobs ISP', '345 Main St','Grants Pass', 'OR', 'USA',97526, '(541) ')";

21 JDBC Application int ct = stmt.executeUpdate(sqlSelect2); System.out.println("Insert row: " + sqlSelect2.valueOf(ct)); sqlselect = "Select company_id, company_name, postal_cd" + " from company"; // run query rs = stmt.executeQuery(sqlselect); // process results while(rs.next()) { result = ""; id = rs.getInt(1); zip = rs.getInt(3); name = rs.getString(2); result = result.valueOf(id) + " "; result += result.valueOf(zip) + " "; result += name; System.out.println("Values are: " + result); }

22 JDBC Application //create last (delete) query System.out.println("\n\n"); sqlSelect2 = "DELETE FROM company WHERE company_name = 'Bobs ISP' "; ct = stmt.executeUpdate(sqlSelect2); System.out.println("Deleted row: " + sqlSelect2.valueOf(ct)); sqlselect = "Select company_id, company_name, postal_cd" + " from company"; // run query rs = stmt.executeQuery(sqlselect); // process results while(rs.next()) { result = ""; id = rs.getInt(1); zip = rs.getInt(3); name = rs.getString(2); result = result.valueOf(id) + " "; result += result.valueOf(zip) + " "; result += name; System.out.println("Values are: " + result); }

23 JDBC Application //save your actions con.commit(); //close connection con.close(); } catch(Exception e) { System.out.println(e.getMessage()); }

24 Sample JDBC Results D:\data\cs423_fs00\java\jdbc examples>java sample1b Values are: Some Shirt Company Values are: Good Candy Company Values are: Penns Pens Values are: Close To You Magnets Values are: Cute Critters Values are: Seattle Signs Values are: The Artist Group Values are: Yummys Values are: Bobs PCs Insert row: 1 Values are: Some Shirt Company Values are: Good Candy Company Values are: Penns Pens Values are: Close To You Magnets Values are: Cute Critters Values are: Seattle Signs Values are: The Artist Group Values are: Yummys Values are: Bobs PCs Values are: Bobs ISP

25 Sample JDBC Results Deleted row: 1 Values are: Some Shirt Company Values are: Good Candy Company Values are: Penns Pens Values are: Close To You Magnets Values are: Cute Critters Values are: Seattle Signs Values are: The Artist Group Values are: Yummys Values are: Bobs PCs D:\data\cs423_fs00\java\jdbc examples>

26 Setting up ODBC in Windows (NT, 2000, XP, Vista, 7(?) ) 1.Verify that ODBC Data Sources (ODBC) is loaded in Control Panel / Administrative tools

27 Setting up ODBC in Windows 2.Open Data Source Administrator and add source

28 Setting up ODBC in Windows 3.After selecting database type, add Source name, and select specific database image.

29 Setting up ODBC in Windows 4.Add login Name and password

30 Transaction Support Consists of one or more statements that have been executed, completed, and then either committed or rolled back Default mode for statements is auto-commit –Each statement becomes a “transaction” Can be disabled to support controlled commit or rollback (for multiple statement transactions). –con.setAutoCommit(false); –con.commit(); –con.setSavepoint(); –con.rollback(); Transaction support defined in DatabaseMetaData

31 Summary JDBC Allows Java programmers to build customized interfaces to standard database management systems through a number of approaches. JDBC uses a standardized instruction syntax (SQL) to send queries to databases The JDBC-ODBC bridge allows Java to use an existing database connectivity method to gain easy access to existing databases.