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.

Slides:



Advertisements
Similar presentations
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme.
Distributed Application Development B. Ramamurthy.
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.
EEC-681/781 Distributed Computing Systems Lecture 5 Wenbing Zhao Department of Electrical and Computer Engineering Cleveland State University
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:
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.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
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.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
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.
Database Programming in Java Corresponds with Chapter 32, 33.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Database Environment Chapter 2 AIT632 Sungchul Hong.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
CS 584 Lecture 18 l Assignment » Glenda assignment extended to the Java RMI Deadline » No Java RMI Assignment l Test » Friday, Saturday, Monday.
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.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
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.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
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.
Basics of JDBC Session 14.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
In the Name Of Almighty Allah. Java Application Connection To Mysql Created by Hasibullah (Sahibzada) Kabul Computer Science Faculty Afghanistan.
Java Object-Relational Layer Sharon Diskin GUS 3.0 Workshop June 18-21, 2002.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Using RMI The Example of A Remote Calculator 1. Parts of A Working RMI System A working RMI system is composed of several parts. –Interface definitions.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
Interacting with Database
JDBC 15-Apr-18.
Principles of Software Development
Java Remote Method Invocation (RMI)
JDBC 21-Aug-18.
JDBC.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC 15-Nov-18.
Interacting with Database
Using RMI -The Example of A Remote Calculator
JDBC Example.
CS 584 Lecture 18 Assignment Glenda assignment extended to the Java RMI Deadline No Java RMI Assignment Test Friday, Saturday, Monday.
JDBC – Java DataBase Connectivity
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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 JDBC/RMI example.

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

Sample Table

Attribute

Tuple

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

Select Statement We will use this data for our examples

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.

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

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

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

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

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.)

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

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

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.

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

Click on the desired driver (MSAccess) Click on the Finish button

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

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

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.

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 !=

JDBC and RMI Create an ODBC data source that will allow us to connect to an Access database (see above) Create a server class that registers an object with the rmi registry. The remote object must have an interface on the client and the server (needed to create the stub for marshalling) The client accesses the remote object and calls its methods. The remote methods access the database with SQL statements.

// On the server side DBServer.java import java.rmi.Naming; public class DBServer { public DBServer() { try { StockDB rc = new StockDBImpl(); Naming.rebind("stocks", rc); } catch(Exception e) { System.out.println("Trouble: " + e); } public static void main(String args[]) { new DBServer(); System.out.println("Server Active"); } Remote object Register With rmi registry

Provide an interface for the client and the server // On the client and the server StockDB.java //This is the interface to the remote object SockDBImpl import java.sql.*; public interface StockDB extends java.rmi.Remote { public String getCustomers() throws java.rmi.RemoteException; public String getStocks() throws java.rmi.RemoteException; public String getPortfolio() throws java.rmi.RemoteException; }

The remote object connects to JDBC // DBImpl2.java // The remote object import java.util.*; import java.sql.*; import java.io.*; public class StockDBImpl2 extends java.rmi.server.UnicastRemoteObject implements StockDB { private Connection con; private Statement s;

//The constructor runs the inherited constructor public StockDBImpl2() throws java.rmi.RemoteException { super(); } // This private method is called to connect to the database. private void connectToDB() throws SQLException { DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); con = DriverManager.getConnection("jdbc:odbc:stocks"); } // This private method is called to disconnect from the database. private void disconnectFromDB() throws SQLException{ con.close(); }

private String getXML(ResultSet rs, ResultSetMetaData rsm) throws SQLException { // Currently, this method only returns a non-xml String // representing the rows of the result set String answer; answer = ""; while(rs.next()) { for(int col = 1; col <= rsm.getColumnCount(); col++) { answer += rs.getString(col); } return answer; }

public String getCustomers() throws java.rmi.RemoteException { try { connectToDB(); Statement s = con.createStatement(); ResultSet rs; ResultSetMetaData rsm; rs = s.executeQuery("select * from customer"); rsm = rs.getMetaData(); String answer = getXML(rs,rsm); rs.close(); s.close(); disconnectFromDB(); return answer; } catch(SQLException sq) { System.out.println("SQLException: "+sq); } return null; }

public String getStocks() throws java.rmi.RemoteException { return null; } public String getPortfolio() throws java.rmi.RemoteException { return null; }

The client makes requests on the remote object // StockDBClient.java import java.io.*; import java.rmi.*; import java.net.*; import java.sql.*; public class StockDBClient {

public static StockDB stockDataBase; public static void main(String[] args) { try { stockDataBase = (StockDB)Naming.lookup( "rmi://mccarthy.heinz.cmu.edu/stocks"); String xml = stockDataBase.getCustomers(); System.out.println(xml); xml = stockDataBase.getPortfolio(); System.out.println(xml); xml = stockDataBase.getStocks(); System.out.println(xml); }

catch (MalformedURLException murle) { System.out.println(); System.out.println("MalformedURLException"); System.out.println(murle); } catch (RemoteException re) { System.out.println(); System.out.println("RemoteException"); System.out.println(re); } catch (NotBoundException nbe) { System.out.println(); System.out.println("NotBoundException"); System.out.println(nbe); }

catch (java.lang.ArithmeticException ae) { System.out.println(); System.out.println("ArithmeticException"); System.out.println(ae); } catch(IOException e) { System.out.println(); System.out.println("IOException"); System.out.println(e); }

Homework Database Schema There are three tables. Both customer and stocks have a one- to-many relationship with portfolios

Customer | id | lname | fname | | 1 | Jones | Robert | | 2 | Smith | Elaine | | 3 | Chan | Jane | | 4 | Morales | Hector | | 5 | Schwartz | Michael |

Stocks | symbol | company | price | | COMS | 3Com Corporation | | | IBM | International Business Machines | | | INTC | Intel Corporation | | | ORCL | Oracle Corporation | | | SE | 7Eleven Inc. | |

Portfolio | id | symbol | num_shares | | 1 | COMS | 1250 | | 1 | INTC | 300 | | 1 | ORCL | 450 | | 2 | COMS | 750 | | 2 | IBM | 900 |

| 2 | INTC | 600 | | 3 | COMS | 3232 | | 3 | IBM | 1000 | | 3 | SE | 200 | | 4 | COMS | 333 | | 4 | IBM | 250 | | 4 | INTC | 400 | | 5 | COMS | 750 | | 5 | IBM | 250 | | 5 | ORCL | 1000 | | 5 | SE | 900 |