Java Access to RDB Relational databases (RDBs) dominate today, due to:

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

19 augustus 2003augustus 2003 JSP-2. BICT 2JDBC BICT 3Install MySQL Download MySQL daemon – Free – Windows version… Start Daemon – Mysqld-nt.exe Download.
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
1 CSE5200 JDBC and JDeveloper JDBC java.sql package java.sql classes.
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.
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.
Object-Oriented Enterprise Application Development Introduction to JDBC.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Database Connectivity ODBC, JDBC and SQLJ CS2312.
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.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
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.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
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.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
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.
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.
EXAMPLE I An application showing JDBC access to Cloudscape.
JDBC™ Fundamentals (a.k.a. Java Database Connectivity, although technically not an acronym) ©SoftMoore ConsultingSlide 1.
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 - 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.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
DEPTT. OF COMP. SC & APPLICATIONS
CS3220 Web and Internet Programming Database Access with JDBC
Interacting with Database
1st Semester, 2017 Sanghyun Park
Lec - 14.
JDBC 15-Apr-18.
CS320 Web and Internet Programming Database Access with JDBC
Advanced Web Automation Using Selenium
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
JDBC – Java DataBase Connectivity
Client Access, Queries, Stored Procedures, JDBC
Introduction to Server-Side Web Development using JSP and Databases
JDBC 15-Nov-18.
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
JDBC API.
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Java Access to RDB Relational databases (RDBs) dominate today, due to: Representational simplicity and flexibility Maturity Scalability Broad accessibility But applications are increasingly object oriented Especially with Java These apps view RDBs as persistent object systems How can this view be supported? Basically two choices Use low level, standard access facility: JDBC Use middleware: an object-relational gateway product

Accessing RDBs From Java Illustrated by access of an Oracle9i database Details on the two approaches JDBC Java DB Connectivity Send SQL strings to relational DB Receive enumerations of records as result Each query is an individual transaction Object relational gateway Presents OO view of relational data Each row is an object (exactly one!) Foreign key -> primary key mappings converted to references Lazy loading Transaction semantics

JDBC Provides platform and vendor independent access to relational DBs from Java clients Principal classes java.sql.Connection: manages DB connection java.sql.Statement: presents SQL query as String java.sql.ResultSet: returns collection of query result rows Two varieties of JDBC packages JDBC-ODBC bridge Uses ODBC database connectivity support in Windows Thin JDBC clients Pure Java solutions – good esp. for Web clients (applets)

Selecting a driver Java Application Driver Manager ODBC Microsoft Access Java Application Driver Manager Oracle Driver Oracle Connection object Other DB Driver Other DB

Sample code import java.sql.*; public class ConnectionDemo { public static void main( String av[] ) Connection conn; try { DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() ); conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:CSC", “scott", “tiger"); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery( "select * from branch" );

Sample code // iterate through rows in result set while ( rs.next() ) { // extract column values in current row String branchName = rs.getString( "branch_name" ); String branchCity = rs.getString( "branch_city" ); long assets = rs.getLong( "assets" ); System.out.println( branchName + " " + branchCity + " " + assets ); } // catch any SQLException in this block } catch ( SQLException e ) { System.out.println( e.toString() ); e.printStackTrace();

Connection Load DB driver DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() ); Create a connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:CSC", “scott", “tiger"); Close a connection conn.close();

Statement Statement object Create statement an envelope into which you put your message Create statement Statement statement = conn.createStatement(); Execute statement ResultSet rs = statement.executeQuery( "select * from branch" ); ResultSet rs = statement.executeUpdate( “delete from loan where loan-number=‘L-101’" );

Statement A generic execute statement is available String command = “..”; boolean hasResultSet = statement.execute(command); if (hasResultSet) { ResultSet result = statement.getResultSet(); …… } else { int count = statement.getUpdateCount(); }

PreparedStatement Provide better performance when frequently used PreparedStatement pstmt = conn.prepareStatement( insert into loan values (?, ?, ?)); pstmt.setString(1, “L-201”); pstmt.setString(2, “Downtown”); pstmt.setInt(1, 1500); int insCount = pstmt.executeUpdate(); System.out.println(“Inserted “ + insCount + “ rows.”);

ResultSet ResultSet organized into logical rows and columns Use next() to move to the next record Get columns by name and type while ( rs.next() ) { String branchName = rs.getString( "branch_name" ); String branchCity = rs.getString( "branch_city" ); long assets = rs.getLong( "assets" ); System.out.println( branchName + " " + branchCity + " " + assets ); }