Top Banner
JAVA JDBC Java Database Programming Lamiaa Said
24

JAVA JDBC Java Database Programming

Feb 23, 2016

Download

Documents

zorica zorica

JAVA JDBC Java Database Programming. Lamiaa Said. What is a Database System? . e.g., Access, MySQL , Oracle, and MS SQL Server. Database Application Systems . The application program may use more than one DBMS. Examples of Simple SQL Statements. Select statement - PowerPoint PPT Presentation
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: JAVA JDBC  Java Database Programming

JAVA JDBC Java Database Programming

Lamiaa Said

Page 2: JAVA JDBC  Java Database Programming

What is a Database System?

Database Management System (DBMS)

Application Programs

System Users

Application Users

database

e.g., Access, MySQL, Oracle, and MS SQL Server

Page 3: JAVA JDBC  Java Database Programming

Database Application Systems

Database Management System

… database

Application Programs

Application Users

Database Management System

The application program may use more than one DBMS

Page 4: JAVA JDBC  Java Database Programming

Examples of Simple SQL Statements

• Select statement

select firstName, mi, lastNamefrom Studentwhere deptId = 'CS';

select firstName, mi, lastName from Studentwhere deptId = 'CS' and zipCode = '31411';

select * from Studentwhere deptId = 'CS' and zipCode = '31411';

Page 5: JAVA JDBC  Java Database Programming

Examples of Simple SQL Statements, cont.

• Insert statement

insert into Course (courseId, subjectId, courseNumber, title)

values ('11113', 'CSCI', '3720', 'Database Systems', 3);

Page 6: JAVA JDBC  Java Database Programming

Examples of Simple SQL Statements, cont.

• Update statement

update Course set numOfCredits = 4where title = 'Database Systems';

Page 7: JAVA JDBC  Java Database Programming

Examples of Simple SQL Statements, cont.

• Delete statement

delete Course where title = 'Database System';

Page 8: JAVA JDBC  Java Database Programming

Java Database Connectivity (JDBC)

• Is a java API that allow java programs to access Database.

• The JDBC classes are contained in the Java package java.sql

Page 9: JAVA JDBC  Java Database Programming

The Architecture of JDBC

Java Applications/Applets

JDBC API

Oracle JDBCDriver

JDBC-ODBCBridge Driver

Local or remoteORACLE DB

Oracle ODBCDriver

MicrosoftODBC Driver

Microsoft AccessDatabase

Page 10: JAVA JDBC  Java Database Programming

The JDBC-ODBC Bridge

• Is a database driver that utilize the ODBC driver to connect the database.

• This driver translates JDBC method calls into ODBC function calls.

• ODBC (Open DataBase Connectivity), is used to make it possible to access any data from any application, regardless of which (DBMS) is handling the data

Page 11: JAVA JDBC  Java Database Programming

JDBC Drivers

• A JDBC driver allows a Java application/client to communicate with a SQL database.

• A JDBC driver is a Java class. • A JDBC driver converts program (and

typically SQL) requests for a particular database.

Page 12: JAVA JDBC  Java Database Programming

Developing JDBC Programs

Page 13: JAVA JDBC  Java Database Programming

The JDBC Interfaces

Driver Manager

Connection Connection

Statement Statement Statement Statement

ResultSet ResultSet ResultSet ResultSet

Page 14: JAVA JDBC  Java Database Programming

JDBC Drivers

• A JDBC driver allows a Java application/client to communicate with a SQL database.

• A JDBC driver is a Java class. • A JDBC driver converts program (and

typically SQL) requests for a particular database.

Page 15: JAVA JDBC  Java Database Programming

Loading Drivers• Statement to load a driver:

– Class.forName("JDBCDriverClass");– or

DriverManager.registerDriver(new JDBCDriverClass());• A driver is a class. For example:

Database Driver Class SourceAccess sun.jdbc.odbc.JdbcOdbcDriver Already in JDKMySQL com.mysql.jdbc.Driver Website

Oracle oracle.jdbc.driver.OracleDriver Website

• The JDBC-ODBC driver for Access is bundled in JDK. • MySQL driver class is in mysqljdbc.jar • Oracle driver class is in classes12.jar

Page 16: JAVA JDBC  Java Database Programming

Establishing Connections

• Connection connection = DriverManager.getConnection(databaseURL);

Database URL Pattern

Access jdbc:odbc:dataSource

MySQL jdbc:mysql://hostname/dbname

Oracle jdbc:oracle:thin:@hostname:port#:oracleDBSID

Page 17: JAVA JDBC  Java Database Programming

Establishing Connections Examples

• For Access:– Connection connection = DriverManager.getConnection ("jdbc:odbc:ExampleMDBDataSource");

• For MySQL:– Connection connection = DriverManager.getConnection ("jdbc:mysql://localhost/test");

• For Oracle:– Connection connection = DriverManager.getConnection

("jdbc:oracle:thin:@liang.armstrong.edu:1521:ora9i", "scott", "tiger");

Page 18: JAVA JDBC  Java Database Programming

Creating and Executing Statements

• Creating statement:– Statement statement = connection.createStatement();

• Executing statement (for update, delete, insert):– statement.executeUpdate("create table Temp (col1

char(5), col2 char(5))");• Executing statement (for select):

– ResultSet resultSet = stmt.executeQuery ("select firstName, mi, lastName from Student where lastName " + " = 'Smith'");

Page 19: JAVA JDBC  Java Database Programming

Processing ResultSet

• Executing statement (for select):– ResultSet resultSet = stmt.executeQuery ("select

firstName, mi, lastName from Student where lastName " + " ='Smith'");

• Processing ResultSet (for select)://Iterate through the result and print the student

nameswhile (resultSet.next()){

System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3));

}

Page 20: JAVA JDBC  Java Database Programming

Simple JDBC Example import java.sql.*;public class SimpleJdbc {

public static void main(String[] args){ try {DriverManager.registerDriver(new  com.mysql.jdbc.Driver());Connection connection = DriverManager.getConnection

("jdbc:mysql://localhost/test");

Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery ("select

firstName, mi, lastName from Student where lastName " + " = 'Smith'"); 

while (resultSet.next())System.out.println(resultSet.getString(1) + "\t" +

resultSet.getString(2) + "\t" + resultSet.getString(3)); connection.close();} catch (Exception e) {System.err.println("Exception: "+e.getMessage());}

}}

Page 21: JAVA JDBC  Java Database Programming

Processing Statements

• Once a connection to a particular database is established, it can be used to send SQL statements from your program to the database.

• JDBC provides the Statement, PreparedStatement, and CallableStatement interfaces to facilitate sending statements to a database for execution and receiving execution results from the database.

Page 22: JAVA JDBC  Java Database Programming

The executeQuery, and executeUpdate Methods

• The methods for executing SQL statements are execute, executeQuery, and executeUpdate, each of which accepts a string containing a SQL statement as an argument.

• This string is passed to the database for execution.• The executeQuery method should be used if the

execution produces a single result set, such as the SQL select statement.

• The executeUpdate method should be used if the statement results in a single update count or no update count, such as a SQL INSERT, DELETE, UPDATE, or DDL statement.

Page 23: JAVA JDBC  Java Database Programming

PreparedStatement

• The PreparedStatement interface is designed to execute dynamic SQL statements and SQL-stored procedures.

• These SQL statements and stored procedures are precompiled for efficient use when repeatedly executed.

PreparedStatement pstmt = connection.prepareStatement

("insert into Student (firstName, mi, lastName) +

values (?, ?, ?)");

Pstmt.setStirng(1, name); ……

Page 24: JAVA JDBC  Java Database Programming

24

Questions