Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.

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

Distributed Application Development B. Ramamurthy.
V OOP: JDBC1 V Week 8 v Objective –to give some background on JDBC to help with the lab exercises Fall, 2002 Introduction to Java Database.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
Adv. CoE Lab: JDBC 1 Advanced Computer Engineering Lab Objective – –to give some background on JDBC to help with the lab exercises , Semester.
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
Overview 1. What is JDBC? 2. The JDBC-ODBC Bridge 3. JDBC Pseudocode 4. simpJDBC.java.
Lab: JDBC 1 Computer Engineering Lab III v Objective –to give some background on JDBC to help with the lab exercises , Semester 1,
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.
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.
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.
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.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
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,
Computer Engineering Lab
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. 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.
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.
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.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
Database Access Using JDBC BCIS 3680 Enterprise Programming.
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)
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
DEPTT. OF COMP. SC & APPLICATIONS
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.
ODBC, OCCI and JDBC overview
JDBC Database Management Database connectivity
Advanced Web Automation Using Selenium
JDBC.
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.
Java Database Connectivity
JAVA DATABaSE CONNECTIVITY
Presentation transcript:

Accessing Database using JDBC

JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using SQL constructs Contents What is a Relational Database? What is JDBC? JDBC Drivers Connecting to the Database Executing Common SQL Commands Closing the Database Connection Meta Data

What is a Relational Database? A relational database is a powerful data storage and retrieval system Databases can be described as a collection of tables Tables are made up of rows and columns Structured Query Language (SQL) allows users to manipulate data in relational databases

SQL Fundamentals SQL (Structured Query Language) is a powerful set-oriented language that can interact with any relational database for the manipulation, definition and control of data. All the major database vendors support SQL Example SQL queries: SELECT * FROM BooksTable SELECT Title, [Author Name], Description FROM BooksTable WHERE Title='Look for me‘ INSERT INTO BooksTable VALUES ('Book Name', 'P.G. Author', 'Brief Description')

What is JDBC? Provides universal access to many relational databases An interface which allows Java code to execute SQL statements inside relational databases Sybase Oracle DB2 JDBC Code JAVA Application

What is JDBC? (cont.) The JDBC API provides applications with a universal database language With the JDBC API, programmers can construct SQL statements and embed them inside Java code The query results are stored in Java variables and errors are stored in Java exceptions The JDBC API is in the java.sql package

ODBC Open Database Connectivity is an application programming interface (API) for database access ODBC is not a platform independent API The main proponent and supplier of ODBC programming support is Microsoft By using ODBC statements in a program, you can access data in a number of different databases including Access, dBase, Excel, and Text In addition to the ODBC software, a separate driver is needed for each database to be accessed

The JDBC-ODBC Bridge Allows Java code to use the C/C++ interface of ODBC it means that JDBC can access many different database products JDBC Data processing utilities Java Code jdbc-odbc bridge odbc driver

The JDBC-ODBC Bridge The JDBC-ODBC bridge comes free with the JDK: called sun.jdbc.odbc.JdbcOdbcDriver The ODBC driver for Microsoft Access comes with MS Office so it is easy to connect Java and Access

JDBC Drivers The JDBC driver essentially hides the vendor specific code from the application developer The JDBC driver is the mediator between the Java application and the database

Types of JDBC Drivers There are 4 types of JDBC drivers each using different protocols to access the database 1.JDBC-ODBC Bridge Provides a gateway to the ODBC API. ODBC actually accesses the database This driver generally requires software to be installed on the client systems 2.Native API translate Java to the database’s own API

Types of JDBC Drivers 3. Net Protocol All-Java Translates JDBC calls into a database- independent network protocol which is then translated to a database protocol by a server. An extremely flexible 100% Pure Java driver. It does not require code to be installed on the client and will provide access to multiple databases 4.Native Protocol All-Java Translates JDBC calls into a database dependent network protocol used by databases directly Talks directly to the database using Java sockets This is a 100% Pure Java solution that generally comes from the database vendor

