CMPUT 391 Database Management Systems. JDBC in Review. - Lab 2 -

Similar documents
SQL and Java. Database Systems Lecture 20 Natasha Alechina

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

Top 50 JDBC Interview Questions and Answers

Database connectivity (II)

JDBC [Java DataBase Connectivity]

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

Chapter 16: Databases

Lab # 9. Java to Database Connection

JDBC. Sun Microsystems has included JDBC API as a part of J2SDK to develop Java applications that can communicate with databases.

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

Embedded SQL. Introduction

Embedded SQL. Davood Rafiei

Java Database Connectivity

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

O ne of the most important features of JavaServer

Introduction to SQL & Database Application Development Using Java

SQL in a Server Environment

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

DB I. 1 Dr. Ahmed ElShafee, Java course

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Non-interactive SQL. EECS Introduction to Database Management Systems

JDBC - INTERVIEW QUESTIONS

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

JAVA AND DATABASES. Summer 2018

Database Applications (15-415)

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

JDBC Java Database Connectivity is a Java feature that lets you connect

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

JDBC Programming: Intro

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

Accessing databases in Java using JDBC

Introduction to Databases

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

Unit 3 - Java Data Base Connectivity

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

Unit 2 JDBC Programming

Java Database Connectivity

UNIT-3 Java Database Client/Server

Server-side Web Programming

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

How to program applications. CS 2550 / Spring 2006 Principles of Database Systems. SQL is not enough. Roadmap

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Topic 12: Database Programming using JDBC. Database & DBMS SQL JDBC

Java Database Connectivity

Lecture 2. Introduction to JDBC

Programming in Java

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

JDBC 3.0. Java Database Connectivity. 1 Java

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

Working with Databases and Java

PERSİSTENCE OBJECT RELATİON MAPPİNG

SQL-PL Interface. Murali Mani. Perl /CGI with Oracle/mySQL Install your own web server and use servlets with JDBC and Oracle/mySQL

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

Databases 2012 Embedded SQL

Java Database Connectivity (JDBC) 25.1 What is JDBC?

SQL DML and DB Applications, JDBC

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

Database Application Development

UNIT III - JDBC Two Marks

CSE 135. Three-Tier Architecture. Applications Utilizing Databases. Browser. App. Server. Database. Server

EMBEDDED SQL. SE 3DB3 Fall 2016 MICHAEL LIUT DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY

SQL: Programming Midterm in class next Thursday (October 5)

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

Types of Databases. Types of Databases. Types of Databases. Databases and Web. Databases and Web. Relational databases may also have indexes

COMP102: Introduction to Databases, 23

SNS COLLEGE OF ENGINEERING, Coimbatore

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

JDBC. Oracle ODBC SP API SP API. SQL server C function calls. SQL server ODBC SP API. Oracle DSN Oracle ODBC Oracle

JDBC MOCK TEST JDBC MOCK TEST IV

Running SQL in Java and PHP

IBM -

Developer s Guide. Version 4.4

Designing a Persistence Framework

Servlet 5.1 JDBC 5.2 JDBC

13.1 Relational Databases (continued) 13.1 Relational Databases. - Logical model

13.1 Relational Databases

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

SQL Application Programming. CS 377: Database Systems

SQL Environment: Module Types. System Aspects of SQL. SQL Environment: Introduction. SQL Environment: Introduction. SQL Environment: Privileges

Running SQL in Java and PHP

Pieter van den Hombergh. March 25, 2018

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

CSE 308. Database Issues. Goals. Separate the application code from the database

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

Database Application Development

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

Transcription:

CMPUT 391 Database Management Systems JDBC in Review - - Department of Computing Science University of Alberta

What Is JDBC? JDBC is a programming interface JDBC allows developers using java to gain access to a wide range of DBMSs JDBC allows users of different operating systems to access the same DBMS 2

What can you do with JDBC? Connect to different types of DBMSs Integrate SQL with Java to interact with data sources JSP, Servlet, Applet Your Assignments and/or Projects. 3

JDBC Architecture Java Application JDBC API JDBC Driver Manager JDBC Driver DBMS Server Comes in four varieties For more information: http://java.sun.com/products/jdbc/driverdesc.html 4

JDBC Interface Provides Library of function calls Standard way to connect Standard representation of data types Standard set of error codes Performs Connections with a database or other tabular data sources Sending of SQL statements Processing of results 5

Basic JDBC Classes DriverManager: manages connection between the data source and driver Connection: establishes connection to data source Statement: used to send DDL and DML statements to the data source ResultSet: used to access results of a query 6

