Java DataBase Connectivity API (JDBC API)
Click here to load reader
-
Upload
luzan-baral -
Category
Engineering
-
view
191 -
download
3
description
Transcript of Java DataBase Connectivity API (JDBC API)
JDBC API Presenter:Luzan Baral
Sijan ShresthaPratima Tiwari
What is an API?• application program interface• a set of routines, protocols, and tools for building
software applications• provides all the building blocks
JDBC API• JDBC is a Java Database Connectivity API that lets you access virtually
any tabular data source from a Java application.
• JDBC defines a low-level API designed to support basic SQL functionality independently of any specific SQL implementation. This means the focus is on executing raw SQL statements and retrieving their results.
• The JDBC 2.0 API includes two packages: java.sql, known as the JDBC 2.0 core API and javax.sql, known as the JDBC Standard Extension. Together, they contain the necessary classes to develop database applications using Java.
JDBC API (cont..)• The JDBC Extension Package (javax.sql) was introduced to contain the
parts of the JDBC API that are closely related to other pieces of the Java platform that are themselves optional packages, such as the Java Naming and Directory Interface (JNDI) and the Java Transaction Service (JTS).
• The JDBC API, found in the java.sql package, contains only a few concrete classes. Much of the API is distributed as database-neutral interface classes that specify behavior without providing any implementation. The actual implementations are provided by third-party vendors.
How JDBC API helps?
1. Open a Connection
2. Send a statement
3. Retrieve results
4. Close a connection 1. Create a connection Session
2. Execute statement
3. Send results
4. Close the session
Java Application
DBMS Engine
Java DB API
JDBC API works as a connectivity helper between Java Application between DBMS Engine
Using JDBC APITo execute a statement against a database, the following flow is observed• Load the driver (only performed once)• Obtain a Connection to the database (save for later use)• Obtain a Statement object from the Connection• Use the Statement object to execute SQL. Updates, inserts and deletes
return Boolean. Selects return a ResultSet• Navigate ResultSet, using data as required• Close ResultSet• Close Statement• The same connection object can be used to create further statements, so
donot close the Connection• But close the connection when no longer need to access the database
Classes and Interfaces of java.sqlClass or Interfaces Description
java.sql.Connection Create a connection with specific database
java.sql.DriverManager The task of DriverManager is to manage the database driver
java.sql.Statement It executes SQL statements for particular connection and retrieve the results
java.sql.PreparedStatement
It allows the programmer to create prepared SQL statements
java.sql.CallableStatement
It executes stored procedures
java.sql.ResultSet This interface provides methods to get result row by row generated by SELECT statements
Methods in JDBC API - ConnectionMethod Description
void close() This methods frees an object of type Connection from database and other JDBC resources
void commit() This method makes all the changes made since the last commit or rollback permanent. It throws SQLException.
Statement createStatement()
This method creates an object of type Statement for sending SQL statements to the database. It throws SQLException.
boolean isClosed() Return true if the connection is closed else return false.CallableStatement prepareCall(String S)
This method creates an object of type CallableStatement for calling the stored procedures from database. It throws SQLException.
PreparedStatedment prepareStatement(String S)
This method creates an object of type PrepareStatement for sending dynamic SQL statements to the database. It throws SQLException.
Void rollback() This method undoes all changes mage to the database.
Methods in JDBC API - StatementMethod Description
void close() This methods frees an object of type Statement from database and other JDBC resources
boolean execute(String S)
This method executes the SQL statement specified by s. The getResultSet() method is used to retrieve the result.
ResultSet getResultSet() This method retrieves the ResultSet that is generated by the execute() method.
ResultSet executeQuery(String S)
This method is used to execute the SQL statement specified by s and returns the object of type ResultSet.
Int getMaxRows() This method returns the maximum number of rows those are generated by the executeQuery() method.
int executeUpdate(String s)
This method executes the SQL statement specified by s. The SQL statement may be a SQL insert, update and delete statement.
Thank You!
Follow me on Twitter @luzanb