JDBC Drivers A searchable list of drivers (freeware, shareware, and commercial) can be found at:

JDBC Pseudo Code 1.load the JDBC driver 2.Specify the name and location of the database being used 3.Connect to the database with a Connection object 4.Execute a SQL query using a Statement object 5.Get the results in a ResultSet object 6.Finish by closing the ResultSet, Statement and Connection objects

Pseudocode as a Diagram DriveManagerConnectionStatementResultSet creates Driver SQL data make link to driver

Connecting to the Database Establishing a database connection Load the driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Open a database connection Connection conn = DriveManager.getConnection(url,”myLogin”, “myPasswd”); Connection Object Database opened, return the connection

Name the Database The name and location of the database is given as a URL the details of the URL vary depending on the type of database that is being used

JDBC Database URL jdbc:odbc: //host.domain.com: 2048 /data/file The protocolThe machine holding the database The port used for the connection The path to the database on the machine e.g. jdbc:odbc:Books

JDBC Statement Creating a JDBC connection Creating a JDBC statement A Statement object is used to send the SQL statement to the database The Statement object must be created by using the open Connection object Connection con = DriverManager.getConnection("jdbc:odbc:bookdb","",""); Connection con = DriverManager.getConnection("jdbc:odbc:bookdb","",""); Statement st = conn.createStatement(): ResultSet rs = st.executeQuery(“select * from Authors”); st.close();

JDBC Statements (cont.) There are 2 important Statement methods executeQuery(String SQLstatement) Executes a SQL SELECT statement. Returns a ResultSet object executeUpdate(String SQLstatement) Executes a SQL INSERT, UPDATE or DELETE statement Before reusing a Statement object, call the close() method

ResultSet Object The ResultSet object contains the rows retrieved from the database query To access each row use ResultSets’s next() method This method moves a database cursor sequentially through the rows retrieved from the query while(rs.next()) { name = rs.getString(“id"); age = rs.getInt(“name"); System.out.println(id + " " + name); } while(rs.next()) { name = rs.getString(“id"); age = rs.getInt(“name"); System.out.println(id + " " + name); }

ResultSet Object Cursor operations: first() last() next() previous(), etc. Typical code: while (rs.next()){ //process the row; } John Mark Paul Peter cursor

SimpleJDBC.java // SimpleJDBC.java // Displays the firstnames and lastnames // of the Authors table in the Books db import java.sql.*; public class SimpleJDBC { public static void main(String[] args) { // The URL for the Books database. // ’Protected' by a login and password. String url = "jdbc:odbc:Books"; String username = "anonymous"; String password = "guest";

try { // load the JDBC-ODBC Bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // connect to db using DriverManager Connection conn = DriverManager.getConnection(url, username, password); // Create a statement object Statement statement = conn.createStatement(); // Execute the SQL query ResultSet rs = statement.executeQuery( "SELECT lastName, firstName FROM Authors" );

//Print the result set while(rs.next()) { System.out.println(rs.getString("lastName") + ", " + rs.getString("firstName")); } // Close down statement.close(); conn.close(); } catch (ClassNotFoundException cnfex ) { System.err.println( "Failed to load JDBC/ODBC driver." ); cnfex.printStackTrace(); System.exit( 1 ); // terminate program } catch (SQLException sqlex ) { System.err.println( sqlex ); sqlex.printStackTrace(); } } // end of main() } // end of SimpleJDBC class

Books.mdb as an ODBC Data Source 1. Click on “32 bit ODBC” in the Control Panel. This displays the ODBC Data Sources Administrator

2. Press Add to add a data source and select Microsoft Access Driver (*.mdb). Press “Finish”.

3. Type in a source name, description, and press Select to browse to set the path to the Books.mdb file. Now click on Advanced

4. Type in a username and password (guest) Click Ok

Table in Books.mdb Publishers PublisherID PublisherName Titles ISBN Title EditionNumber YearPublished Description PublisherID AuthorISBN ISBN AuthorID Authors AuthorID FirstName LastName YearBorn