JDBC I IS 313 1.21.2003. Why do we have databases?

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
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.
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 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.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
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. –
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
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.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Database Programming in Java Corresponds with Chapter 32, 33.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
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.
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.
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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
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 Establish a connection with a database or access any tabular data source Send SQL statements Process the results Two major sets of interfaces: JDBC.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
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.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
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.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
EXAMPLE I An application showing JDBC access to Cloudscape.
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.)
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.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Web Programming Assistant Professor Xiaozhong Liu
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
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.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
JSP and DB.
CompSci 280 S Introduction to Software Development
Lec - 14.
JDBC III IS
Advanced Web Automation Using Selenium
JDBC.
Client Access, Queries, Stored Procedures, JDBC
Introduction to Server-Side Web Development using JSP and Databases
Objectives In this lesson, you will learn about:
Interacting with Database
Using a Database with JDBC
JDBC API.
JDBC Example.
JDBC II IS
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

JDBC I IS

Why do we have databases?

Why not just use files?

Four perspectives on the database  Large data store  Persistent data store  Query service  Transaction service

Programmer’s view  Not how it works how to administer it how to design a database  Database services persistent storage sophisticated querying transactions

JDBC  Group of Java classes  Correspond to basic database concepts Connecting to the database Issuing a query Examining results

Higher-level option  Enterprise JavaBeans simply state that an object is going to be persistent the EJB “container” uses JDBC to save the object in a database SE 554

Database table 1103PaulMcCartney JohnLennon AaronPresley ElvisPresley FredFlintstone SuzyQ RobinBurke12341 Fuel Option DaysCar type First name Last nameIDRow #

Key relationships 175FredFlintstone SuzyQ RobinBurke1234 Fuel Option DaysCar type First name Last nameID SUV5 Luxury4 Full size3 Mid-size2 Compact1 Subcompact0 DescriptionID

Basic terms  Table basic unit of organization  Row / Record single “entry”  Column named attributed of a row  View a dynamically created table  Query an operation on the database typically retrieval of a view  Record set / result set / row set the information returned by a query

SQL  Declarative language not procedural  Describe what you want database figures out “how”  In general do as much in SQL as you can

Query SELECT LastName, FirstName FROM Reservations

Sorting SELECT * FROM Reservations ORDER BY Reservations.LastName;

Choosing SELECT * FROM Reservations WHERE (LastName = ‘Presley’) AND (CarType = 1)

SQL Language  Case insensitive  Strings enclosed in single quotes  Whitespace ignored  Commands for (creating and structuring databases) retrieving data inserting rows deleting rows  Different versions SQL-92 most widely supported

SELECT statement SELECT { columns } FROM { table(s) } WHERE { criteria }... other options... ;

Whole table SELECT * FROM Reservations

Simple criteria SELECT * FROM Reservations WHERE (LastName = ‘Presley’) AND (CarType = 1)

Complex criteria SELECT titles.type, titles.title FROM titles t1 WHERE titles.price > (SELECT AVG(t2.price) FROM titles t2 WHERE t1.type = t2.type)

Note  Dot syntax for columns table.column titles.type  Table aliases table alias titles t1

Join SELECT Reservations.ID, Reservations.LastName, Reservations.FirstName, CarType.Description, Reservation.Days, FuelOption.Description FROM Reservations, CarType, FuelOption WHERE Reservations.CarType = CarType.ID AND Reservations.FuelOption = FuelOption.ID

Join example 175RobinBurke1234 Fuel Option DaysCar type First name Last nameID Pre-paid fuel1 DescriptionID SUV5 DescriptionID Pre-paid fuel7SUVRobinBurke1234 F.DescriptionR.DaysC.DescriptionR.FirstNameR.LastNameR.ID

Join example cont’d  What if? SUV5 DescriptionID Sport Ut.5

JDBC

 DriverManager knows how to connect to differents DBs  Connection represents a connection to a particular DB  Statement a query or other request made to a DB  ResultSet results returned from a query

Loading the driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  What is this?

Creating a connection Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password");  Why not “ new Connection () ”?

Property file jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver jdbc.url=jdbc:odbc:rentalcars jdbc.username=PUBLIC jdbc.password=PUBLIC

Using properties public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); String fileName = “rentalcars.properties"; FileInputStream in = new FileInputStream(fileName); props.load(in); String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password); }

Statement Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Statement stmt = conn.createStatement();

Executing a statement Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Statement stmt = conn.createStatement(); String sql = “SELECT * FROM Reservations WHERE (LastName = ‘Burke’);”; stmt.execute (sql);

ResultSet Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Statement stmt = conn.createStatement(); String sql = “SELECT * FROM Reservations WHERE (LastName = ‘Burke’);”; ResultSet rs = stmt.executeQuery (sql); rs.next(); int carType = rs.getInt (“CarType”);

Iteration using ResultSet int totalDays = 0; String sql = “SELECT * FROM Reservations;”; ResultSet rs = stmt.executeQuery (sql); while (rs.next()) { totalDays += rs.getInt (“Days”); }

Better solution int totalDays = 0; String sql = “SELECT SUM(Days) FROM Reservations;”; ResultSet rs = stmt.executeQuery (sql); rs.next(); totalDays = rs.getInt (1);

Example  Connection  Statement  ResultSet  Properties / property file