Getting Started Environment Settings > echo $CLASSPATH If /oracle/jdbc/lib/classes12.zip is not set: For bash users, add to.bashrc: > export CLASSPATH=$CLASSPATH\:.\:/oracle/jdbc/lib/classes12.zip For csh or tcsh users, add to.cshrc: > setenv CLASSPATH $CLASSPATH\:.\:/oracle/jdbc/lib/classes12.zip Import java.sql package in your java code import java.sql.*; 7

Getting Started (cont.) Establishing a connection Loading the driver Class drvclass = Class.forName(driverName); Making a connection Connection con = DriverManager.getConnection (url, username, password); drivername = oracle.jdbc.driver.oracledriver url = jdbc:oracle:thin:@gwynne.cs.ualberta.ca:1521:crs Calling Class.forName will create an instance of the driver and register it for you automatically. You don t need to use DriverManager.registerDriver method in this context. http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html 8

Getting Started (cont.) Sending SQL Statements Creating JDBC Statements Statement stmt = m_con.createstatement(); Executing Statements stmt.executequery("select mname, category from movie"); stmt.executeupdate("insert into movie (movieid) values (101)"); Manipulating the results: ResultSet class 9

ResultSet Class represents the results of a query to the database the result can be read sequentially from the ResultSet object using method next() Example: rset.next(), where rset is an object of class ResultSet 10

Creating a ResultSet use method createstatement from class Connection to specify properties of the ResultSet object. Example: creating a scrollable ResultSet Statement stmt= conn.createstatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rset=stmt.executequery( select ); 11

ResultSet Properties Specification types: TYPE_FORWARD_ONLY (default) TYPE_SCROLL_INSENSITIVE (allows scroll and doesn t reflect changes) TYPE_SCROLL_SENSITIVE (reflects changes) concurrency: CONCUR_READ_ONLY (default, no updates) CONCUR_UPDATABLE (updates allowed) 12

ResultSet Properties Specification (cont.) Result Set Type Can See Internal Delete Can See Internal Update Can See Internal Insert Can See External Delete Can See External Update Can See External Insert Forward-only Yes Scroll-insensitive Yes Yes Scroll-sensitive Yes Yes Yes Internal changes user changes in the ResultSet External changes changes made from elsewhere (user changes outside the ResultSet or other committed transactions) 13

ResultSet Methods for Cursors rset.next() move forward one row; rset.previous() move backward one row; rset.first() move to the first row; rset.last() move to the last row; rset.getrow() obtain current position rset.absolute(int n) move to the n th row rset.relative(int n) move n rows from current 14

Updating Data using ResultSet move ResultSet rset to the row to be changed use method rset.updatexxx(column,value) to change e.g.: rset.updateint(1,25) OR rset.updateint( age,25) use method rset.updaterow() to make changes permanent te 1: statement must be CONCUR_UPDATABLE te 2: cannot be used if query is select * from 15

Inserting Data using ResultSet move cursor to special insert row using method call rset.movetoinsertrow() set every column value using method call rset.updatexxx( ) insert new row in ResultSet and table using method call rset.insertrow() te: statement must be TYPE _SCROLL_SENSITIVE and CONCUR_UPDATABLE 16

Deleting Data using ResultSet move cursor to the desired row delete row from ResultSet and table using method call rset.deleterow() te: statement must be TYPE_SCROLL_SENSITIVE and CONCUR_UPDATABLE 17

Using MetaData to Display the ResultSet ResultSetMetaData class provides information about types and properties in a ResultSet use method getmetadata() on a ResultSet object to get the result set s metadata information use methods from ResultSetMetaData class to get the available information getcolumncount () returns the number of columns in the ResultSet getcolumnlabel (int column) returns the column title for use to display getcolumntype (int column) returns a column s SQL type 18

Using MetaData to Display the ResultSet (cont.) ResultSetMetaData rsetmetadata = rset.getmetadata(); /* get metadata for ResultSet rset*/ int columncount = rsetmetadata.getcolumncount(); /* get number of columns in result set*/ for ( int column = 1; column <= columncount; column++) { } value = rsetmetadata.getcolumnlabel(column); /* get column name */ while (rset.next() ) { } for ( int index = 1; index <= columncount; index++) { /* get data one tuple at a time */ o = rset.getobject(index); /* get the value as an Object */ if (o!= null ) value = o.tostring(); /* convert it to String to display it */ else } value = "null"; 19

What s Next: Work through the JDBC Tutorial #1 and #2 at: http://ugweb.cs.ualberta.ca/~c291/tutorials/jdbc/1/ http://ugweb.cs.ualberta.ca/~c291/tutorials/jdbc/2/ Solve the exercises at the end of tutorial Other Reference Materials: http://ugweb.cs.ualberta.ca/~c391/ Questions? 20