Jdbc api

  • 1,294 views
Uploaded on

jdbc api, classes and interfaces of jdbc to connect with databases and to fire a query

jdbc api, classes and interfaces of jdbc to connect with databases and to fire a query

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,294
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. JDBC APIS http://www.java2all.com
  • 2. Chapter 2JDBC APIs: http://www.java2all.com
  • 3. JDBC APIs: http://www.java2all.com
  • 4. If any java application or an applet wants toconnect with a database then there are variousclasses and interfaces available in java.sql package. Depending on the requirements these classesand interfaces can be used. Some of them are list out the below which areused to perform the various tasks with database aswell as for connection. http://www.java2all.com
  • 5. Class or Interface DescriptionJava.sql.Connection Create a connection with specific database The task of DriverManager is to manage the databaseJava.sql.DriverManager driver It executes SQL statements for particular connection andJava.sql.Statement retrieve the results It allows the programmer to create prepared SQLJava.sql.PreparedStatement statementsJava.sql.CallableStatement It executes stored procedures This interface provides methods to get result row by rowJava.sql.ResultSet generated by SELECT statements http://www.java2all.com
  • 6. Now we are going to elobrate each class orinterface in detail with their methods and will giveprogram for each one in next topic. http://www.java2all.com
  • 7. The Connection interface: http://www.java2all.com
  • 8. The Connection interface used to connect javaapplication with particular database. After crating the connection with database wecan execute SQL statements for that particularconnection using object of Connection and retrieve theresults. The interface has few methods that makeschanges to the database temporary or permanently.The some methods are as given below. http://www.java2all.com
  • 9. Method Description This method frees an object of type Connection fromvoid close() database and other JDBC resources. This method makes all the changes made since the lastvoid commit() commit or rollback permanent. It throws SQLExeception. This method creates an object of type Statement forStatement createStatement() sending SQL statements to the database. It throws SQLExeception.boolean isClosed() Return true if the connection is close else return false. This method creates an object of typeCallableStatement prepareCall(String s) CallableStatement for calling the stored procedures from database. It throws SQLExeception. This method creates an object of typePreparedStatement PrepareStatement for sending dynamic (with or withoutprepareStatement(String s) IN parameter) SQL statements to the database. It throws SQLExeception.void rollback() This method undoes all changes made to the database. http://www.java2all.com
  • 10. The example program for Connection interfaceand its methods are given in next chapter for differentdatabases. http://www.java2all.com
  • 11. Statement Interface: http://www.java2all.com
  • 12. The Statement interface is used for toexecute a static query. It’s a very simple and easy so it also calls a“Simple Statement”. The statement interface has several methodsfor execute the SQL statements and also get theappropriate result as per the query sent to thedatabase. Some of the most common methods are asgiven below http://www.java2all.com
  • 13. Method Description This method frees an object of type Statement fromvoid close() database and other JDBC resources. This method executes the SQL statement specified by s.boolean execute(String s) The getResultSet() method is used to retrieve the result. This method retrieves the ResultSet that is generated byResultSet getResultet() the execute() method.ResultSet This method is used to execute the SQL statementexecuteQuery(String s) specified by s and returns the object of type ResultSet. This method returns the maximum number of rows thoseint getMaxRows() are generated by the executeQuery() method. This method executes the SQL statement specified by s.Int executeUpdate(String s) The SQL statement may be a SQL insert, update and delete statement. http://www.java2all.com
  • 14. The example program for Statement interfaceand its methods are given in next chapter for differentdatabases. http://www.java2all.com
  • 15. The Prepared Statement Interface: http://www.java2all.com
  • 16. The Prepared Statement interface is used toexecute a dynamic query (parameterized SQLstatement) with IN parameter. IN Parameter:- In some situation where we need to passdifferent values to an query then such values can bespecified as a “?” in the query and the actual valuescan be passed using the setXXX() method at the timeof execution.Syntax :setXXX(integer data ,XXX value); http://www.java2all.com
  • 17. Where XXX means a data type as per the valuewe want to pass in the query.For example,String query = "Select * from Data where ID = ? andName = ? ";PreparedStatement ps = con.prepareStatement(query); ps.setInt(1, 1); ps.setString(2, "Ashutosh Abhangi"); The Prepared statement interface has severalmethods to execute the parameterized SQL statementsand retrieve appropriate result as per the query sent tothe database. Some of the most common methods are as givenbelow http://www.java2all.com
  • 18. Method Description This method frees an object of type Prepared Statement fromvoid close() database and other JDBC resources. This method executes the dynamic query in the object of typeboolean execute() Prepared Statement.The getResult() method is used to retrieve the result. This method is used to execute the dynamic query in theResultSet executeQuery() object of type Prepared Statement and returns the object of type ResultSet. This method executes the SQL statement in the object of typeInt executeUpdate() Prepared Statement. The SQL statement may be a SQL insert, update and delete statement. The ResultSetMetaData means a deta about the data of ResultSet.This method retrieves an object of typeResultSetMetaData getMetaData() ResultSetMetaData that contains information about the columns of the ResultSet object that will be return when a query is execute. This method returns the maximum number of rows those areint getMaxRows() generated by the executeQuery() method. http://www.java2all.com
  • 19. The example program for Prepared Statementinterface and its methods are given in next chapter fordifferent databases. http://www.java2all.com