CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java...

29
CSI 3125, Preliminaries, page 1 JDBC

description

CSI 3125, Preliminaries, page 3 JDBC Architecture:

Transcript of CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java...

Page 1: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 1

JDBC

Page 2: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 2

JDBC• JDBC stands for Java Database Connectivity, which is a

standard Java API (application program interface ) for database-independent connectivity between the Java programming language and a wide range of databases.

• The JDBC library includes APIs for each of the tasks commonly associated with database usage:

• Making a connection to a database• Creating SQL or MySQL statements• Executing that SQL or MySQL queries in the database• Viewing & Modifying the resulting records

Page 3: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 3

JDBC Architecture:

Page 4: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 4

JDBC Architecture:• The JDBC API uses a driver manager and

database-specific drivers to provide connectivity to different databases.

• The JDBC driver manager ensures that the correct driver is used to access each data source.

• JDBC drivers are normally provided by specific data base vendors and they act as the interface to communicate between JDBC and a database

Page 5: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 5

JDBC Architecture: JDBC DriverManager• DriverManager manage the different types of JDBC

database driver. • DriverManager's responsibility to load all the drivers

found in the system properly. • Driver Manager's duty to choose the most

appropriate driver from the previously loaded drivers.

• JDBC DriverManager is a class in JDBC API and objects of this class can connect Java applications to a JDBC driver

Page 6: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 6

JDBC Architecture: JDBC Driver• JDBC Drivers are written by vendors and must

support the basic features of the JDBC specification

• It serves as the interface to communications between JDBC and a database.

• It processes JDBC methods invocations, sends SQL statements to a specific data source, and returns results back to the application.

Page 7: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 7

JDBC Architecture: Driver Type• JDBC DRIVERS TYPES: • JDBC driver implementations vary because of the

wide variety of operating systems and hardware platforms in which Java operates.

• Sun has divided the implementation types into four categories:

• Type 1 - JDBC-ODBC Bridge Driver • Type 2 - JDBC-Native Bridge Driver • Type 3 - JDBC-Net Bridge Driver (Net pure Java)• Type 4 - Direct JDBC Driver

Page 8: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 8

JDBC Architecture: Driver Type• Type 1 - JDBC-ODBC Bridge Driver • In a Type 1 driver, a JDBC bridge is used to access

ODBC drivers installed on each client machine. • Since there are different types of databases and each

of them has its own way of dealing with SQL queries and to return the results, ODBC provides identical results regardless of the type of database

• ODBC driver is included in the Java 2 SDK within the sun.jdbc.odbc package

Page 9: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 9

JDBC Architecture: Driver Type• Type 1 - JDBC-ODBC Bridge Driver • In order to use Type 1 drivers ODBC must be installed

on the computer having the driver • Type 1 drivers are not suitable for developing web

applications as they need software to be installed on client machine

• Using ODBC requires configuring on the system a Data Source Name (DSN) that represents the target database.

• ODBC is developed by Microsoft that enables communication between databases clients running on Windows and DBMS

Page 10: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 10

JDBC Architecture: Driver Type• Type 1 - JDBC-ODBC Bridge Driver

Page 11: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 11

JDBC Architecture: Driver Type• Type 2 - JDBC-Native Bridge Driver • Type 2 drivers use the Java Native Interface (JNI) to

make calls to a local database library API. • This driver converts the JDBC calls into a database

specific call for databases such as SQL, ORACLE etc i.e.

• This driver is specific to a particular database.

Page 12: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 12

JDBC Architecture: Driver Type• Type 2 - JDBC-Native Bridge Driver • This driver communicates directly with the database

server and requires some native code to connect to the database.

• These drivers typically provided by the database vendors and used in the same manner as the JDBC-ODBC Bridge, driver must be installed on each client machine.

• If we change the Database we have to change the native API driver as it is specific to a database.

• Cannot be used for the Internet.

Page 13: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 13

JDBC Architecture: Driver Type• Type 3 - JDBC-Net Bridge Driver (Net pure Java)• The JDBC type 3 driver, also known as the network-protocol

driver • The middle-tier (application server) converts JDBC calls directly

or indirectly into the vendor-specific database protocol. • Type 3 database requests are passed through the network to

the middle-tier server and the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers to translate the request to the database.

• Since the communication between client and the middleware server is database independent, there is no need for the vendor db library on the client machine

Page 14: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 14

JDBC Architecture: Driver Type• Type 3 - JDBC-Net Bridge Driver (Net pure Java)

Page 15: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 15

JDBC Architecture: Driver Type• Type 4 – Direct JDBC Driver • Type 4 drivers are pure Java drivers to communicate directly with

the database. • Like Type 3 drivers, they do not require native database libraries

and can be deployed over the Internet without client installation. • One drawback to Type 4 drivers is that they are database specific. • Unlike Type 3 drivers, if your back-end database changes, you

may have to purchase and deploy a new Type 4 driver.• However, because Type 4 drivers communicate directly with the

database engine rather than through middleware or a native library, they are usually the fastest

Page 16: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 16

