Presentation is loading. Please wait.

Presentation is loading. Please wait.

1302383 Web Programming MySql JDBC Web Programming.

Similar presentations


Presentation on theme: "1302383 Web Programming MySql JDBC Web Programming."— Presentation transcript:

1 1302383 Web Programming MySql JDBC Web Programming

2 Course Content  SQL Command  JDBC Statement Web Programming

3 ::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  http://industry.java.sun.com/products/j dbc/drivers http://industry.java.sun.com/products/j dbc/drivers Web Programming

4 ::Database URL  Used to make a connection to the database  Can contain server, port, protocol etc…  jdbc:subprotocol_name:driver_dependan t_databasename  Oracle thin driver jdbc:oracle:thin:@machinename:1521:dbname  Derby jdbc:derby://localhost:1527/sample  Pointbase jdbc:pointbase:server://localhost/sample Web Programming

5 :Steps of Using JDBC 1. Load DB-specific JDBC driver 2. Get a Connection object 3. Get a Statement object 4. Execute queries and/or updates 5. Read results 6. Read Meta-data (optional step) 7. Close Statement and Connection objects Web Programming

6 ::1. Load DB-Specific Database Driver  To manually load the database driver and register it with the DriverManager, load its class file  Class.forName( ) Web Programming

7 ::2. Get a Connection Object  DriverManager 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

8 ::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

9 ::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 queries Web Programming

10 ::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

11 ::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

12 ::5. Reading Results (Continued)  Once you have the ResultSet, you can easily retrieve the data by looping through it Web Programming

13 ::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

14 ::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

15 :::ResultSetMetaData Examp Web Programming

16 :::CREATE TABLE Web Programming

17 ::: DROP TABLE Web Programming

18 ::SELECT Statement distinct keyword eliminates duplicates Web Programming

19 :::SELECT Statement Select * from customer_tbl; Web Programming

20 :::FROM Clause  Comma delimited list of tables to retrieve data from  With or without aliases Web Programming

21 :::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

22 :::WHERE Clause (Continued)  Not negates any Boolean expressions and keywords such as like, null, between and in Web Programming

23 :::Example: WHERE Clause Web Programming

24 :::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

25 :::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

26 ::::Example: GROUP BY Clause Web Programming

27 :::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

28 ::::Example: HAVING Clause Web Programming

29 :::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

30 :::ORDER BY Clause Example Web Programming

31 ::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

32 :::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

33 :::Example: Join Statement Web Programming

34 :::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

35 ::::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 JOIN Web Programming

36 ::Alias  Use of temporary names for tables within a query  Can be used anywhere in query  Reduces amount of typing Web Programming

37 :::Example: Alias Web Programming

38 :::Example: Alias cont. Web Programming

39 ::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

40 :::Example: Sub-Query Web Programming

41 ::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 EXISTS Web Programming

42 :::EXISTS cont. Web Programming

43 ::Union, Intersection and Difference  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

44 :::Union cont.  Difference is table containing all rows in A but not in B.  Two tables must be union compatible. Web Programming

45 ::Comparison Operators Web Programming

46 :::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

47 :::Numerical Expressions  Numerical expressions can be used in any numeric column or in any clause that allows an expression Web Programming

48 ::Aggregate Functions  Aggregates ignore null values (except count(*))  sum and avg only work with numeric values Web Programming

49 :::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

50 ::NULL Values  A 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

51 ::INSERT Web Programming

52 :::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

53 :::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

54 ::::INSERT Examples Web Programming

55 ::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

56 ::::Update Example Web Programming

57 ::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

58 :::DELETE examples Web Programming


Download ppt "1302383 Web Programming MySql JDBC Web Programming."

Similar presentations


Ads by Google