+ All Categories
Home > Documents > 1302383 Web Programming MySql JDBC Web Programming.

1302383 Web Programming MySql JDBC Web Programming.

Date post: 06-Jan-2018
Category:
Upload: miles-gibson
View: 251 times
Download: 9 times
Share this document with a friend
Description:
::JDBC Driver  Database specific implemention of JDBC interfaces  Every database server has corresponding JDBC driver(s)  You can see the list of available drivers from  dbc/drivers dbc/drivers Web Programming
58
1302383 Web Programming MySql JDBC Web Programming
Transcript
Page 1: 1302383 Web Programming MySql JDBC Web Programming.

1302383Web Programming

MySql JDBC

Web Programming

Page 2: 1302383 Web Programming MySql JDBC Web Programming.

Course Content SQL Command JDBC Statement

Web Programming

Page 3: 1302383 Web Programming MySql JDBC Web Programming.

::JDBC DriverDatabase specific implemention of

JDBC interfaces Every database server has

corresponding JDBC driver(s)You can see the list of available

drivers from http://industry.java.sun.com/products/j

dbc/driversWeb Programming

Page 4: 1302383 Web Programming MySql JDBC Web Programming.

::Database URL Used to make a connection to the database

Can contain server, port, protocol etc… jdbc:subprotocol_name:driver_dependant_d

atabasename Oracle thin driver jdbc:oracle:thin:@machinename:1521:dbname Derby jdbc:derby://localhost:1527/sample Pointbase jdbc:pointbase:server://localhost/sample

Web Programming

Page 5: 1302383 Web Programming MySql JDBC Web Programming.

:Steps of Using JDBC1. Load DB-specific JDBC driver2. Get a Connection object3. Get a Statement object4. Execute queries and/or updates5. Read results6. Read Meta-data (optional step)7. Close Statement and Connection

objectsWeb Programming

Page 6: 1302383 Web Programming MySql JDBC Web Programming.

::1. Load DB-Specific DatabaseDriver To manually load the database driver

and register it with the DriverManager, load its class file Class.forName(<database-driver>)

Web Programming

Page 7: 1302383 Web Programming MySql JDBC Web Programming.

::2. Get a Connection ObjectDriverManager class is responsible

for selecting the database and and creating the database connection Using DataSource is a prefered means

of getting a conection object (we will talk about this later)

Create the database connection as follows:

Web Programming

Page 8: 1302383 Web Programming MySql JDBC Web Programming.

::DriverManager & Connection java.sql.DriverManager

getConnection(String url, String user, String password) throws SQLException

java.sql.Connection Statement createStatement() throws

SQLException void close() throws SQLException void setAutoCommit(boolean b) throws

SQLException void commit() throws SQLException void rollback() throws SQLException

Web Programming

Page 9: 1302383 Web Programming MySql JDBC Web Programming.

::3. Get a Statement Object• Create a Statement Object from Connection object

– java.sql.Statement• ResultSet executeQuery(string sql)• int executeUpdate(String sql)

• Example: Statement statement =

connection.createStatement();• The same Statement object can be used for many,

unrelated queriesWeb Programming

Page 10: 1302383 Web Programming MySql JDBC Web Programming.

::4. Executing Query or Update

From the Statement object, the 2 most used commands are (a) QUERY (SELECT)

ResultSet rs = statement.executeQuery("select * from customer_tbl");

