Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL (Structured Query Language) 4.Methods in packages java.sql & javax.sql
Intro to JDBC Popular Relational Databases: Oracle Microsoft SQL Server Sybase Informix Microsoft Access mySQL CloudScape … and many, many more! RDBMS (p.1)
Intro to JDBC A Database consists of: Tables, which store… records, which contain fields of information. Relational Databases: store data in multiple tables that can be related (or linked) to one another via key fields. RDBMS (p.2)
Intro to JDBC An example: let’s look at the Microsoft Access database we just used. RDBMS (p.3) NumberNameDepartmentSalaryLocation 23603Jones New Jersey 24568Kerwin New Jersey 34589Larson Los Angeles 35761Myers Orlando 47132Neumann New Jersey 78321Stephens Orlando Row ColumnPrimary key
Intro to JDBC RDBMS (p.4) A Sample Relational Database:
Intro to JDBC RDBMS (p.5) In this example, a one-to-many relationship exists between the Contacts table and the Calls table. Contacts.ContactID is the primary key; Calls.ContactID is the foreign key. Primary and foreign keys need not have the same name, but must be of the same data type.
Intro to JDBC JDBC Drivers (p.1)
Intro to JDBC JDBC Drivers (p.2) Sun’s JDBC-ODBC Bridge – a type 1 driver Tips: - Always use System DSN, so DSN is available to all - ODBC Data Sources are only available on LAN/WAN (Not Internet accessible) - Use a Java/NativeAPI driver to access databases via internet - More info: JDBC-ODBC Bridge DriverJDBC-ODBC Bridge Driver
Intro to JDBC JDBC Drivers (p.3) More drivers available at: Lab: Find a type 2, 3 or 4 driver to use with Microsoft Access Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverXYZ, you would load the driver with the following line of code: Class.forName("jdbc.DriverXYZ"); Another driver lab: Set up distributed DB at \\B122-1
Intro to JDBC SQL (Structured Query Language) (p.1) SQL is an industry standard for querying relational databases. Although SQL, like everything else in the software industry, exists in several versions and flavors, basic syntax is consistent and reliable across all systems and software. Database languages, such as SQL, ordinarily include two sublanguages: Data Definition Language (DDL) Data Manipulation Language (DML)
Intro to JDBC SQL (p.2) Data Definition Language (DDL): Enables developer to create and modify database architecture (tables, records, fields). Query keywords: CREATE TABLE ALTER TABLE DROP TABLE
Intro to JDBC SQL (p.3) Data Manipulation Language (DML): Enables developer to add, edit and delete records and retrieve data. Query keywords: SELECT – retrieves data from table(s) INSERT – populates table with data UPDATE – changes existing data in table DELETE – removes data from table
Intro to JDBC SQL (p.4) SELECT statement clauses: SELECT – Select and retrieve records FROM – Tables from which to get fields WHERE – Criteria for filtering selections GROUP BY – How to group found records HAVING – Used with GROUP BY to specify criteria ORDER BY – Criteria for ordering found records
Intro to JDBC SQL (p.5) SELECT statement syntax: Basic SELECT Query : SELECT FieldName, … FROM TableName, … Examples: SELECT * FROM Authors SELECT AuthorID, LastName FROM Authors
Intro to JDBC SQL (p.6) SELECT statement syntax: SELECT Query with WHERE clause: SELECT FieldName, … FROM TableName, … WHERE criteria Examples: SELECT * FROM Authors WHERE YearBorn > 1960 SELECT * FROM Authors WHERE LastName = ‘Dietel’ (WHERE operators:, =, =,, LIKE ( [?, *] )
Intro to JDBC SQL (p.7) SELECT statement syntax: ORDER BY Clause: SELECT FieldName, … FROM TableName, … WHERE criteria ORDER BY FieldName, … ASC | DESC Example: SELECT * FROM Authors ORDER BY LastName, FirstName DESC
Intro to JDBC SQL (p.8) SELECT statement syntax: WHERE with LIKE clause: SELECT * FROM Authors WHERE lastName LIKE ‘D*’ [Or, use ‘D%’ other DBs] LIKE clause wildcard operators: ? * [Or, _ % other DBs]
Intro to JDBC SQL (p.9) SELECT statement syntax: WHERE with LIKE clause: SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘?i*’ [Or, use ‘_i%’ other DBs] LIKE clause wildcard operators: ? * [Or, _ % other DBs]
Intro to JDBC SQL (p.10) SELECT statement syntax: A More Complex Example Using an Implied Join: SELECT PublisherName, Title FROM Publishers, Titles WHERE (Titles.PublisherID = Publishers.PublisherID) ORDER BY PublisherName, Title ASC OR SELECT Publishers.PublisherName, Titles.Title FROM Publishers, Titles WHERE (Titles.PublisherID = Publishers.PublisherID) ORDER BY Publishers.PublisherName, Titles.Title ASC
Intro to JDBC SQL (p.11) INSERT statement syntax: Basic INSERT Query : INSERT INTO tableName ( columnName1,.., columnNameN ) VALUES ( value1, …, valueN ) Example: INSERT INTO authors ( firstName, lastName ) VALUES ( ‘Sue’, ‘Smith’ )
Intro to JDBC SQL (p.12) UPDATE statement syntax: Basic UPDATE Query : UPDATE tableName SET columnName1 = value1, …, columnNameN = valueN WHERE criteria Example: UPDATE authors SET lastName = ‘Jones’ WHERE lastName = ‘Smith’ AND firstName = ‘Sue’
Intro to JDBC SQL (p.13) DELETE statement syntax: Basic DELETE Query : DELETE FROM tableName WHERE criteria Example: DELETE FROM authors WHERE lastName = ‘Jones’ AND firstName = ‘Sue’
Intro to JDBC Package java.sql (p.1) Result Sets ResultSet (Recordset in ASP): JDBC returns query results in a ResultSet Object ResultSets are retrieved by executing queries: resultSet = statement.executeQuery( query );
Intro to JDBC Package java.sql (p.2) Traversing Result Sets A ResultSet can be traversed forward and backward (depending on cursor type) using: resultSet.first(); resultSet.next(); resultSet.previous(); resultSet.last(); … and more!
Intro to JDBC Package java.sql (p.3) Data types for Result Sets Data can be retrieved from a ResultSet by using various getXXX methods: resultSet.getString(FieldName); // returns data as type resultSet.getFloat(FieldName); resultSet.getDate(FieldName); resultSet.getInt(FieldName); resultSet.getMetaData(); // returns ResultSetMetaData obj. … and more!
Intro to JDBC Package java.sql (p.4) Result Set Cursors