Introduction to JDBC Instructor: Mohamed Eltabakh 1.

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 (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.
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.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
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
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.
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.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
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.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
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.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
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.
Advanced SQL: Cursors & Stored Procedures
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
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.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
Computer Science [3] Java Programming II - Laboratory Course Lab 9: Accessing Databases with JDBC Instructions on Setting Accesses Connecting to and Querying.
JDBC - Connection The programming involved to establish a JDBC connection is fairly simple. Here are these simple four steps − Import JDBC Packages: Add.
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.
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 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
Basics of JDBC.
JDBC (Java Database Connectivity)
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.
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.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_Oracle10g_JDBC 1 Application Development (JDBC)
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.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JSP and DB.
Database: JDBC Overview
CS3220 Web and Internet Programming Database Access with JDBC
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
1st Semester, 2017 Sanghyun Park
Lec - 14.
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
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Java Database Connectivity
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Introduction to JDBC Instructor: Mohamed Eltabakh 1

Ways to Access DB 2 Direct SQL PL/SQL External language connected to DB

What is JDBC JDBC: Java Database Connectivity JDBC is a standard interface for connecting to relational databases from Java 3 Java code DB

4 Overview of Querying a Database With JDBC

Stage 1: Connect 5

JDBC Driver 6 Database JDBC driver Java appDatabase JDBC calls Database commands JDBC driver Inside the code, you make calls using JDBC APIs Software module that translates JDBC calls to SQL commands

JDBC Driver Is an interpreter that translates JDBC method calls to vendor-specific database commands Implements interfaces in java.sql Can also provide a vendor ’ s extensions to the JDBC standard 7

How to Make the Connection 8 1.Register the driver. DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Class.forName(“oracle.jdbc.driver.OracleDriver”); Or

How to Make the Connection 9 2. Connect to the DB Connection conn = DriverManager.getConnection (URL, userid, password); Connection conn = DriverManager.getConnection "userid", "password"); In our Oracle DB Your Oracle username and pw

10 Import java.sql package Register the driver Establish connection

Stage 2: Query the DB 11

JDBC Statement Object A Statement object sends your SQL command to the database You need an active connection to create a JDBC statement Statement has methods to execute a SQL statement: executeQuery() for QUERY statements executeUpdate() for INSERT, UPDATE, DELETE 12

How to Query the Database 13 Output relation Number of affected tuples The query string

Querying the Database: Example I 14

Querying the Database: Example II 15 Execute a select statement Statement stmt = conn.createStatement(); String str = "SELECT * FROM users”; Resultset rset = stmt.executeQuery(str); Build your SQL command in a separate string and then pass it for execution

Stage 3: Process Results 16

Resultset Object JDBC returns the results of a query in a ResultSet object. A ResultSet maintains a cursor pointing to its current row of data. Use next() to step through the result set row by row. getString(), getInt(), and so on assign each value to a Java variable. 17

How to Process Results 18

Example 19 Statement stmt = conn.createStatement(); String str = " SELECT branch_name, amount FROM R”; Resultset rset = stmt.executeQuery(str); While ( rset.next()) { String bName = rset.getString(“branch_name”); int amt = rset.getInt(“amount”); … System.out.println(”Name:” + bName + ” Amount: ” + amt); }

Getxxx Function over Resultset 20 And many more:

Stage 4: Closing 21

How to Close 22

JDBC PrepareStatement If execute the statement multiple times Use a PrepareStatement object It is compiled once and used multiple times PrepareStatement may contain variables Placeholder for actual values supplied at execution time 23

How to create PrepareStatement 24 “?” Is the placeholder for variables

How to Execute 25 For SQL queries For Insert/Update/ Delete With each execution set the values and then execute…

How to Connect to WPI Oracle 1- Log in to CCC machine 2- Set environment variables > source /usr/local/bin/oraenv 3- Set CLASSPATH for java > export CLASSPATH=./:/usr/local/oracle11gr203/product/11.2.0/ db_1/jdbc/lib/ojdbc6.jar 4- Write your java code (say file name is OracleTest.java) and then compile it > Javac OracleTest.java 5- Run it > Java OracleTest 26

Sources Some links with examples