Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

JDBC – Java DataBase Connectivity CSE432 Object Oriented Software Engineering.
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.
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.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
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.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
Database Programming in Java Corresponds with Chapter 32, 33.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Database: SQL and MySQL
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
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.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
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.
Basics of JDBC Session 14.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC – Java DataBase Connectivity
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Fundamentals of DBMS Notes-1.
Lec - 14.
JDBC 15-Apr-18.
Chapter 5 Introduction to SQL.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
 2012 Pearson Education, Inc. All rights reserved.
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
JDBC.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC – Java DataBase Connectivity
Client Access, Queries, Stored Procedures, JDBC
JDBC – Java DataBase Connectivity
JDBC 15-Nov-18.
Chapter 8 Working with Databases and MySQL
Chapter 22 - SQL, MySQL, DBI and ADO
JDBC – ODBC DRIVERS.
Java Database Connectivity
Lecture 11 Database Connection
JDBC – Java DataBase Connectivity
Presentation transcript:

Advanced Programming Estifanos T. (MSc in Computer Networking)

CHAPTER SIX Java Database Connectivity Estifanos T. (MSc in Computer Networking)

Objectives 3 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) To overview SQL in Java Installing and Setting up JDBC To describe the Basic JDBC Programming Concepts To demonstrate How to Executing Queries

Introduction 4 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) A database is an organized collection of data. There are many different strategies for organizing data to facilitate easy access and manipulation A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data for many users Database management systems allow for the access and storage of data without concern for the internal representation of data

Introduction 5 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) Today’s most popular database systems are relational databases A language called SQL - pronounced “sequel,” or as its individual letters - is the international standard language used almost universally with relational databases to perform queries (i.e., to request information that satisfies given criteria) and to manipulate data

Introduction 6 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) A relational database is a logical representation of data that allows the data to be accessed without consideration of its physical structure A relational database stores data in tables

Structured Query Language 7 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking)

Basic SELECT Query 8 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) A SQL query “selects” rows and columns from one or more tables in a database Such selections are performed by queries with the SELECT keyword The basic form of a SELECT query is SELECT * FROM tableName

WHERE Clause 9 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) In most cases, it’s necessary to locate rows in a database that satisfy certain selection criteria Only rows that satisfy the selection criteria (formally called predicates) are selected SQL uses the optional WHERE clause in a query to specify the selection criteria for the query The basic form of a query with selection criteria is SELECT columnName1, columnName2, … FROM tableName WHERE criteria

ORDER BY Clause 10 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) The rows in the result of a query can be sorted into ascending or descending order by using the optional ORDER BY clause The basic form of a query with an ORDER BY clause is SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC SELECT columnName1, columnName2,…FROM tableName ORDER BY column DESC

INNER JOIN 11 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) Often, it’s necessary to merge data from multiple tables into a single result Referred to as joining the tables, this is specified by an INNER JOIN operator, which merges rows from two tables by matching values in columns that are common to the tables The basic form of an INNER JOIN is SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1.columnName = table2.columnName

INSERT Statement 12 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) The INSERT statement inserts a row into a table The basic form of this statement is INSERT INTO tableName ( columnName1, columnName2, …, columnNameN ) VALUES ( value1, value2, …, valueN )

UPDATE Statement 13 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) An UPDATE statement modifies data in a table Its basic form is UPDATE tableName SET columnName1 = value1, columnName2 = value2, …, columnNameN = valueN WHERE criteria

DELETE Statement 14 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) A SQL DELETE statement removes rows from a table Its basic form is DELETE FROM tableName WHERE criteria

JDBC 15 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) It is an API that lets you access virtually any tabular data source from the Java programming language It can access virtually any data source, from relational databases to spreadsheets and flat files

Architecture of JDBC 16 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking)

Basic Steps to Use a Database in Java 17 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) Establish a connection Create JDBC Statements Execute SQL Statements GET ResultSet Close connections

Establish a Connection 18 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) import java.sql.*; Load the vendor specific driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Make the connection Connection con = DriverManager.getConnection( database,"",""); Establishes connection to database by obtaining a Connection object

Create JDBC Statement 19 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) Statement s = con.createStatement() ; Creates a Statement object for sending SQL statements to the database

Executing SQL Statements 20 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) Statement s = con.createStatement() ; s.execute("create table TEST12345 ( firstcolumn integer )"); s.execute("insert into TEST12345 values(1)"); s.execute("select firstcolumn from TEST12345");

Get ResultSet 21 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) ResultSet rs = s.getResultSet(); while (rs.next()) { int ssn = rs.getInt("SSN"); String name = rs.getString("NAME"); int marks = rs.getInt("MARKS"); }

Close Connection 22 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) s.close(); con.close();

Sample Demo 23 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) import java.sql.*; class Test { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //dynamic loading of driver String filename = "c:/db1.mdb"; //Location of an Access database String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; database+= filename.trim() + ";DriverID=22;READONLY=true}"; //add on to end Connection con = DriverManager.getConnection( database,"",""); Statement s = con.createStatement(); s.execute("create table TEST12345 ( firstcolumn integer )"); s.execute("insert into TEST12345 values(1)"); s.execute("select firstcolumn from TEST12345");

Sample Demo 24 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) ResultSet rs = s.getResultSet(); if (rs != null) // if rs == null, then there is no ResultSet to view while ( rs.next() ) // this will step through our data row-by-row { /* the next line will get the first column in our current row's ResultSet as a String ( getString( columnNumber) ) and output it to the screen */ System.out.println("Data from column_name: " + rs.getString(1) ); } s.close(); // close Statement to let the database know we're done with it con.close(); //close connection } catch (Exception err) { System.out.println("ERROR: " + err); }}}

End Of Chapter Six