20
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: http://homel.vsb.cz/~ciz034/cze/study.ht ml

Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Embed Size (px)

Citation preview

Page 1: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Helena Pomezná, ciz034

St. skupina: L392

FEI, VŠB-TUO

Ak. rok. 2002/2003

Download: http://homel.vsb.cz/~ciz034/cze/study.html

Page 2: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Some facts about JDBC

How to make our application

Page 3: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

What is the JDBC API?

JDBC = Java Database Connectivity

a Java API for accessing virtually any kind of tabular data

Consists of a set of classes and interfaces that provide a standard API for tool/database developers

Makes it easy to send SQL statements to relational database systems

Supports all dialects of SQL.

Page 4: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

The value of JDBC

Can access virtually any data source Can run on any platform with a JVM We don’t have to worry about writing different applications to run on different platforms

=> lets a programmer write once and run anywhere.

Page 5: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

What does the JDBC API do?

A JDBC technology-based driver = JDBC driver makes it possible to do 3 things:

Establish a connection with a data source

Send queries and update statements to the data source

Process the results.

Page 6: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

The JDBC versus ODBC

ODBC API (Open DataBase Connectivity) was the most widely used programming interface for accessing databases.

Page 7: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Why not use ODBC from Java?

You can use ODBC from Java, but this is best done with the help of the JDBC API in the form of the JDBC-ODBC Bridge.

ODBC is not appropriate for direct use from the Java because it uses a C interface.

A literal translation of the ODBC C API into a Java API would not be desirable.

ODBC is hard to learn.

Page 8: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Java Software Framework

Java Software provides 3 JDBC product components:

the JDBC driver manager (included in Java2 Platform)

the JDBC driver test suite(on JDBC web site)

the JDBC-ODBC bridge (included in the Solaris and Windows versions of the Java2 Platform)

Page 9: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

How to do it?

Installation

Setting Up a Database

Establishing a Connection

Creating Complete JDBC Applications

Page 10: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Installation

Install Java and JDBC on your machineYou will get JDBC when you download the JDK

Install driver on your machineJDBC driver

JDBC-ODBC Bridge driver

ODBC driver

Page 11: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Setting Up a Database

We will assume that the database already exists.

When you create the tables as examples below, they will be in the default database.

Page 12: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Establishing a ConnectionLoading Drivers

We want to use the JDBC-ODBC Bridge Class.forName(

“sun.jdbc.odbc.JdbcOdbcDriver”);

For instance, if the class name is jdbc.DriverXYZ

Class.forName(“jdbc.DriverXYZ”);

Page 13: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Establishing a Connection 2

Making the connection

Connection con = DriverManager.getConnection (url,

“myLogin”, “myPassword”);

Page 14: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Creating complete JDBC applications

Putting code in class definition

Importing classes to make them visible

import java.sql.*;

Using try and catch blocksSQLException

ClassNotFoundException

Using SQL

Page 15: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Using SQL

Creating a TableString createTableContact = “CREATE TABLE CONTACT“ +

“(FIRST VARCHAR(32), LAST VARCHAR(32), EMAIL VARCHAR(32))”;

Creating JDBC StatementStatement stmt = con.createStatement();

stmt.executeUpdate(createTableContact);

Page 16: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Using SQL - continuation

Entering data into a table

first line:stmt.executeUpdate(“INSERT INTO CONTACT “ + “VALUES

(‘Thomas’, ‘Willson’, ‘[email protected]’)”);

second line:stmt.executeUpdate(“INSERT INTO CONTACT “ + “VALUES

(‘Jane’, ‘Willson’, ‘[email protected]’)”);

Page 17: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Simple example

import java.sql.*;

Public class Lookup{public static void main(String[] args) throws SQLException, ClassNotFoundException{

String dbUrl = “jdbc:odbc:contact”;String user = “”;String pass = “”;

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);Connection con =

DriverManager.getConnection( dbUrl, user, pass);

Statement sta = con.createStatement();

Page 18: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Simple example - continuation

ResultSet res = sta.executeQuery( “SELECT FIRST, LAST, EMAIL “ + “FROM contact.csv contact” + “WHERE” + “(LAST = ‘ “ + args[0] + “,) “ + “ORDER BY FIRST”);

while(res.next()){

System.out.println(res.getString(“Last”) + “, “ + res.getString(fiRST”) + “: “ + res.getString(“EMAIL”));

}

sta.close();

}

Page 19: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Sources

• http://java.sun.com/docs/books/tutorial

- the Java Tutorial

• Thinking in Java 2nd Edition, Bruce Eckel

Page 20: Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download: ciz034/cze/study.html

Thank you