1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Distributed Application Development B. Ramamurthy.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #18 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 JDBC Java Database Connectivity. 2 c.pdf
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
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.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
Lab: JDBC 1 Computer Engineering Lab III v Objective –to give some background on JDBC to help with the lab exercises , Semester 1,
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.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
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.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
Using Java with PSQL and Oracle Thanks to Drs. Raj and Liu for sharing some of these slides TRUDY: ALSO NEED SAMPLE PROGRAM.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
 2004 Keith Vander Linden. All Rights Reserved. Calvin CollegeDepartment of Computer Science(1/25) Database Programming with JDBC Keith Vander Linden.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
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.
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
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,
CS 405G: Introduction to Database Systems Database programming.
Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
JDBC (Java Database Connectivity)
JDBC Java DataBase Connectivity. Loading the driver import java.sql.*... Class.forName("org.postgresql.Driver")‏
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
1. Writing a Java program to connect to SQL Server 2008 and Create a table Populate the table (insert data) Perform queries to retrieve information from.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
Database Programming Basic JDBC Programming Concepts.
JSP and DB.
JDBC – Java Database Connectivity
Advanced Web Automation Using Selenium
HW#4 Making Simple BBS Using JDBC
Design and Implementation of Software for the Web
Objectives In this lesson, you will learn about:
Java Database Connectivity
JDBC Example.
Presentation transcript:

1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from

2 The Project: What You Will Need DBMS SQL (DDL and DML) Host languages (Java, C/C++, Perl, …) Web application servers (optional) SQL editors (optional) – e.g., Toad Tools for user interface (optional): forms, reports, etc.

3 Course DBMS Oracle Information about accessing the course DBMS: oracle.php oracle.php

4 SQL A data-definition and data-manipulation language Can be used for ad-hoc queries on (relational) databases  Generic SQL interface: users sit at terminals and ask queries on database Can be used in programs in some host language  Programs access (relational) database by “calls” to SQL statements

5 Connecting SQL to Host Language Embedded SQL  Special SQL statements (not part of host language)  Preprocessor transforms SQL statements into host-language code Call-level interfaces:  SQL/CLI (adaptation of ODBC)  JDBC: links Java programs to databases

6 JDBC Basics Read the tutorial at

7 Two-Tier Model Data source Application JDBC Driver

8 Steps to Use JDBC Loading a driver for our db system  Creates a DriverManager object Establishing a connection to database  Creates instance of a Connection object Using the connection to:  Create statement objects  Place SQL statements “in” these objects  Bind values to SQL statement parameters  Execute the SQL statements  Examine results tuple-at-a-time

9 DBMS Driver Specific information you need to know: see the sample JDBC program and the project FAQ on:  Driver for the course DBMS  Using the driver (add to classpath)  Driver specifics for your programs

10 firstExample.java // Loading the driver: Class.forName("oracle.jdbc.driver.OracleD river"); //Establishing a connection: Connection conn = DriverManager.getConnection(jdbcURL, user, passwd);

11 Statements Two JDBC classes:  Statement: object that can accept and execute a string that is a SQL statement  PreparedStatement: object that has an associated SQL statement ready to execute

12 Using Statements in JDBC Creating statements: using methods in the Connection class Executing statements:  executeUpdate: for database modifications  executeQuery: for database queries

13 firstExample.java // Create a statement object that will be sending your // SQL statements to the DBMS: Statement stmt = conn.createStatement(); // Create the COFFEES table: stmt.executeUpdate("CREATE TABLE COFFEES " + "(COF_NAME VARCHAR(32), SUP_ID INTEGER, " + "PRICE FLOAT, SALES INTEGER, TOTAL INTEGER)"); // Populate the COFFEES table: stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)"); // Get data from the COFFEES table: ResultSet rs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");

14 ResultSet An object of type ResultSet is like a cursor Method “next” advances cursor to next tuple:  The first time next() returns the first tuple  If no more tuples then next() returns FALSE Accessing components of tuples:  Method getX(name), where X is some type and name is an attribute name

15 firstExample.java // Now rs contains the rows of coffees and prices from // the COFFEES table. To access the data, use the method // NEXT to access all rows in rs, one row at a time while (rs.next()) { String s = rs.getString("COF_NAME"); float n = rs.getFloat("PRICE"); System.out.println(s + " " + n); }

16 JDBC URL (Oracle) The general form of a URL is jdbc:oracle: The is one of  thin  Use Java sockets  Recommended for our class  oci  Use Oracle OCI calls  Works through SQL *Net  kprb  Mainly for stored procedures

17 Notes The is either empty or of the form / A URL like has an empty username and password whereas this URL does not specify a username and password. When using this form the username and password must be provided some other way.

18 JDBC URL (Oracle) The description depends on the driver type.  (thin or oci driver and not bequeath) the database description is one of the following:  Service approach  // : /  SID approach  : :  Discouraged; Oracle will stop supporting this form.   TNS (Transparent Network Substrate)  You need to define the parameters for the TNS name

19 Example In our class: RLD Or Driver type: Hostname: Port: Service: SID:

20 JDBC Object Summary Basic JDBC objects:  DriverManager (DataSource is used instead in most applications)  Connection  Abstract representation of a DBMS session  Statement  Can be used to execute queries and update the database  ResultSet (= cursor)  Used to hold answers to database queries

21 In-Class Exercises - sqlplus login to remote-linux.eos.ncsu.edu using your unity account. add oracle10g run sqlplus   Password: 9 digit student ID check a few tables  tabs : all tables the user has  session_privs : privileges in this session  session_roles : roles in this session Useful SQL statement:  SELECT * FROM ;

22 In-Class Exercises - JDBC Preparation (see jdbc-prep.txt on course website)  Install JDK  Download JDBC driver  Configure CLASSPATH  Test running javac and java  Should be done before class

23 In-Class Exercises - JDBC Ex. 1  Open and read firstExample.java  Recognize critical steps just discussed in class  Edit the program to put in your username and password  Compile and run  Report the output

24 In-Class Exercises - JDBC Ex. 2  Use sqlplus to connect the Oracle server  Check tabs  Check table COFFEES Useful SQL statements:  SELECT * FROM ;  DROP TABLE ;

25 In-Class Exercises - JDBC Ex. 3:  The current program uses the service format for URL  Change it to SID format  Redo Ex 1 & 2 Useful SQL statements:  SELECT * FROM ;  DROP TABLE ;

26 In-Class Exercises - JDBC Ex. 4:  It’s annoying to have to drop table COFFEES through sqlplus.  Modify the program to drop table COFFEES if it’s already there. Report the drop action if it happens.  Hint: Just drop the table and catch the SQLException.

27 In-Class Exercises - JDBC Ex. 5 (Optional)  Use PreparedStatement instead of Statement to update the COFFEES table.  Put the values into arrays  You need to set parameters of the PreparedStatement  Redo Ex. 1 & Ex.2