Top Banner
JDBC Java Database Connectivity
40

JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Dec 26, 2015

Download

Documents

Arron Neal
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

JDBC

Java Database Connectivity

Page 2: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

What is an RDBMS?

• Relational database management system.

• There are other kinds of DBMS.

• Access is a GUI on a JET RBDMS backend.

• Language for RBMS - > SQL

Page 3: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

SQL

• Structured Query Language

• There are many standards for SQL

• SQL92, SQL 89, SQL99,

• Some commands exists for all the standards.

• Some commands exists only for specific products

Page 4: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Why do I need JDBC?

• Connects to a RDBMS

• RDBMS Neutral

• Standard API (portable)

• Allows for transmission of SQL

• Can help to process results.

Page 5: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Using JDBC and Tiers

• Java program calls JDBC methods

• These methods transmit SQL statements

• The RDBMS responds, via the JDBC methods

• This is a 2 Tier system.

Page 6: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

SQL Is

• NOT OOP language (yet)

• able to store instances of classes

• able to store blobs (unformatted sequences of bytes)

Page 7: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

RBMS is good for

• storing data

• searching data

• non-programmer updates of data.

Page 8: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

JDBC+RDBMS=2 tier computing

• Client + server = 2 tier computing.

• JDBC does connections, query and result processing from the RDBMS.

• JDBC uses SQL.

Page 9: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Process

1. Connect to DBMS

2. Instance SQL Statement

3. Execute SQL statement

4. Process SQL results

5. close connection to DBMS

Page 10: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

3 tier system - distributed computing

client web server

rdbms

cgi

html

browserw/o java

jdbc jdbc results

Page 11: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

4 tier system - distributed computing

client web server

rdbms

cgi

html

browserw/o java jdbc

jdbc results

b-logic

Page 12: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Jdbc driver ideas

• JDBC needs to work with many DBMS

• Oracle

• mySql

• JET

• DB2

• SQLServer, etc....

Page 13: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Mapping to a DSN...

• JDBC with ODBC driver is called JDBC/ODBC (microsoft method).

• connect using a protocol and subprotocol

Page 14: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

How do I formulate a connect URL?

• String url =“jdbc:dbnet://show.docjava.com:356/addresses”

• jdbc – primary protocol

• dbnet – sub protocol

• show.docjava.com – FQDN

• 356 – standard port for RDBMS services

Page 15: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

What Packages do I need?

• import java.sql.*;

Page 16: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

The format of the url string is vendor specific

• String url = “jdbc:dbnet://localhost:356/Books”;• where dbnet is an example of a subprotocol,

and //localhost:356/Books is an example of a subname. The DriverManager class can also take an optional user name and password. For example:

• Connection c = DriverManager.getConnection(url, “userid”, “password”);

Page 17: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Setting up a RDBMS

• How to set up your RDBMS

• How to get drivers for JDBC

• How to load the drivers

• How to set up a select few IDEs for programming

Page 18: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

data source name (DSN)

• The DSN can be set using one of several techniques. Under Windows, double-click the ODBC Data Sources (32bit) control panel.

Page 19: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Using Appendix H

• ODBC Data Source Administrator is shown upon opening the ODBC Data Sources control panel. Select the MS Access Database driver and click Add...

Page 20: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Adding a database

Page 21: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Create a new data source

Page 22: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Set up

Page 23: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Add the DSN

Page 24: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

MDB Files look like:

Page 25: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Mapping the DSN File:

Page 26: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Using odbc to mysql

• http://www.mysql.com/downloads/api-myodbc.html

• Access to mysql via odbc driver

Page 27: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Configuration

Page 28: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Accessing and ODBC database

Page 29: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Select your Data Source

Page 30: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Select your tables

Page 31: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Click on the tables

Page 32: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Setting up mysql

• >mysql• Welcome to the MySQL monitor.

Commands end with ; or \g.• Your MySQL connection id is 4 to server

version: 3.23.22-beta-log• Type 'help' for help.• mysql> use test • Database changed

Page 33: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Make a table

• mysql> create table User ( UserId varchar(10), Password varchar(10));

• Query OK, 0 rows affected (0.06 sec)• To create a more more complex table (for example a phone

book) use:• mysql> create table phonelist (firstname

varchar(20),lastname varchar(20), address1 varchar(255), address2 varchar(255), phone1 varchar(50), phone2 varchar(50), phone3 varchar(50));

• Query OK, 0 rows affected (0.04 sec)• mysql> show tables;

Page 34: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Get a driver

• <http://mysql.sourceforge.net/>.

Page 35: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

grant access to the user

• mysql> show grants ;• grant create on *.* to guest;• Query OK, 0 rows affected (0.08 sec)• mysql> use test;• Database changed• mysql> create table flatfile (message char(255));• Query OK, 0 rows affected (0.02 sec)

Page 36: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Loading a table

• Use the load command to import data from a text file. For example:

• mysql> LOAD DATA LOCAL INFILE “c:\lyon\j4p\src\addbk\JAddressBook\foo.csv" INTO TABLE phonelist FIELDS TERMINATED BY '\t' (name, kind, createdDate, modifiedDate);

Page 37: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Selecting records

• select * from flatfile;

Page 38: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

class DataBaseMetaData is obtained automatically by the SqlBean

• Class.forName(driver);• c =

DriverManager.getConnection(url,userId,password);• dbmd = c.getMetaData();• catalogName =• c.getCatalog();• isReadOnly = c.isReadOnly();• usesLocalFiles =• dbmd.usesLocalFiles();• driverName =• dbmd.getDriverName();

Page 39: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

For Next Week

• Make the addressbook program index work so that selecting a letter will display the index so that it starts from that letter. Use the first letter that appears in the name field.

• If you select ‘I’ but there are no records that start with ‘I’ go to the record preceding where I should be..

• For ‘a’ go to the beginning.

Page 40: JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.

Implements the CSV features

• Using your jtable, import from text files via a delimiter panel.

• Using the delimiter panel, export your text files.