java utility classes

23
Java Utility Classes CS 21b

Upload: pippa

Post on 24-Jan-2016

63 views

Category:

Documents


0 download

DESCRIPTION

Java Utility Classes. CS 21b. Some Java Utility Classes. Vector Hashtable StringTokenizer * import java.util.*;. Vector. Indexed data structure that automatically resizes Selected Methods void addElement(Object o) int size() Object elementAt(int index) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Java Utility Classes

Java Utility Classes

CS 21b

Page 2: Java Utility Classes

Some Java Utility Classes Vector Hashtable StringTokenizer

* import java.util.*;

Page 3: Java Utility Classes

Vector Indexed data structure that

automatically resizes Selected Methods

void addElement(Object o) int size() Object elementAt(int index) void setElementAt(Object elem, int

index)

Page 4: Java Utility Classes

Vector ExampleVector names = new Vector();names.addElement(“Bart”);names.addElement(“Lisa”);names.addElement(“Maggie”);for (int i = 0; i < names.size(); i++){ System.out.println(names.elementAt(i));}names.setElementAt(“Homer”,1);names.addElement(“Marge”);for (int i = 0; i < names.size(); i++){ System.out.println(names.elementAt(i));}

Page 5: Java Utility Classes

Hashtable Data structure that associates

values with a key for efficient look up

Selected Methods void put(Object key, Object value) Object get(Object key) Object remove(Object key)

Page 6: Java Utility Classes

Hashtable ExampleHashtable grades = new Hashtable();

grades.put(“Martin”, “A”);

grades.put(“Nelson”, “F”);

grades.put(“Millhouse”, “C”);

System.out.println(grades.get(“Nelson”));

System.out.println(grades.get(“Martin”));

grades.put(“Nelson”, “W”);

grades.remove(“Martin”);

System.out.println(grades.get(“Nelson”));

System.out.println(grades.get(“Martin”));

Page 7: Java Utility Classes

StringTokenizer Breaks up a string into substrings

(tokens) separated by a specified delimiter

Selected Methods StringTokenizer(String str, String delim) int countTokens() String nextToken() boolean hasMoreTokens()

Page 8: Java Utility Classes

StringTokenizer ExampleStringTokenizer st;

String longstr = “This is the last slide”;

st = new StringTokenizer(longstr, “ ”);

int numwords = st.countTokens();

System.out.println(numwords);

String firstword = st.nextToken();

System.out.println(firstword);

while (st.hasMoreTokens())

{ System.out.println(st.nextToken());

}

Page 9: Java Utility Classes

Introduction to Databasesand JDBC

CS 21b

Page 10: Java Utility Classes

Crash Course inDatabases (Relational) Database: set of tables

represents information Table: columns (fields) and rows

(values) SQL - Structured Query Language:

facilitates manipulation of data in a database

Page 11: Java Utility Classes

Database Example

PRODUCTCode Item PriceBB Beyblade 150.00ZD Zoid 799.95SF Stickfas 500.00

SALENum Code Quantity001 BB 2002 ZD 1003 BB 5004 SF 1

Page 12: Java Utility Classes

SQL Statements To add a row:

INSERT INTO SALE VALUES(005,‘ZD’,2) To retrieve a row:

(e.g., product name given a code)SELECT ITEM FROM PRODUCT WHERE CODE =

‘BB’ To retrieve several rows:

(e.g., all details of all orders)SELECT * FROM SALE

Page 13: Java Utility Classes

JDBC Java Database Connectivity Database Access Interface

provides access to a relational database (by allowing SQL statements to be sent and executed through a Java program)

JDBC package: set of Java classes that facilitate this access (java.sql.*)

Comes with JDK (since 1.1)

Page 14: Java Utility Classes

JDBC Driver

Need a driver, specific to the DB product, to mediate between JDBC and the database the driver is a Java class that needs to

be loaded first

RelationalDB

ManagementSystem

Java Program- load driver- establish connection- send SQL statements

Page 15: Java Utility Classes

JDBC-ODBC Bridge Driver that interfaces with ODBC

(Object Database Connectivity--also an access interface)

Easiest way to access databases created by Microsoft products register database as an ODBC data

source use JDBC-ODBC bridge as the JDBC driver

(included in JDK 1.2 distribution)

Page 16: Java Utility Classes

Key Classes in JDBC Connection

need to create an instance of this class when establishing a connection to the database

Statement for issuing SQL statements

ResultSet (interface) a ResultSet object represents the table

returned by an SQL select statement

Page 17: Java Utility Classes

Establishing a Connection

Use the getConnection() method under the DriverManager class String argument: "jdbc:driver:name” returns a Connection object

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);// above line loads the jdbc-odbc driverString dbname = “jdbc:odbc:MyDB”;Connection c = Driver.getConnection(dbname);

Page 18: Java Utility Classes

Creating aStatement Object

Execute the createStatement() method on the Connection object returns a Statement object afterwards, run methods on the

Statement object to execute an SQL statement

Statement s = c.createStatement();

Page 19: Java Utility Classes

Methods of theStatement Class executeQuery()

requires a String argument (a select statement)

returns a ResultSet object executeUpdate()

requires a String argument (an insert, update, or delete statement)

returns an int (row count, in most cases)

Page 20: Java Utility Classes

The ResultSet Interface A ResultSet object represents the

table returned by the select statement sent

Navigation/retrieval methods next(): moves to the next row (first row

if called for the first time), returns false if no rows remain

getXXX methods return the value of a field for the current row

Page 21: Java Utility Classes

get Method Example: getInt() ResultSet rs;rs = s.executeQuery(“SELECT * FROM ORDER”);rs.next(); // gets the first row

// suppose the Orders table has an integer field// called quantity int myvar = rs.getInt(“Quantity”);// if you knew that quantity is the 3rd field in the

tablemyvar = rs.getInt(3);

Page 22: Java Utility Classes

Exercise Create a Microsoft Access Database

insert sample rows Add an ODBC data source

use the Microsoft Access driver associate with the created database

Create a Java program use JDBC-ODBC bridge create a loop that lists all rows of the

table

Page 23: Java Utility Classes

Summary JDBC allows you to write Java programs

that manipulate a database A driver (often a separate product) is

required that facilitates access Key classes: Connection, Statement,

and ResultSet Other features: metadata,

parameterized statements, and stored-proc invocation