95-712 Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent:

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

RMI and JDBC Some Database terminology Connecting a remote object to a database From relations to XML Gary Alperson helped developed these slides and the.
Java/JDBC Some Database terminology (brief) A simple stand alone JDBC Application Java Server Pages and Scoping A simple JSP and JDBC example JSP and JDBC.
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.
19-Jun-15 SQL. SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell” is the only correct.
Java Database Connectivity By: Abe Marji CS616. Agenda 1.Quick Review of Databases 2.What is SQL? 3.What is JDBC? 4.Advanced Functions of JDBC 5.Summary.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
ODBC and JDBC What are they – libraries of function calls that support SQL statements Why do we need them –Provide a way for an application to communicate.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
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).
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
CSE470 Software Engineering Fall Database Access through Java.
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.*;
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
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. 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 Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
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. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Oracle & SQL Introduction. Database Concepts Revision DB? DBMS? DB Application? Application Programs? DBS? Examples of DBS? Examples of DBMS? 2Oracle.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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.
Presentation On How To Create Connection To A Database.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
Connecting to MySQL using Java By:. – Required to use Java.sql so that we can use Connection and Queries using strings. – Javax.swing.* needed for components.
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.
Web Programming MySql JDBC Web Programming.
Basics of JDBC Session 14.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
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.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
MYSQL APPLICATIONS USING JAVA & JDBC CIS 430 –Database Concepts Victor Matos.
Lec - 14.
JDBC 15-Apr-18.
This shows the user interface and the SQL Select for a situation with two criteria in an AND relationship.
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
JDBC.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC – Java DataBase Connectivity
Introduction to Server-Side Web Development using JSP and Databases
JDBC 15-Nov-18.
Interacting with Database
Lecture 11 Database Connection
JDBC – Java DataBase Connectivity
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent: database connections SQL Statements Result sets database metadata Can be connected to ODBC We’ll review some database terminology And look over a simple stand alone JDBC Application

Object Oriented Programming Java Database Terminology Database: A shared collection of logically related data (and a description of this data) designed to meet the information needs of an organization Relation: A table with columns and rows Attribute: A named column of a relation Tuple: A row in a relation Definitions from Database Systems by Connolly, Begg, and Strachan Gary Alperson helped developed these slides and the JDBC example.

Object Oriented Programming Java Sample Table

Object Oriented Programming Java Attribute

Object Oriented Programming Java Tuple

Object Oriented Programming Java SQL Data Definition Language (DDL) –Create tables –Modify tables –Delete (drop) tables Data Manipulation Language (DML) –Insert data –Update data –Select data

Object Oriented Programming Java Select Statement We will use this data for our examples

Object Oriented Programming Java From the broker table, select the contents of the last name attribute Query SELECT lname FROM broker; Results SQL is not case sensitive. Key SQL words are capitalized and line breaks are inserted by convention.

Object Oriented Programming Java From the broker table, select all attributes Query SELECT * FROM broker; Results * Acts as a wildcard

Object Oriented Programming Java From the broker table, select all attributes where the last name is Smith Query SELECT * FROM broker WHERE lname = ‘Smith’; Results Note that the string is enclosed by single quotes The contents of a string are case sensitive

Object Oriented Programming Java Use AND or OR to connect multiple where clauses Query SELECT * FROM broker WHERE lname = ‘Smith’ AND fname = ‘John’; Results

Object Oriented Programming Java Example with two Tables One-to-many relationship Each broker may have many customers Each customer is only affiliated with one broker The b_id joins both tables by identifying the unique broker that each customer is associated with

Object Oriented Programming Java Cartesian Product When you do a query on multiple tables, SQL begins by creating the Cartesian product, which combines each tuple from one relation from every tuple of the other relation. (Actual SQL implementations are free to compute the resulting table efficiently,i.e., the actual Cartesian product may not be generated at all.)

Object Oriented Programming Java Query SELECT * FROM customer, broker WHERE broker.b_id = 1; SQL does not realize that the b_id in the customer table is the same as the b_id in the broker table unless you join them in the where clause. Results

Object Oriented Programming Java Cartesian Product Query SELECT * FROM customer, broker WHERE broker.b_id = 1 AND broker.b_id = customer.b_id; Results

Object Oriented Programming Java ODBC ODBC is a programming interface that enables applications to access data in database systems that use Structured Query Language (SQL) as a data standard.

