JDBC – Java Database Connectivity

Slides:



Advertisements
Similar presentations
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
Advertisements

JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
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.
1 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
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.
1 JDBC – Java Database Connectivity Representation and Management of Data on the Internet.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
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.
Think Possibility Integrating Web Applications With Databases.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
1 JDBC – Java Database Connectivity Modified slides from Dr. Yehoshua Sagiv.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
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  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Chapter 8 Databases.
JDBC – Java Database Concentricity
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.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
CS122B: Projects in Databases and Web Applications Winter 2016
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
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.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC.
JDBC 2 Getting Started Guide: etstart/GettingStartedTOC.fm.html java.sql Package API:
1 JDBC – Java Database Connectivity THETOPPERSWAY.COM.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
CompSci 280 S Introduction to Software Development
CS3220 Web and Internet Programming Database Access with JDBC
Lec - 14.
JDBC 15-Apr-18.
JDBC
Web Technologies IT230 Dr Mohamed Habib.
CS320 Web and Internet Programming Database Access with JDBC
Database JDBC Overview CS Programming Languages for Web Applications
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Design and Implementation of Software for the Web
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – Java Database Connectivity
Bolat Azamat, Kim Dongmin
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

JDBC – Java Database Connectivity CS 236607, Spring 2008/9

Today’s Menu JDBC Architecture Using JDBC Timeout ResultSet Object Null Values Prepared Statements

JDBC (Java Database Connectiveity) is an API (Application Programming Interface) That is, a collection of classes and interfaces JDBC is used for accessing databases from Java applications Information is transferred from relations to objects and vice-versa

JDBC Architecture

JDBC Architecture Java code calls JDBC library JDBC loads a driver The driver talks to a particular DBMS An application can work with several DBMS by using corresponding drivers

“Movies” Relation movieName producer releaseDate Movie1 Producer1 1.1.2000 Movie2 Producer2 1.1.2001 Movie3 Producer3 3.4.2003

7 Steps for Using JDBC Load the driver Define the connection URL Establish the connection Create a Statement object Execute a query using the Statement Process the result Close the connection

Loading the Driver Class.forName loads the given class dynamically Class.forName(“com.mysql.jdbc.Driver ”); Class.forName loads the given class dynamically When the driver is loaded, it automatically creates an instance of itself registers this instance within DriverManager Another way: Driver driver = new com.mysql.jdbc.Driver(); DriverManager.registerDriver(driver); MySql JDBC driver can be downloaded from here.

Define the connection URL Every database is identified by a URL Given a URL, DriverManager looks for the driver that can talk to the corresponding database DriverManager tries all registered drivers,until a suitable one is found

An Example // A driver for imaginary1 Class.forName("ORG.img.imgSQL1.imaginary1Driver"); // A driver for imaginary2 Driver driver = new ORG.img.imgSQL2.imaginary2Driver(); DriverManager.registerDriver(driver); //A driver for PostgreSQL Class.forName("org.postgresql.Driver");

Establish the connection Connection con = DriverManager.getConnection("jdbc:imaginaryDB1://localhost:3306/");

4. Create a Statement object We use Statement objects in order to Query the DB Update the DB(insert, update, create, drop, …)

5. Execute a query using the Statement executeQuery returns a ResultSet object representing the query result (discussed later…)

Manipulating DB with Statement String deleteStr = “delete from movies where movieName=‘Movie1’ ”; Statement stmt = con.createStatement(); int rowsDeleted = stmt.executeUpdate(deleteStr); executeUpdate is for data manipulation: insert, delete, update, create table, etc. executeUpdate returns the number of rows modified (or 0 for DDL commands)

6. Process the result We will discuss ResultSet in a while…

7. Close the connection Close Connections, Statements, and Result Sets con.close(); stmt.close(); rs.close(); ‘finally’ block is a good place…

ResultSet ResultSet objects provide access to the tables generated as results of executing Statement queries. Only one ResultSet per Statement can be open at a given time! The table rows are retrieved in sequence: A ResultSet maintains a cursor pointing to its current row. next() moves the cursor to the next row

ResultSet Methods boolean next() Activates the next row First call to next() activates the first row Returns false if there are no more rows Not all of the next calls actually involve the DB void close() Disposes of the ResultSet Allows to re-use the Statement that created it Automatically called by most Statement methods Type getType(int columnIndex) Returns the given field as the given type Indices start at 1 and not 0! Add the column name as a comment if it is known! Type getType(String columnName) Same, but uses name of field int findColumn(String columnName) Looks up column index given column name Type = int || double || long || boolean || byte || time || date …

Timeout Use setQueryTimeOut(int seconds) of Statement class to set a timeout for the driver to wait for a query to be completed. If the operation is not completed in the given time, an SQLException is thrown What is it good for?

Mapping Java Types to SQL Types

Null Values In SQL, NULL means the field is empty Not the same as 0 or “”! In JDBC, you must explicitly ask if the last read field was null ResultSet.wasNull(column) For example, getInt(column) will return 0 if the value is either 0 or NULL!

Database Time Times in SQL are notoriously non-standard Java defines three classes to help java.sql.Date year, month, day java.sql.Time hours, minutes, seconds java.sql.Timestamp year, month, day, hours, minutes, seconds, nanoseconds Usually use this one

Exceptions An SQLException is actually a list of exceptions

Prepared Statements The PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first. Most often used for SQL statements that take parameters.

Creating a PreparedStatement Object As with Statement objects, you create PreparedStatement objects with a Connection method. The following code create a PreparedStatement object that takes two input parameters:

Supplying Values for PreparedStatement Parameters You need to supply values to be used in place of the question mark placeholders (if there are any) before you can execute a PreparedStatement object. You do this by calling one of the setXXX methods defined in the PreparedStatement class.

Example updateSales.setInt(1, 75); the following line of code sets the first question mark placeholder to a Java int with a value of 75: updateSales.setInt(1, 75); The next example sets the second placeholder parameter to the string " Colombian": updateSales.setString(2, "Colombian");

Another Example

Callable Statements Execute a call to a database stored procedure. We will not go into details

Resources used for this presentation http://www.cs.huji.ac.il/~dbi/recitations/JDBC-PSQL-c.pdf http://java.sun.com/docs/books/tutorial/jdbc/ http://www.java-samples.com/showtutorial.php?tutorialid=202