Download presentation
Presentation is loading. Please wait.
Published byEstifanos Tilahun Mihret Modified over 5 years ago
1
Advanced Programming Estifanos T. (MSc in Computer Networking)
2
CHAPTER SIX Java Database Connectivity Estifanos T. (MSc in Computer Networking)
3
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
4
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
5
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
6
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
7
Structured Query Language 7 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking)
8
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
9
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
10
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
11
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
12
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 )
13
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
14
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
15
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
16
Architecture of JDBC 16 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking)
17
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
18
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
19
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
20
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");
21
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"); }
22
Close Connection 22 Lecture 6: Java Database Connectivity (JDBC) 9/10/2019 Estifanos T. (MSc in Computer Networking) s.close(); con.close();
23
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");
24
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); }}}
25
End Of Chapter Six
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.