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.

Slides:



Advertisements
Similar presentations
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 PostgreSQL JDBC interface documentation
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.
Servlets and JDBC. Servlets A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
1 CSE5200 JDBC and JDeveloper JDBC java.sql package java.sql classes.
Distributed Application Development B. Ramamurthy.
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
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
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. –
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
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.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© 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.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
 2004 Keith Vander Linden. All Rights Reserved. Calvin CollegeDepartment of Computer Science(1/25) Database Programming with JDBC Keith Vander Linden.
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.
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.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
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.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
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.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
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.
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.)
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
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.
JDBC (Java Database Connectivity)
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.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
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.
CS3220 Web and Internet Programming Database Access with JDBC
Interacting with Database
1st Semester, 2017 Sanghyun Park
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Interacting with Database
JDBC – ODBC DRIVERS.
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

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 using SQL commands. Most important members, such as Connection, Statement, and ResultSet, are interfaces instead of being classes. –This is because, the whole point of JDBC is to hide the specifics of accessing a particular database. An application programmer doesn’t have to worry about the implementation of the underlying classes. –All you have to do is to use the methods defined by the various interfaces. –The implementation of the underlying classes is done in the vendor provided driver and associated classes.

Basics The DriverManager class is responsible for keeping track of all the JDBC drivers that are available on a system. First task of a JDBC program is to load an appropriate driver for the type of database being used. After that a JDBC program should connect to the database by calling DriverManager.getConnection(). –You specify the database to connect with a jdbc:URL. This URL has the following general syntax: jdbc:subprotocol:host:port:databasename import java.sql.*; class InsertMovie { public static void main (String args []) throws SQLException { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection "thomo", “password"); userid, password …

Creating JDBC Statements A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it: –For a SELECT statement, the method to use is executeQuery. –For statements that create or modify tables, the method to use is executeUpdate. It takes an instance of an active connection to create a Statement object. In the following example, we use our Connection object conn to create the Statement object stmt: –Statement stmt = conn.createStatement(); At this point stmt exists, but it does not have an SQL statement to pass on to DBMS. We need to supply that with execute…

import java.sql.*; class InsertMovie { public static void main (String args []) throws SQLException { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection "thomo", “password"); userid, password String title = "Movie ABCDEF"; int year = 2005; int length = 200; String studioName = "UVic"; String statementString = "INSERT INTO Movie(title, year, length, studioName) " + "VALUES( '" + title + "'," + year + "," + length + ",'" + studioName + "')"; Statement stmt = conn.createStatement(); stmt.executeUpdate(statementString); stmt.close(); }

Getting Data Recall the impediance mismatch problem… Well, Java provides us a class ResultSet which helps us bridge the gap. ResultSet rset = stmt.executeQuery( "SELECT title, year " + "FROM Movie"); while (rset.next()) … The variable rset, which is an instance of ResultSet, contains the rows of the query result. In order to access the titles and years, we will go to each row and retrieve the values according to their types. The first call of the method next moves the cursor to the first row and makes it the current row. Successive invocations of the method next move the cursor down one row at a time from top to bottom.

Using the getXXX methods Use the getXXX method of the appropriate type to retrieve the value in each column. –For example, the first column in each row of the previous rset stores a value of SQL type VARCHAR. The method for retrieving a value of SQL type VARCHAR is getString. –The second column in each row stores a value of SQL type INT, and the method for retrieving values of that type is getInt. while (rset.next()) { String s = rset.getString("Title"); int n = rset.getInt("Year"); System.out.println(s + " " + n); } However we can also say instead: (which is slightly more efficient) String s = rset.getString(1); int n = rset.getInt(2); Column Name Column Position

Using the getXXX methods (Continued) JDBC allows a lot of flexibility as far as which getXXX methods you can use to retrieve the different SQL types. For example, the method getInt can be used to retrieve any of the numeric or character types. The data it retrieves will be converted to an int; –that is, if the SQL type is VARCHAR, JDBC will attempt to parse an integer out of the VARCHAR. The getString method can retrieve any other datatype. However, in such a case we should convert strings to numbers.

import java.sql.*; class dbAccess { public static void main (String args []) throws SQLException { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection "thomo", “password"); userid, password Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "SELECT title, year " + "FROM Movie"); while (rset.next()) System.out.println (rset.getString("title") + " " + rset.getString("year")); stmt.close(); }

Prepared statements Sometimes it is more convenient or more efficient to use a PreparedStatement object for sending SQL statements to the database. –This special type of statement is derived from the more general interface, Statement, that you already know. If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. The main feature of a PreparedStatement object is that, unlike a Statement object, it is given an SQL statement when it is created. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. –As a result, 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 's SQL statement without having to compile it first.

Prepared statements (Continued) Using our open connection conn from previous examples, we create a PreparedStatement: PreparedStatement updateMovies = conn.prepareStatement( "UPDATE Movie SET studioName = ? WHERE studioName = ?"); The variable updateMovies now contains the SQL statement, "UPDATE Movie SET studioName = ? WHERE studioName = ?", which has also, been sent to the DBMS and been precompiled.

import java.sql.*; class UpdateMovie { public static void main (String args []) throws SQLException { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection "thomo", “password"); userid, password PreparedStatement updateMovieStatement; String updateMovieString = "UPDATE Movie " + "SET studioName = ? " + "WHERE studioName LIKE ?"; updateMovieStatement = conn.prepareStatement(updateMovieString);

String studiosBoughtByParamount [] = {"Disney", "Fox"}; for(int i=0; i<studiosBoughtByParamount.length; i++) { updateMovieStatement.setString(1, "Paramount"); updateMovieStatement.setString(2, "%"+studiosBoughtByParamount[i]+"%"); updateMovieStatement.executeUpdate(); } updateMovieStatement.close(); }