java- jdbc- mazenet solution
TRANSCRIPT
![Page 1: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/1.jpg)
JDBC
BySharmilee
9894303344Java Trainer
Mazenet Solution
![Page 2: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/2.jpg)
Objective• Introduction to JDBC• JDBC Drivers• Steps to connect database• ResultSet• Statement & PreparedStatement• Transaction Management• Batch Processing
![Page 3: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/3.jpg)
Introduction• Java JDBC is a java API to connect
and execute query with the database.
• JDBC API uses jdbc drivers to connect with the database.
![Page 4: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/4.jpg)
![Page 5: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/5.jpg)
Why we use JDBC?• Before JDBC, ODBC API is used• ODBC API uses ODBC driver • ODBC Driver written in C language– platform dependent and – unsecured.
• That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
![Page 6: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/6.jpg)
API• API (Application programming
interface) is a document that contains description of all the features of a product or software.
• It represents classes and interfaces that software programs can follow to communicate with each other.
• An API can be created for applications, libraries, operating systems, etc
![Page 7: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/7.jpg)
JDBC Drivers
![Page 8: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/8.jpg)
JDBC Drivers
• JDBC Driver is a software component
• enables java application to interact with the database
![Page 9: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/9.jpg)
Types of JDBC Drivers1. JDBC-ODBC bridge driver2. Native-API driver (partially java
driver)3. Network Protocol driver (fully java
driver)4. Thin driver (fully java driver)
![Page 10: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/10.jpg)
1. JDBC-ODBC bridge Driver
• This uses ODBC driver to connect to the database.
• This driver converts JDBC method calls into the ODBC function calls.
![Page 11: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/11.jpg)
![Page 12: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/12.jpg)
Advantages•easy to use.•can be easily connected to any database.
Disadvantages•Performance degraded because JDBC method call is converted into the ODBC function calls.•The ODBC driver needs to be installed on the client machine.
![Page 13: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/13.jpg)
2. Native – API Driver• It uses the client-side libraries of the
database.
• It converts JDBC method calls into native calls of the database API.
• It is not written entirely in java.
![Page 14: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/14.jpg)
![Page 15: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/15.jpg)
Advantages• performance upgraded
than JDBC-ODBC bridge driver.
Dis-advantages
•The Native driver needs to be installed on the each client machine.•The Vendor client library needs to be installed on client machine.
![Page 16: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/16.jpg)
3. Network Protocol driver• It uses middleware (application
server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol.
• It is fully written in java.
![Page 17: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/17.jpg)
![Page 18: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/18.jpg)
AdvantagesNo client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.
Dis-advantages
• Network support is required on client machine.Requires database-specific coding to be done in the middle tier.
•Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier.
![Page 19: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/19.jpg)
4. Thin Layer• The thin driver converts JDBC calls
directly into the vendor-specific database protocol.
• It is fully written in Java language.
![Page 20: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/20.jpg)
![Page 21: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/21.jpg)
Advantages•Better performance than all other drivers.•No software is required at client side or server side.
Dis-advantages
• Drivers depends on the Database.
![Page 22: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/22.jpg)
Steps to connect oracle Database
![Page 23: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/23.jpg)
5 Steps to connect to the database in java
• Register the driver class• Creating connection• Creating statement• Executing queries• Closing connection
![Page 24: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/24.jpg)
1. Register the driver class• The forName() method of Class class
is used to register the driver class.• This method is used to dynamically
load the driver class.Syntax of forName() method
public static void forName(String className)throws ClassNotFoundException
Example to register the OracleDriver class
Class.forName("oracle.jdbc.driver.OracleDriver");
![Page 25: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/25.jpg)
2. Create the connection object
• The getConnection() method of DriverManager class is used to establish connection with the database.
Syntax of getConnection() method
1) public static Connection getConnection(String url)throws SQLException 2) public static Connection getConnection(String url,String name,String password) throws SQLException Example to establish connection with the Oracle database
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","password");
![Page 26: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/26.jpg)
3) Create the Statement object
• The createStatement() method of Connection interface is used to create statement.
• The object of statement is responsible to execute queries with the database.Syntax of createStatement() method
public Statement createStatement()throws SQLException
Example to create the statement object
Statement stmt=con.createStatement();
![Page 27: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/27.jpg)
4. Execute the query• It is used to execute queries to the
database. • This method returns the object of
ResultSet that can be used to get all the records of a table.Syntax of executeQuery() methodpublic ResultSet executeQuery(String sql)throws SQLException
Example to execute queryResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()){ System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
![Page 28: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/28.jpg)
5. Close the connection object
• By closing connection object statement and ResultSet will be closed automatically.
• The close() method of Connection interface is used to close the connection.Syntax of close() methodpublic void close()throws SQLException
Example to close connectioncon.close();
![Page 29: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/29.jpg)
DriverManager Class• It acts as an interface between user
and drivers.
• It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver.
![Page 30: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/30.jpg)
Connection interface• A Connection is the session between
java application and database.• The Connection interface provide
many methods for transaction management like commit(),rollback() etc.
Note: By default, connection commits the changes after executing queries.
![Page 31: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/31.jpg)
Statement Interface• The Statement interface provides
methods to execute queries with the database.
• It provides factory method to get the object of ResultSet.
![Page 32: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/32.jpg)
ResultSet Interface• The object of ResultSet maintains a
cursor pointing to a particular row of data.
• Initially, cursor points to before the first row.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
![Page 33: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/33.jpg)
PreparedStatement Interface
• The PreparedStatement interface is a subinterface of Statement.
• It is used to execute parameterized query
String sql="insert into emp values(?,?,?)";
![Page 34: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/34.jpg)
Why we use PreparedStatement?
• Improves performance: The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.
![Page 35: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/35.jpg)
ResultSetMetaData Interface
• The metadata means data about data i.e. we can get further information from the data.
• Metadata of a table – total number of column, – column name, – column type etc. ,
• ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.
![Page 36: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/36.jpg)
Transaction Management
![Page 37: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/37.jpg)
Transaction Management• Transaction represents a single
unit of work.• The ACID properties describes the
transaction management well. • ACID stands for
Atomicity, Consistency,isolation and durability.
![Page 38: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/38.jpg)
Transaction Management• Atomicity means either all successful or
none.• Consistency ensures bringing the
database from one consistent state to another consistent state.
• Isolation ensures that transaction is isolated from other transaction.
• Durability means once a transaction has been committed, it will remain so, even in the event of errors, power loss etc.
![Page 39: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/39.jpg)
Advantage of Transaction management
• Fast performance - It makes the performance fast because database is hit at the time of commit.
![Page 40: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/40.jpg)
![Page 41: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/41.jpg)
Batch Processing
![Page 42: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/42.jpg)
• Instead of executing a single query, we can execute a batch (group) of queries.
• It makes the performance fast.
• java.sql.Statement & java.sql.PreparedStatement interfaces provide methods for batch processing.
![Page 43: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/43.jpg)
Example for Batch Processing
• Load the driver class• Create Connection• Create Statement• Add query in the batch• Execute Batch• Close Connection
![Page 44: Java- JDBC- Mazenet Solution](https://reader033.vdocuments.mx/reader033/viewer/2022052405/58d1d6771a28ab66108b4f2f/html5/thumbnails/44.jpg)
Thank You!