(b) ACTION COMMAND (UPDATE/DELETE) int iReturnValue = statement.executeUpdate("update

manufacture_tbl set name = ‘IBM' where mfr_num = 19985678");

Web Programming

Page 11: 1302383 Web Programming MySql JDBC Web Programming.

::5. Reading Results• Loop through ResultSet retrieving information

– java.sql.ResultSet• boolean next()• xxx getXxx(int columnNumber)• xxx getXxx(String columnName)• void close()

• The iterator is initialized to a position before the first row– You must call next() once to move it to the first

row

Web Programming

Page 12: 1302383 Web Programming MySql JDBC Web Programming.

::5. Reading Results (Continued) Once you have the ResultSet, you can

easily retrieve the data by looping through it

Web Programming

Page 13: 1302383 Web Programming MySql JDBC Web Programming.

::5. Reading Results (Continued) When retrieving data from the ResultSet,

use the appropriate getXXX() method getString() getInt() getDouble() getObject()

There is an appropriate getXXX method of each java.sql.Types datatype

Web Programming

Page 14: 1302383 Web Programming MySql JDBC Web Programming.

::6. Read ResultSet MetaData Once you have the ResultSet or Connection

objects, you can obtain the Meta Data about the database or the query

This gives valuable information about the data that you are retrieving or the database that you are using ResultSetMetaData rsMeta = rs.getMetaData(); DatabaseMetaData dbmetadata =

connection.getMetaData(); There are approximately 150 methods in the

DatabaseMetaData class.

Web Programming

Page 15: 1302383 Web Programming MySql JDBC Web Programming.

:::ResultSetMetaData Examp

Web Programming

Page 16: 1302383 Web Programming MySql JDBC Web Programming.

:::CREATE TABLE

Web Programming

Page 17: 1302383 Web Programming MySql JDBC Web Programming.

::: DROP TABLE

Web Programming

Page 18: 1302383 Web Programming MySql JDBC Web Programming.

::SELECT Statement

• distinct keyword eliminates duplicatesWeb Programming

Page 19: 1302383 Web Programming MySql JDBC Web Programming.

:::SELECT StatementSelect * from customer_tbl;

Web Programming

Page 20: 1302383 Web Programming MySql JDBC Web Programming.

:::FROM Clause Comma delimited list of tables to

retrieve data from With or without aliases

Web Programming

Page 21: 1302383 Web Programming MySql JDBC Web Programming.

:::WHERE Clause Determines exactly which rows are

retrieved Qualifications in the where clause

Comparison operators (=,>,<, >=,<=) Ranges (between and not between) Character matches (like and not like) Unknown values (is null and is not null) Lists (in and not in) Combinations of the above (and, or)

Web Programming

Page 22: 1302383 Web Programming MySql JDBC Web Programming.

:::WHERE Clause (Continued) Not negates any Boolean expressions

and keywords such as like, null, between and in

Web Programming

Page 23: 1302383 Web Programming MySql JDBC Web Programming.

:::Example: WHERE Clause

Web Programming

Page 24: 1302383 Web Programming MySql JDBC Web Programming.

:::GROUP BY Clause• The group by function organises data into

groups based on the contents of a column(s)– Usually used with an aggregate function in the

select list– The aggregate is calculated for each group– All null values in the group by column are

treated as one group• Grouping can be done by a column_name or

by any expression that does not contain an aggregate function

Web Programming

Page 25: 1302383 Web Programming MySql JDBC Web Programming.

:::GROUP BY Clause Cont. The group by function usually contains

all columns and expressions in the select list that are not aggregates

In conjunction with the where clause, rows are eliminated before they go into groups

Applies a condition on a table before the groups are formed

Web Programming

Page 26: 1302383 Web Programming MySql JDBC Web Programming.

::::Example: GROUP BY Clause

Web Programming

Page 27: 1302383 Web Programming MySql JDBC Web Programming.

:::HAVING Clause Set conditions on groups

Restricts groups Applies a condition to the groups after

they have been formed having is usually used in conjunction

with an aggregate function

Web Programming

Page 28: 1302383 Web Programming MySql JDBC Web Programming.

::::Example: HAVING Clause

Web Programming

Page 29: 1302383 Web Programming MySql JDBC Web Programming.

:::ORDER BY Clause The order by clause sorts the query

results (in ascending order by default) Items named in an order by clause need

not appear in the select list When using order by, nulls are listed

first

Web Programming

Page 30: 1302383 Web Programming MySql JDBC Web Programming.

:::ORDER BY Clause Example

Web Programming

Page 31: 1302383 Web Programming MySql JDBC Web Programming.

::Join Statement Retrieves data from two or more tables Combines tables by matching values

from rows in each table Joins are done in the where clause Columns in join don't have to be in

select

Web Programming

Page 32: 1302383 Web Programming MySql JDBC Web Programming.

:::Join Statement (Continued) If you do not specify how to join rows from

different tables, the database server assumes you want to join every row in each table Cartesian Product

Very costly (cpu time) May take a while to return large result set

Column names that appear in more then one table should be prefixed by table name

Web Programming

Page 33: 1302383 Web Programming MySql JDBC Web Programming.

:::Example: Join Statement

Web Programming

Page 34: 1302383 Web Programming MySql JDBC Web Programming.

:::Outer Joins With a join, if one row of a table is

unmatched, row is omitted from result table.

The outer join operations retain rows that do not satisfy the join condition List branches and properties that are in

same city along with any unmatched branches.

Web Programming

Page 35: 1302383 Web Programming MySql JDBC Web Programming.

::::Outer Join cont.

There is a LEFT JOIN and a RIGHT JOIN These can be very vendor specific (check with your vendor

for outer join specifications) Oracle uses (+) for outer joins, MS SQL Server uses LEFT

OUTER JOINWeb Programming

Page 36: 1302383 Web Programming MySql JDBC Web Programming.

::Alias Use of temporary names for tables

within a query Can be used anywhere in query Reduces amount of typing

Web Programming

Page 37: 1302383 Web Programming MySql JDBC Web Programming.

:::Example: Alias

Web Programming

Page 38: 1302383 Web Programming MySql JDBC Web Programming.

:::Example: Alias cont.

Web Programming

Page 39: 1302383 Web Programming MySql JDBC Web Programming.

::Sub-Queries A select statement, used as an expression in part

of another select, update, insert or delete The sub-query is resolved first and the results

are substituted into the outer query's where clause Used because it can be quicker to understand than

a join Perform otherwise impossible tasks (i.e. using an

aggregate)

Web Programming

Page 40: 1302383 Web Programming MySql JDBC Web Programming.

:::Example: Sub-Query

Web Programming

Page 41: 1302383 Web Programming MySql JDBC Web Programming.

::EXISTS and NOT EXISTS EXISTS and NOT EXISTS are for use only

with sub-queries. They produce a simple true/false result EXISTS is true if and only if there exists at

least one row in result table returned by sub-query

It is false if sub-query returns an empty result table

NOT EXISTS is the opposite of EXISTSWeb Programming

Page 42: 1302383 Web Programming MySql JDBC Web Programming.

:::EXISTS cont.

Web Programming

Page 43: 1302383 Web Programming MySql JDBC Web Programming.

::Union, Intersection andDifference Can use normal set operations of union,

intersection, and difference to combine results of two or more queries into a single result table.

Union of two tables, A and B, is table containing all rows in either A or B or both.

Intersection is table containing all rows common to both A and B.

Web Programming

Page 44: 1302383 Web Programming MySql JDBC Web Programming.

:::Union cont. Difference is table containing all rows in

A but not in B. Two tables must be union compatible.

Web Programming

Page 45: 1302383 Web Programming MySql JDBC Web Programming.

::Comparison Operators

Web Programming

Page 46: 1302383 Web Programming MySql JDBC Web Programming.

:::Character Matches Use to select rows containing field that

match specified portions of characterstring Use with character data only Can use wildcards

Enclose wildcards and character strings in quotes

Web Programming

Page 47: 1302383 Web Programming MySql JDBC Web Programming.

:::Numerical Expressions Numerical expressions can be used in

any numeric column or in any clause that allows an expression

Web Programming

Page 48: 1302383 Web Programming MySql JDBC Web Programming.

::Aggregate Functions

Aggregates ignore null values (except count(*)) sum and avg only work with numeric

valuesWeb Programming

Page 49: 1302383 Web Programming MySql JDBC Web Programming.

:::Aggregate Functions Cont. If a group by clause is not used, only

one row is returned Aggregates may not be used in a where

clause Aggregates can be applied to all rows in a

table or to a subset of a table Distinct keyword eliminates duplicate

values before applying the aggregate function

Web Programming

Page 50: 1302383 Web Programming MySql JDBC Web Programming.

::NULL ValuesA null value is an unknown value

null is a special value which means “no information available”

is null should be used to match columns contains null values

“= null” can also be used, but is not recommended One null value is never equal to another null value null's sort and group together Some columns are defined to permit null values

Web Programming

Page 51: 1302383 Web Programming MySql JDBC Web Programming.

::INSERT

Web Programming

Page 52: 1302383 Web Programming MySql JDBC Web Programming.

:::INSERT cont The column list is optional. If omitted,

SQL assumes a list of all columns in their original CREATE TABLE order.

Any columns omitted must have been declared as NULL when table was created, unless DEFAULT was specified when creating column.

Web Programming

Page 53: 1302383 Web Programming MySql JDBC Web Programming.

:::INSERT cont. Data value list must match column list

as follows: Number of items in each list must be the

same. Must be direct correspondence in position

of items in two lists. Data type of each item in data_value_list

must be compatible with data type of corresponding column.

Web Programming

Page 54: 1302383 Web Programming MySql JDBC Web Programming.

::::INSERT Examples

Web Programming

Page 55: 1302383 Web Programming MySql JDBC Web Programming.

::UPDATE

• SET clause specifies names of one or more columns that are to be updated.

• Where clause is optional. If omitted, named columns are updated for all rows in table.

Web Programming

Page 56: 1302383 Web Programming MySql JDBC Web Programming.

::::Update Example

Web Programming

Page 57: 1302383 Web Programming MySql JDBC Web Programming.

::DELETE

where is optional; if omitted, all rows are deleted from table. This does not delete table. If the where is specified, only those rows that satisfy condition are deleted.

Web Programming

Page 58: 1302383 Web Programming MySql JDBC Web Programming.

:::DELETE examples

Web Programming


Recommended