O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Computer Science:Java jdbc

Java jdbc-JDBC Drivers
Classes and Interfaces Of JDBC API
Java Database Connectivity with 5 Steps-Register the driver class

  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Computer Science:Java jdbc

  1. 1. JAVA JDBC Seena. K Assistant Professor Department of Vocational Studies St. Mary’s College, Thrissur
  2. 2. Java JDBC,Seena.k.,St.Mary’s College Java JDBC JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database . It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database. “It is a Java-based data access technology used for Java database connectivity.  It provides methods to query and update data in a database, and is oriented towards relational Databases
  3. 3. Java JDBC,Seena.k.,St.Mary’s College JDBC Drivers There are four types of JDBC drivers 1. JDBC-ODBC Bridge Driver, 2. Native Driver, 3. Network Protocol Driver, and Thin Driver
  4. 4. Java JDBC,Seena.k.,St.Mary’s College  We can use JDBC API to access tabular data stored in any relational database. By the help of JDBC API, we can save, update, delete and fetch data from the database.
  5. 5. Java JDBC,seena.K,St.Mary’s College  We can use JDBC API to handle database using Java program and can perform the following activities: 1. Connect to the database 2. Execute queries and update statements to the database 3. Retrieve the result received from the database.  It is based on the X/Open SQL Call Level Interface.  The java.sql package contains classes and interfaces for JDBC API
  6. 6. Java JDBC,Seena.k,St.Mar’ys College Classes and Interfaces Of JDBC API The java.sql package contains classes and interfaces for JDBC API. A list of popular interfaces of JDBC API are given below: Classes Interfaces 1. DriverManager class 1. Driver Interface 2. Blob class 2. Connection Interface 3. Clob class 3. Statement Interface 4. Types class 4. PreparedStatement Interface & ResultSet Interface
  7. 7. Java JDBC,Seena.k,St.Mar’ys College What is API  API (Application programming interface) is a document that contains a 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,
  8. 8. Java JDBC,Seena.K,St.Mary’s College Java Database Connectivity with 5 Steps  There are 5 steps to connect any java application with the database using JDBC. These steps are as follows: 1. Register the Driver class 2. Create connection 3. Create statement 4. Execute queries 5. Close connection
  9. 9. Java JDBC,Seena.K,St.Mary’s College  The forName() method of class is used to register the driver class. This method is used to dynamically load the driver class.  The DriverManager class 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. Register the driver class
  10. 10. Java JDBC,Seena.K,St.Mary’s College  A Connection is the session between java application and database. The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of Connection can be used to get the object of Statement and DatabaseMetaData.  public Statement createStatement(): creates a statement object that can be used to execute SQL queries. Connection interface
  11. 11. Java JDBC,Seena.K,St.Mary’s College  The Statement interface provides methods to execute queries with the database. The statement interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.  public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet. Statement interface
  12. 12. Java JDBC,Seena.K,St.Mary’s College  The object of ResultSet maintains a cursor pointing to a row of a table. Initially, cursor points to before the first row. ResultSet Interface public boolean next(): is used to move the cursor to the one row next from the current position. 3) public boolean first(): is used to move the cursor to the first row in result set object. 4) public boolean last(): is used to move the cursor to the last row in result set object.
  13. 13. Java JDBC,Seena.k,St.Mar’ys College To connect Java application with the MySQL database, we need to follow 5 following steps.  Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.  Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number. Java Database Connectivity with MySQL
  14. 14. Java JDBC,Seena.k,St.Mar’ys College Username: The default username for the mysql database is root. Password: It is the password given by the user at the time of installing the mysql database. In this example, we are going to use root as the password. Java Database Connectivity with MySQL
  15. 15. Java JDBC,Seena.k,St.Mar’ys College import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/sonoo","root","root"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); }catch(Exception e){ System.out.println(e);} }} Example to Connect Java Application with mysql database
  16. 16. Java JDBC,Seena.k,St.Mar’ys College REFERENCE www.javatpoint.com

×