Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
1 JDBC Java Database Connectivity. 2 c.pdf
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
JDBC CS 122. JDBC zJava Database Connectivity zDatabase Access Interface Õprovides access to a relational database (by allowing SQL statements to be sent.
Introduction to JDBC (Java Database Connectivity).
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Database Programming in Java Corresponds with Chapter 32, 33.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Session 30 Basics of JDBC. Java Simplified / Session 30 / 2 of 33 Review A Swing menu consists of a menubar, menuitems and menus. Trees are used to depict.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Basics of JDBC Session 14.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Interacting with Database
Lec - 14.
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
Database JDBC Overview CS Programming Languages for Web Applications
JDBC 21-Aug-18.
Introduction to Programming with Java
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

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) void setElementAt(Object elem, int index)

Vector Example Vector 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)); }

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)

Hashtable Example Hashtable 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”));

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()

StringTokenizer Example StringTokenizer 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()); }

Introduction to Databases and JDBC CS 21b

Crash Course in Databases (Relational) Database: set of tables represents information Table: columns (fields) and rows (values) SQL - Structured Query Language: facilitates manipulation of data in a database

Database Example PRODUCT CodeItemPrice BBBeyblade ZDZoid SFStickfas SALE NumCodeQuantity 001BB2 002ZD1 003BB5 004SF1

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

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)

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 Relational DB Management System Java Program - load driver - establish connection - send SQL statements

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)

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

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 driver String dbname = “jdbc:odbc:MyDB”; Connection c = Driver.getConnection(dbname);

Creating a Statement 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();

Methods of the Statement 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)

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

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 table myvar = rs.getInt(3);

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

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