JDBC Architecture: Driver Type• Type 4 – Direct JDBC Driver • In a Type 4 driver, a pure Java-based driver that

communicates directly with vendor's database through socket connection.

• This is the highest performance driver available for the database and is usually provided by the vendor itself.

• This kind of driver is extremely flexible, you don't need to install special software on the client or server.

• Driver completely written in Java

Page 17: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 17

Driver Usage• When accessing one type of database, such as Oracle,

Sybase, or IBM, the preferred driver type is 4.• when Java application is accessing multiple types of

databases at the same time, type 3 is the preferred driver.

• Type 2 drivers are useful in situations where a type 3 or type 4 driver is not available yet for your database.

• The type 1 driver is used for development and testing purposes only.

Page 18: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 18

JDBC• The java.sql package contains the entire set of

interfaces and classes defined by JDBC API that sends SQL (Structured Query Language) statements to relational databases and retrieves the results of executing those SQL statements.

Page 19: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 19

JDBC Program Steps• Step 1:• Create a table check(id:PK, u_name:Text, age:number) in the any

database such as(MS Access, Oracle, MySQL,SQL..) • Create Data Source Name, using ODBC • Step 2:• Import JDBC Packages (import java.sql.*;)• Step 3:• Register JDBC Driver: • Load database specific driver by calling Class.forName("Name of the

driver class file").Syntax - Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");The Class class is avaliable in java.lang package.

• The database driver is different for each DBMS. • The type1 driver is same for all the databases.

Page 20: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 20

JDBC Program Steps• Step 4:• Once JDBC driver is loaded, • The DriverManager is used to establish connection with

database server using getConnection() method.• The Connection type is the return type of getConnection(). • The DriverManager and Connection are available in java.sql

package.• Syntax - Connection

con=DriverManager.getConnection("URL","userid","password");• URL - jdbc:odbc:<datasourcename>• Userid : user Id of database (Optional)• Password : Password of database (Optional)

Page 21: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 21

JDBC Program Steps• Step 5:• Creating a jdbc Statement object • Statement statement =

dbConnection.createStatement(); • eg: Statement stmt = con.createStatement() ;

• String query = "SELECT col1, col2, col3 FROM sometable”;

• Step 6:• ResultSet resultSet = statement.executeQuery(query);

Page 22: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 22

JDBC Prog Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

• import java.sql.*;• public class db6• { public static void main(String[] args) • {try• {• Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");• Connection con = DriverManager.getConnection("jdbc:odbc:ds");• Statement stmt = con.createStatement();• ResultSet rs = stmt.executeQuery("select * from check");• while (rs.next())• System.out.println("Name " + rs.getString("u_name") + " Age " +rs.getString("age"));• con.close();• }• catch (SQLException e)• {System.out.println(e);}• catch (Exception e)• {System.out.println(e); }• }• }

Page 23: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 23

JDBC Prog Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

• Insert a row in the table

• Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");• Connection con =

DriverManager.getConnection("jdbc:odbc:ds");• Statement stmt = con.createStatement();• stmt.executeUpdate("insert into check (u_name,age)

values('appu',23)");

Page 24: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 24

JDBC Prog insert & displayDatabase: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

• import java.sql.*;• public class db7• { public static void main(String[] args) throws Exception• {• Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");• Connection con = DriverManager.getConnection("jdbc:odbc:ds");• Statement stmt = con.createStatement();• stmt.executeUpdate("insert into check (u_name,age) values('appu',23)");• ResultSet rs=stmt.executeQuery("select *from check");• while (rs.next())• System.out.println("Name " + rs.getString("u_name") + " Age "

+rs.getString("age"));• con.close();•• }• }

Page 25: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 25

JDBC Prog updateDatabase: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

• Connection con = DriverManager.getConnection("jdbc:odbc:ds");

• Statement stmt = con.createStatement();• stmt.executeUpdate("update check set u_name='zoo'

where u_name='appu'");

Page 26: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 26

JDBC Prog deleteDatabase: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

• Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");• Connection con =

DriverManager.getConnection("jdbc:odbc:ds");• Statement stmt = con.createStatement();• stmt.executeUpdate("delete from check where

u_name='zoo'");

Page 27: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 27

JDBC Prog deleteDatabase: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

Page 28: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 28

JDBC Prog deleteDatabase: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

Page 29: CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.

CSI 3125, Preliminaries, page 29

JDBC Prog• import java.sql.*;• public class database• { public static void main(String[] args)• {• Connection conn=null;• try{• Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");• } • catch(ClassNotFoundException e){• System.out.println(e);}• try{• conn = DriverManager.getConnection("jdbc:odbc:ds");• } • catch(SQLException se) {• System.out.println(se);}• System.out.println("connection is successful!!!");• try{• String selectSQL = "select * from check";• Statement stmt = conn.createStatement();• ResultSet rset = stmt.executeQuery(selectSQL);• while(rset.next()){• System.out.println("ID: " + rset.getString(1) + " NAME: " +rset.getString(2) + " Age: "+rset.getString(3) );• }• stmt.close();• } catch(SQLException se) {• System.out.println(se);}• }• }