Object Oriented Programming Java Creating an ODBC Connection Click on the Start button. Choose Settings, Control Panel Double-click on ODBC Data Sources Choose the System DSN tab Click Add

Object Oriented Programming Java Click on the desired driver (MSAccess) Click on the Finish button

Object Oriented Programming Java Enter a Data Source Name Click on the Select button Locate the desired file or directory Click OK

Object Oriented Programming Java SQL Query as a Java String The SQL SELECT customer.lname FROM customer, broker WHERE broker.lname = ‘Smith’ AND broker.b_id <> 1 AND broker.b_id = customer.b_id; From both tables select the last names of all customers whose broker’s last name is Smith but whose broker ID is not 1.

Object Oriented Programming Java Executing a query in Java // Statement aStatement = statement got from connection String last = “Smith”; int nonID = 1; String q = “SELECT customer.lname FROM customer, broker” + “WHERE broker.lname = \’” + last + “\’ AND broker.b_id” + “<>” + nonID + “AND broker.b_id = customer.b_id;”); ResultSet rs = aStatement.executeQuery(q); The slash (\) is the escape character. It precedes the single quote to tell Java to include that quote in the String The String last is outside of the double quotes, because it must be concatonated with the String sent to the database, but it falls within the single quotes so that SQL treats it as a string nonID does not go within single quotes since it is numeric Since the String is an SQL statement, it uses = and <> rather than == and !=

Object Oriented Programming Java A Simple Application // This program makes use of a stock database // and the primary JDBC classes (Connection, Statement, // ResultSet and ResultSetMetaData) import java.util.*; import java.sql.*; import java.io.*; public class TestCoolStocksDB { public static void main(String args[]) { Connection con = null; Statement s = null;

Object Oriented Programming Java ResultSet rs = null; ResultSetMetaData rsm = null; String answer = ""; try { DriverManager.registerDriver( new sun.jdbc.odbc.JdbcOdbcDriver()); con = DriverManager.getConnection("jdbc:odbc:CoolStocks"); s = con.createStatement(); rs = s.executeQuery("select * from customer"); rsm = rs.getMetaData();

Object Oriented Programming Java while(rs.next()) { for(int col = 1; col <= rsm.getColumnCount(); col++) answer += rs.getString(col); } con.close(); } catch (SQLException sqle) { System.err.println("Exception caught in main:" + sqle); } System.out.println(answer); }

Object Oriented Programming Java A Simple JSP/JDBC Example stocks.mdb database schema There are three tables. Both customer and stocks have a one- to-many relationship with portfolios. The database stocks.mdb was registered with the ODBC driver as “CoolStocks”

Object Oriented Programming Java Register w/ODBC Create an ODBC data source. Click on the Start button. Choose Settings, Control Panel Double-click on ODBC Data Sources Choose the System DSN tab Click Add Click on the desired driver (MSAccess) Click on the Finish button Enter a Data Source Name (I called my database CoolStocks and that name appears in the java code below) Click on the Select button Locate the directory and file containing your database. This will be the “stock.mdb” file created by Microsoft Access. Click OK

Object Oriented Programming Java A Simple JSP/JDBC Example JSP JDBC Example 1 <!– Adapted from James Goodwill’s Pure JSP 

Object Oriented Programming Java <% Connection con = null; try { // Load the Driver class file Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Make a connection to the ODBC datasource Movie Catalog con = DriverManager.getConnection("jdbc:odbc:CoolStocks"); // Create the statement Statement statement = con.createStatement(); // Use the created statement to SELECT the DATA // FROM the customer Table. ResultSet rs = statement.executeQuery("SELECT * " + "FROM customer"); // Iterate over the ResultSet %>

Object Oriented Programming Java Customer - ID Last Name First Name <% while ( rs.next() ) { // get the id, convert to String out.println(" \n " + rs.getString("id") + " "); // get the last name out.println(" " + rs.getString("lname") + " "); // get the first name out.println(" " + rs.getString("fname") + " \n</TR"); }

Object Oriented Programming Java // Close the ResultSet rs.close(); } catch (IOException ioe) { out.println(ioe.getMessage()); } catch (SQLException sqle) { out.println(sqle.getMessage()); } catch (ClassNotFoundException cnfe) { out.println(cnfe.getMessage()); } catch (Exception e) { out.println(e.getMessage()); }

Object Oriented Programming Java finally { try { if ( con != null ) { // Close the connection no matter what con.close(); } catch (SQLException sqle) { out.println(sqle.getMessage()); } %>

Object Oriented Programming Java It Works!