JDBC Java and Databases. SWC – 2011 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.

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

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.
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
JDBC. JDBC (Java Database Connectivity): JDBC is an API for the Java programming language that defines how a client may access a database. It provides.
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.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
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.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
CSCI 6962: Server-side Design and Programming
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.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Python MySQL Database Access
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
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,
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
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.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 9: Accessing Databases with JDBC Instructions on Setting Accesses Connecting to and Querying.
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.
SQL introduction. SWC Getting data out of databases Databases are ”just” containers of data We could – in principle – just put data in a text.
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.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
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.
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
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
In the Name Of Almighty Allah. Java Application Connection To Mysql Created by Hasibullah (Sahibzada) Kabul Computer Science Faculty Afghanistan.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
JDBC.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JSP and DB.
Database: JDBC Overview
DEPTT. OF COMP. SC & APPLICATIONS
CS3220 Web and Internet Programming Database Access with JDBC
ODBC, OCCI and JDBC overview
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
Database JDBC Overview CS Programming Languages for Web Applications
Advanced Web Automation Using Selenium
HW#4 Making Simple BBS Using JDBC
Client Access, Queries, Stored Procedures, JDBC
MSIS 655 Advanced Business Applications Programming
Developing a Model-View-Controller Component for Joomla Part 3
Using a Database with JDBC
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

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 Java code Primary goal: to make the Java code (almost) independent of the actual data- base being used

SWC – JDBC JDBC allows us to use data from a database with relative ease in a Java program Main steps 1.Connect to a data source 2.Execute queries on the database 3.Store result of query in a data structure 4.Process the data according to business logic

SWC – JDBC There is nothing ”magical” about the JDBC classes and methods – it is just yet another part of the library Is found in java.sql.* packages

SWC – JDBC - setup Before starting, we need to install a so- called ”driver” for MySQL The driver program enables NetBeans to communicate with MySQL databases Comes as a.jar file (Java archive)

SWC – JDBC - setup The MySQL driver is called a ”MySQL connector” 1.Go to 2.Download the ”JDBC Driver for MySQL” 3.Dowload the.ZIP file 4.Unzip somewhere proper, e.g. in the ”MySQL” folder under ”Program Files”

SWC – JDBC - setup

SWC – JDBC - setup After having downloaded the driver, we must make a reference to it from the project that will use the driver We use the NetBeans Library Manager for this 1.Add MySQL driver to a library 2.Refer to the library from the project

SWC – JDBC - setup

SWC – JDBC - setup

SWC – JDBC - setup MySQL driver

SWC – JDBC - setup

SWC – JDBC - setup

SWC – JDBC - setup

SWC – JDBC - setup

SWC – JDBC - setup You’re done!

SWC – JDBC - connecting Making a connection to a data source (database) in done like this. Connection conn = DriverManager.getConnection( url, user, password); What are proper values for these three parameters?

SWC – JDBC - connecting In general, we can connect to a data source ”anywhere on the web” Therefore, the data source is specified as a URL (uniform resource locator) What is the URL for a database on our own computer…?

SWC – JDBC - connecting URL

SWC – JDBC - connecting

SWC – JDBC - connecting Simply copy the URL into the connection statement: Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/MovieInformation”,…,…); In practice, URL can be read from a setup file, registry, etc..

SWC – JDBC - connecting The ”user” and ”password” are those values you also use when connecting ”directly” to the database In practice, they should not be hard-coded into the Java code…

SWC – JDBC - connecting If connection succeeds, we have a connection object available Actual data source is now abstracted away… We can now execute statements on the connection object

SWC – JDBC - statements We can execute various types of statements on the connection object First, we must create a statement object Statement stmt = conn.createStatement();

SWC – JDBC - statements On the statement object, we can call various methods, that perform an operation on the database In particular, we can execute an SQL query on the statement object

SWC – JDBC - queries ResultSet srs = stmt.executeQuery("SELECT Title, prodYear FROM Movie");

SWC – JDBC - queries The return value of executeQuery is a result set (type ResultSet ) The result set object contains the result of the query, i.e the set of rows returned by the query A result set is somewhat similar to an ArrayList, but with some important differences

SWC – JDBC - queries A result set object has a cursor, which points to a specific row in the set The cursor can be moved in the result set, by calling certain methods For instance the next() method –Returns a boolean value (any more rows?) –Advances the cursor one row

SWC – JDBC - queries Typical processing of a result set: while (srs.next()) { // Process the record... }

SWC – JDBC - queries The actual processing of the result set is – of course – application dependent Typically, we will here map the result to the object model in the application This is a topic in itself… Typically, some application-specific class acts as a ”bridge” between relational model and object model

SWC – JDBC - queries while (srs.next()) { String title = srs.getString("Title"); int year = srs.getInt("prodYear"); objectModel.addMovie(new Movie(title, year)); }

SWC – JDBC - queries The ResultSet methods enable us to retrieve the actual field values from each row in the result set Many more details about result set methods are found at

SWC – JDBC - updates It is fairly straightforward to retrieve data from a database using JDBC –Create a connection object, using a URL –Create a statement object –Execute a query on the statement object –Process the result set returned from the query What if we wish to update some data…?

SWC – JDBC - updates It it very important to realise, that the result set object and the underlying database are still ”connected” to each other If we update a value in the result set, the corresponding field in the database is also updated! This is the beauty of JDBC!

SWC – JDBC - updates A few technicalities… A result set has some properties with regards to –Freedom of cursor movement –Ability to update database These properties are specified when we create the statement object

SWC – JDBC - updates In order to let us update the data, we must create the statement object like: Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

SWC – JDBC - updates while (srs.next()) { String title = srs.getString("Title"); int year = srs.getInt("prodYear");... srs.updateInt("prodYear", year + 30); srs.updateRow(); } This also updates the DB

SWC – JDBC - insertion A result set also allows us to insert a new record into the result set – and thereby into the database The procedure requires a few steps: –Move the cursor to a special row called the ”insert row” –Set field values by using the update… methods –Finally, the row is actually inserted

SWC – JDBC - insertion srs.moveToInsertRow(); srs.updateString("TITLE", "District 9"); srs.updateString("COUNTRY", "UK"); srs.updateString("GENRE", "Sci-Fi"); srs.updateInt("MOVIEID", 11); srs.updateInt("PRODYEAR", 2009); srs.updateInt("OSCARS", 0); srs.insertRow();

SWC – JDBC - deletion Finally, it is also possible to delete a row from the result set – and thereby also from the database srs.deleteRow(); Deletes the row to which the result set cursor currently points

SWC – Keeping models in sync We could – in principle – just execute queries, updates, etc. directly on the database, without using result sets It is much more efficient to use result sets But…when and how do we update…?

SWC – Keeping models in sync Object model Result set Appli- cation DB

SWC – Keeping models in sync Questions to ponder –When do we load the data initially? Application start-up When object model is accessed User initiative –When (how often) is data saved? Application shutdown Upon every change to object model User initiative

SWC – Keeping models in sync Questions to ponder (continued) –How much data do we save, when we save Everything (brute force). Inefficient, but easy to implement in code Difference between current object model, and object model at last save. Efficient, but requires more book-keeping