{"id":59,"date":"2018-07-10T10:26:35","date_gmt":"2018-07-10T10:26:35","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=59"},"modified":"2019-05-15T07:11:22","modified_gmt":"2019-05-15T07:11:22","slug":"jdbc1","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/jdbc1\/","title":{"rendered":"JDBC1"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/sOEy-XPlatg\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n&nbsp;\r\n\r\n<strong>What is JDBC?<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">JDBC is JDBC. It is not an acronym. Well, we do come across texts which describes JDBC as an acronym for Java Database Connectivity, and also there are other texts which would mention, that JDBC is not an \u00a0acronym.<\/p>\r\n&nbsp;\r\n\r\nLet\u2019s not worry about that. ODBC and JDBC\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>What is ODBC?<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">ODBC is Open Database Connectivity. ODBC is a standard set of calls, which is used for connecting and interacting with a database. ODBC defines a standard way of interaction with any kind of database. There are various kinds of databases, available from various vendors. These database vendors normally have a native driver for their database. This native driver is a collection of proprietary calls, which can be used by any application developer to interact with the database. These native drivers are normally accessedfrom applications written in C or other native programming languages. Applications written using the native drivers, are not portable to other kinds of databases. ODBC was a standard created to make applications independent of the database. ie. if an application developer, develops an application which uses only ODBC calls, for interaction with a database, then the application is independent of the kind of database being used. The database vendors, supply an ODBC driver along with the database. The ODBC driver is the implementation of the ODBC calls, specifically for interaction with a particular kind of database. So, if an application is using only ODBC calls, then the application would be able to interact with any kind of database for which an ODBC driver is available. This allows, developers to develop prototype using a smaller footprint databases, and that same code could be reused in a production environment. Also switching from one kind of database to another would not require any kind of porting efforts. The same application would work with other kinds of database, without recompilation.<\/p>\r\n<p class=\"hanging-indent\" style=\"text-align: center;\"><img class=\"alignnone size-full wp-image-60\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1.png\" alt=\"\" width=\"825\" height=\"200\" \/>\r\n<strong> Native Applications interacting with database via ODBC.<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Like ODBC, JDBC is used by Java applications to connect and interact with a database. JDBC is a standard set of classes and interfaces, available as part of the standard java . sql package. A Java application can make use of these standard JDBC classes and interfaces in order to connect and interact with a database. Most of the API, in java . sql package is made up of interfaces.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><strong>Java Applications interacting with database via JDBC.<\/strong><\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-61\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2.png\" alt=\"\" width=\"844\" height=\"192\" \/>\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>JDBC Drivers<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The different JDBC drivers, for connection and interaction with different types of databases are the different implementations of these interfaces (available from java . sql package) and their additional supporting classes. A JDBC driver (the set of implementation classes) is normally packaged and available in a jar file. There are different JDBC drivers for different databases. Even for connecting to the same database, there are multiple vendors, supplying the JDBC Drivers. The JDBC drivers have been categorized into four types based on their implementations. The main concern, in this categorization is related to the use of native methods (dependence on native code).<\/p>\r\n<p style=\"text-align: center;\"><strong>Types of JDBC Drivers<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">There are four types of JDBC Drivers. The Type-1, and Type-2 drivers are the ones which make use of native methods, whereas the Type-3 and Type-4 are pure Java drivers, and do not make use of any native calls.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><strong>Types of JDBC Drivers<\/strong><\/p>\r\n\r\n<table class=\"lines\" border=\"1px solid black\" width=\"548\" align=\"center\">\r\n<tbody>\r\n<tr class=\"border\" style=\"height: 28px;\">\r\n<td class=\"border\" style=\"width: 125.063px; height: 28px;\"><\/td>\r\n<td style=\"width: 134.063px; height: 28px;\"><strong>Indirect<\/strong><\/td>\r\n<td style=\"width: 110.063px; height: 28px;\"><strong>Direct<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px;\">\r\n<td style=\"width: 125.063px; height: 28px;\">Native<\/td>\r\n<td style=\"width: 134.063px; height: 28px;\">1<\/td>\r\n<td style=\"width: 110.063px; height: 28px;\">2<\/td>\r\n<\/tr>\r\n<tr style=\"height: 29px;\">\r\n<td style=\"width: 125.063px; height: 29px;\">Pure Java<\/td>\r\n<td style=\"width: 134.063px; height: 29px;\">3<\/td>\r\n<td style=\"width: 110.063px; height: 29px;\">4<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<strong>Type-1 (JDBC-ODBC bridge driver)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The Type-1 driver is the JDBC-ODBC bridge driver. There is an implementation of the JDBC-ODBC bridge driver, included in the distribution of the standard Java Run time. This driver does not directly interact with a database. Instead, it interacts with the database, via an ODBC driver. It is dependent on an ODBC driver. The implementation of this driver makes use of the native methods, in order to make the standard ODBC calls.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-62\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3.png\" alt=\"\" width=\"816\" height=\"169\" \/>\r\n<p style=\"text-align: center;\"><strong> Java Applications interacting with database via Type-1 Driver.<\/strong><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Type-2 (Native driver)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The Type-2 driver is an implementation of the JDBC API, which directly connects to the database. It is not dependent on any other driver or application like ODBC. The native drivers, normally make use of the native library supplied by the database vendor, They make use of the proprietary calls available in the native library supplied by the database vendor. The implementation of this driver makes use of native methods, in order to make the calls to the native driver.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-63\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4.png\" alt=\"\" width=\"816\" height=\"169\" \/>\r\n<p style=\"text-align: center;\"><strong> Java Applications interacting with database via Type-2 Driver.<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><strong>Type-3 (Net driver)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The Type-3 driver is an implementation of the JDBC API, which like the Type- 1 driver is dependent on some other network based application. These drivers interact with the database via some network based middleware. The implementation of this type of JDBC driver with the network based middleware does not make use of any native methods. So, these drivers are pure Java drivers. But they do depend on some middleware which is a native application (non-Java application).<\/p>\r\n<img class=\"alignnone size-full wp-image-64\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5.png\" alt=\"\" width=\"827\" height=\"169\" \/>\r\n<div style=\"text-align: center;\"><strong>JavaApplicationsinteractingwithdatabaseviaType-4Driver.<\/strong><\/div>\r\n<div style=\"text-align: center;\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: center;\"><strong>Using JDBC Drivers and the API<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">As mentioned in above section , the JDBC Drivers are implementations of the interfaces from the java. sql package. In this package, we have lots of interfaces. One of the important interface, which is used for making a connection to the database is the Driver interface. A connection to a \u00a0database \u00a0is represented by an instance of a class implementing the Connection interface. The Driver interface provides methods which returns an instance of Connection.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Overview of API related to connection and interaction with database<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">A Java application, which would like to interact with any database, would have to first obtain an instance of Connection, before it can start interacting with the database. A single Java application may like to interact with different types of databases. ie. the application may have to manage multiple instances of the Driver. In {java. sql package, we have a class called DriverManager which manages various instances of Driver and helps the application to get a instance of Connection by using the appropriate instance of Driver from the various Driver instances which it is managing.<\/p>\r\n&nbsp;\r\n\r\nThere are three common interactions, which are performed by an application with a database.\r\n\r\n&nbsp;\r\n\r\n\u2022 executing any kind of SQL\r\n\r\n\u2022\u00a0 executing pre-compiled SQL\r\n\r\n\u2022\u00a0 executing stored-procedures and functions\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The three kinds of interaction require instances of Statement for executing any kind of SQL, PreparedStatement for executing pre-compiled SQL and CallableStatement for executing stored- procedures and functions. We have methods in the Connection interface to obtain instances of the Statement,\u00a0 PreparedStatement \u00a0and the \u00a0CallableStatement.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Connecting to a database<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">There are two steps which are normally used to make a connection to a particular database. Step one, is to register the appropriate instance of Driver implementation class with the DriverManager. Second step is to use the DriverManager to obtain the Connection instance, which represents the connection to the database. The first step is normally done by simply loading the class which implements the Driver interface. The static block of all the Driver implementation classes, carry out the task of registering an instance of the class, with the DriverManager. The DriverManager class has a static method to register a Driver instance.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">To load any class, we can use the static method forName() in the Class class. The method signature of the static method in the Class class is as given below:<\/p>\r\n&nbsp;\r\n\r\npublic static Class forName(String classname) throws ClassNotFoundException\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The forName method has only one parameter, which is used to pass the fully qualified name of the class to be loaded, and it returns the Class instance for the class which has been loaded. This method can throw a ClassNotFoundException. So, if we want to use the JDBC-ODBC driver class from the JDK. In this case the name of the driver class is \"sun.jdbc.odbc.JdbcOdbcDriver\". If we are using some other JDBC driver, then we would normally have a jar file, containing the driver class. This jar file should be made available in the CLASSPATH. We should also know the name of the class implementing the java. sql .Driver inter- face. This would be available from the documentation, for the JDBC driver being used. So the first step in an application using JDBC for connecting to a database would appear something like<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Note: Use the appropriate class name in the above call, depending on the JDBC driver we are using.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The second step is to establish a connection \u00a0to \u00a0the \u00a0database. \u00a0To \u00a0get \u00a0an \u00a0instance \u00a0of \u00a0Connection, \u00a0we use the static method getConnection() in the DriverManager class, as shown below:<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n\r\n&nbsp;\r\n\r\nConnection con = DriverManager.getConnection(\"jdbc:odbc:&lt;dsn&gt;\");\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The static method getConnection() takes as parameter a String which is the jdbc-url. The jdbc-url is of the form \"jdbc:&lt;sub-protocol&gt;:...\". This string is dependent on the driver used. Different drivers would have different requirements. The jdbc-url syntax has lot of things which depend on the JDBC driver being used. When we call the getConnection() method, the DriverManager would check each of the registered drivers, if they support the specified jdbc- url. The driver instance which accepts such a url would return the Connection to the DriverManager, which is in turn returned to the application by the DriverManager. Let us say, we would like to connect to the PostgreSQL database, and we have downloaded the pg73jdbc3.jar file, which contains the jdbc driver for connecting to PostgreSQL ver.<\/p>\r\n7.3. Then the sequence would be something like:\r\n\r\n&nbsp;\r\n\r\nClass.forName(\"org.postgresql.Driver\") ;\r\n\r\nConnection con = DriverManager.\r\n\r\ngetConnection(\"jdbc:postgresql:\/\/&lt;host&gt;\/&lt;database&gt;\");\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The static method getConnection() in DriverManager is overloaded and we may use it to separately supply \u00a0the \u00a0authentication \u00a0info.\u00a0 like<\/p>\r\n&nbsp;\r\n\r\npublic static Connection getConnection(String jdbcurl)\r\n\r\npublic static Connection getConnection(String jdbcurl, String user,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String password)\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Interacting with the database<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">As mentioned in the above section there are mainly three \u00a0kinds \u00a0of \u00a0interactions. \u00a0Executing \u00a0SQL, executing pre-compiled SQL and executing stored-procedures and functions. \u00a0Let \u00a0us \u00a0look \u00a0at \u00a0how \u00a0we use the JDBC API to carry out each of these tasks.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Executing SQL using Statement<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">Once we have the instance of Connection, and we want to execute a SQL, then we may use the createStatement() on the Connection instance to obtain the instance of Statement. The createStatement() method in Connection interface is overloaded as shown below:<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">Statement createStatement()<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">Statement createStatement(int resultSetType, int resultSetConcurrency)<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">We have the first method without parameters, and the second method takes two integer parameters. The int values for resultSetType and resultSetConcurrency are defined in the java.sql.ResultSet interface. When we use a select query to get results from a database, it would return an instance of ResultSet. The Statement instance is used for executing SQL. So the two parameters define the type of resultSets returned by this Statement when we execute any select query \u00a0using \u00a0this \u00a0statement. \u00a0The valid values for resultSetType are:<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ResultSet.TYPE_FORWARD_ONLY ResultSet.TYPE_SCROLL_SENSITIVE<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ResultSet.TYPE_SCROLL_INSENSITIVE<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">The valid values of resultSetConcurrency are:<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ResultSet.CONCUR_READ_ONLY ResultSet.CONCUR_UPDATABLE<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">By default the ResultSet type is forward only and read only. Let \u00a0us \u00a0look \u00a0at \u00a0how \u00a0to \u00a0execute \u00a0an \u00a0SQL. Once we have an instance of\u00a0 Statement. The Statement interface\u00a0 has the following \u00a0methods\u00a0\u00a0 for executing a SQL.<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">Boolean execute(String sql) int executeUpdate(String sql)<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ResultSet executeQuery(String sql)<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">All the three methods can be used for executing any kind of SQL. The SQL to be executed is passed as the parameter. All the methods have a different return type. Depending on the type of SQL to be executed, we choose the method with the appropriate return type. The execute() method is more commonly used for executing the DDL statements like CREATE and DROP. The executeUpdate() method is more commonly used for executing the DML statements like INSERT, UPDATE and DELETE. This method returns the number of rows affected by the DML statement. The executeQuery() method is more commonly used for executing the SELECT statement. It returns an instance of ResultSet, which contains the results of the SELECT query.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The instance of Statement can be used for executing multiple SQL \u00a0statements \u00a0in \u00a0a \u00a0batch. \u00a0The Statement interface has methods to \u00a0manage \u00a0SQL \u00a0statements \u00a0into \u00a0a \u00a0batch, \u00a0and \u00a0a \u00a0method \u00a0to \u00a0execute all the SQL statements which have been added into the batch. The following \u00a0methods \u00a0are \u00a0used \u00a0for batch \u00a0execution.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void addBatch(String sql) public void clearBatch( )<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public int[] executeBatch( )<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">The size of the int [] returned by executeBatch() method is the number of \u00a0SQL \u00a0statements \u00a0in \u00a0the batch, and it contains an int value corresponding to each SQL statement executed from the batch. The int value is the number of rows affected by the DML statement, and in case of SELECT statement it contains the value of -1.\u00a0<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Executing a precompiled SQL using PreparedStatement<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">An instance of Statement is used to execute any kind of SQL. There is a \u00a0sub-interface \u00a0of \u00a0Statement called PreparedStatement, which is used to execute precompiled SQL. An instance of PreparedStatement is also created from the instance of Connection by using the following methods of Connection\u00a0\u00a0 interface:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">PreparedStatement prepareStatement(String sql)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">P<\/span><span style=\"text-align: initial; font-size: 1em;\">reparedStatement prepareStatement(String sql, int rstype, int rsconcur)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Here unlike the Statement the SQL is specified at the time of creating the in- stance of PreparedStatement. The SQL, which is passed as parameter can have place holders which are introduced by using the \u2019 ?\u2019 character within the SQL string passed as parameter. These place holders are referred to as parameters for the PreparedStatement.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">PreparedStatement ps = con.prepareStatement(\"select * from account where acno=? and type = ?\");<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">PreparedStatement ps = con.prepareStatement(\"insert into account values(?,?,?,?)\");<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The first \u2019 ?\u2019 in the SQL string is the first parameter of the PreparedStatement, and so on. So, \u00a0the number of parameters in a\u00a0 PreparedSTatement will be\u00a0 the number of \u2019 ?\u2019\u00a0 characters used in the SQL string. The parameters can only be used in place for values in the SQL statement. These \u00a0parameter values need to be setup before the precompiled SQL corresponding to the PreparedStatement can be executed.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now the PreparedStatement interface has additional methods for executing the SQL. These are \u00a0the same three methods as in Statement interface, \u00a0but \u00a0without \u00a0any \u00a0parameters. \u00a0In \u00a0Statement \u00a0interface the parameter\u00a0 to the\u00a0 three execute methods was\u00a0 the SQL to be\u00a0 executed. In case\u00a0 of PreparedStatement this has already been specified at the time of creating the instance using the parameter in \u00a0the \u00a0prepareStatement() \u00a0method.\u00a0 So,\u00a0 the \u00a0methods\u00a0 for \u00a0executing\u00a0 the \u00a0precompiled \u00a0SQL \u00a0in a PreparedStatement are:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean execute() int executeUpdate()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">ResultSet executeQuery()<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Executing Stored Procedures and functions using CallableStatement<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is also\u00a0 a sub-interface\u00a0 of PreparedStatement\u00a0 called the CallableStatement, this\u00a0 is used to execute stored procedures and functions. The instance of CallableStatement is created by the method on Connection as<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement prepareCall(String callstatement)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement prepareCall(String callstatement,int rstype,int rsconcur)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The CallableStatement inherits methods from the PreparedStatement and some more additional methods. let us look at how to create an instance of CallableStatement. In the above methods \u00a0the syntax for the callstatement is:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">{ [?] = call &lt;proc-name or function name&gt; (?,...} }<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">if it is a stored procedure which is to be invoked we create CallableStatement as<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement cs = con.prepareCall(\"{ call myproc(?,?,?) }\");<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to execute a stored procedure or<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement cs = con.prepareCall(\"{ ? = call myfunctino(?, ?, ?) }\");<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to execute a function, which returns some value.<\/span><\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify;\"><strong>In Summary:<\/strong><\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify;\">Java applications use JDBC to access any kind of databases. The JDBC is a standard way of interaction with different databases, available from different vendors. The JDBC is defined using the set of interfaces and classes available in the java . sql package. There are various implementations of these standard for different types of database. These JDBC drivers are categorized into four types based on how they have been implemented.<\/li>\r\n \t<li style=\"text-align: justify;\">The Type-1 driver is the one which talks to the ODBC driver available for connected to a database. This driver makes use of native calls in order to make the ODBC calls. The type 2 driver is the one which directly talks to the database, but is not a pure Java driver, it makes use of native libraries available from the database vendor to make connection and talk to the databse. The type 3 driver is a pure Java driver, unlike the type 1 and type 2. but it does not directly talk to the database, instead it talks to some network absed service which in turn talks to the database. and type 4 driver is the one which is a pure Java driver directly communicating with the database.<\/li>\r\n \t<li style=\"text-align: justify;\">The first task in interacting with database for a Java application is to obtain an instance of Connection. This is then used for all other interactions with the database. The instance of a class implementing the Driver interface is used to obtain instance of Connection. The DriverManager class can manage various implementations of Driver, which are first required to be registered. Whenever any implementation of Driver is loaded, it would register with the DriverManager. The DriverManager has method getConnection(), which can be used to obtain a connection for a given url. The DriverManager will select the appropriate instance of Driver and use it to return the required Connection.<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on JDBC1<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/sOEy-XPlatg\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div><strong>Suggested Reading:<\/strong><\/div>\r\n<div><\/div>\r\n<div>1. Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/div>\r\n<div>2. The class of JAVA by Pravin Jain, Pearson Education.<\/div>\r\n<div>3. Database programming with JDBC and Java by George Reese. O'Reilly Media<\/div>\r\n<div>4. https:\/\/docs.oracle.com\/javase\/tutorial\/jdbc\/basics\/<\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/sOEy-XPlatg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<p>&nbsp;<\/p>\n<p><strong>What is JDBC?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">JDBC is JDBC. It is not an acronym. Well, we do come across texts which describes JDBC as an acronym for Java Database Connectivity, and also there are other texts which would mention, that JDBC is not an \u00a0acronym.<\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s not worry about that. ODBC and JDBC<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>What is ODBC?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">ODBC is Open Database Connectivity. ODBC is a standard set of calls, which is used for connecting and interacting with a database. ODBC defines a standard way of interaction with any kind of database. There are various kinds of databases, available from various vendors. These database vendors normally have a native driver for their database. This native driver is a collection of proprietary calls, which can be used by any application developer to interact with the database. These native drivers are normally accessedfrom applications written in C or other native programming languages. Applications written using the native drivers, are not portable to other kinds of databases. ODBC was a standard created to make applications independent of the database. ie. if an application developer, develops an application which uses only ODBC calls, for interaction with a database, then the application is independent of the kind of database being used. The database vendors, supply an ODBC driver along with the database. The ODBC driver is the implementation of the ODBC calls, specifically for interaction with a particular kind of database. So, if an application is using only ODBC calls, then the application would be able to interact with any kind of database for which an ODBC driver is available. This allows, developers to develop prototype using a smaller footprint databases, and that same code could be reused in a production environment. Also switching from one kind of database to another would not require any kind of porting efforts. The same application would work with other kinds of database, without recompilation.<\/p>\n<p class=\"hanging-indent\" style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-60\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1.png\" alt=\"\" width=\"825\" height=\"200\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1.png 825w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1-300x73.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1-768x186.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1-65x16.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1-225x55.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic1-350x85.png 350w\" sizes=\"auto, (max-width: 825px) 100vw, 825px\" \/><br \/>\n<strong> Native Applications interacting with database via ODBC.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Like ODBC, JDBC is used by Java applications to connect and interact with a database. JDBC is a standard set of classes and interfaces, available as part of the standard java . sql package. A Java application can make use of these standard JDBC classes and interfaces in order to connect and interact with a database. Most of the API, in java . sql package is made up of interfaces.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>Java Applications interacting with database via JDBC.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-61\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2.png\" alt=\"\" width=\"844\" height=\"192\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2.png 844w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2-300x68.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2-768x175.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2-65x15.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2-225x51.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic2-350x80.png 350w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>JDBC Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The different JDBC drivers, for connection and interaction with different types of databases are the different implementations of these interfaces (available from java . sql package) and their additional supporting classes. A JDBC driver (the set of implementation classes) is normally packaged and available in a jar file. There are different JDBC drivers for different databases. Even for connecting to the same database, there are multiple vendors, supplying the JDBC Drivers. The JDBC drivers have been categorized into four types based on their implementations. The main concern, in this categorization is related to the use of native methods (dependence on native code).<\/p>\n<p style=\"text-align: center;\"><strong>Types of JDBC Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">There are four types of JDBC Drivers. The Type-1, and Type-2 drivers are the ones which make use of native methods, whereas the Type-3 and Type-4 are pure Java drivers, and do not make use of any native calls.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>Types of JDBC Drivers<\/strong><\/p>\n<table class=\"lines\" style=\"width: 548px; margin: auto;\">\n<tbody>\n<tr class=\"border\" style=\"height: 28px;\">\n<td class=\"border\" style=\"width: 125.063px; height: 28px;\"><\/td>\n<td style=\"width: 134.063px; height: 28px;\"><strong>Indirect<\/strong><\/td>\n<td style=\"width: 110.063px; height: 28px;\"><strong>Direct<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px;\">\n<td style=\"width: 125.063px; height: 28px;\">Native<\/td>\n<td style=\"width: 134.063px; height: 28px;\">1<\/td>\n<td style=\"width: 110.063px; height: 28px;\">2<\/td>\n<\/tr>\n<tr style=\"height: 29px;\">\n<td style=\"width: 125.063px; height: 29px;\">Pure Java<\/td>\n<td style=\"width: 134.063px; height: 29px;\">3<\/td>\n<td style=\"width: 110.063px; height: 29px;\">4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Type-1 (JDBC-ODBC bridge driver)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The Type-1 driver is the JDBC-ODBC bridge driver. There is an implementation of the JDBC-ODBC bridge driver, included in the distribution of the standard Java Run time. This driver does not directly interact with a database. Instead, it interacts with the database, via an ODBC driver. It is dependent on an ODBC driver. The implementation of this driver makes use of the native methods, in order to make the standard ODBC calls.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-62\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3.png\" alt=\"\" width=\"816\" height=\"169\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3.png 816w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3-300x62.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3-768x159.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3-225x47.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic3-350x72.png 350w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<p style=\"text-align: center;\"><strong> Java Applications interacting with database via Type-1 Driver.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Type-2 (Native driver)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The Type-2 driver is an implementation of the JDBC API, which directly connects to the database. It is not dependent on any other driver or application like ODBC. The native drivers, normally make use of the native library supplied by the database vendor, They make use of the proprietary calls available in the native library supplied by the database vendor. The implementation of this driver makes use of native methods, in order to make the calls to the native driver.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-63\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4.png\" alt=\"\" width=\"816\" height=\"169\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4.png 816w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4-300x62.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4-768x159.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4-225x47.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic4-350x72.png 350w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<p style=\"text-align: center;\"><strong> Java Applications interacting with database via Type-2 Driver.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>Type-3 (Net driver)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The Type-3 driver is an implementation of the JDBC API, which like the Type- 1 driver is dependent on some other network based application. These drivers interact with the database via some network based middleware. The implementation of this type of JDBC driver with the network based middleware does not make use of any native methods. So, these drivers are pure Java drivers. But they do depend on some middleware which is a native application (non-Java application).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-64\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5.png\" alt=\"\" width=\"827\" height=\"169\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5.png 827w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5-300x61.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5-768x157.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5-225x46.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M08Pic5-350x72.png 350w\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" \/><\/p>\n<div style=\"text-align: center;\"><strong>JavaApplicationsinteractingwithdatabaseviaType-4Driver.<\/strong><\/div>\n<div style=\"text-align: center;\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>Using JDBC Drivers and the API<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">As mentioned in above section , the JDBC Drivers are implementations of the interfaces from the java. sql package. In this package, we have lots of interfaces. One of the important interface, which is used for making a connection to the database is the Driver interface. A connection to a \u00a0database \u00a0is represented by an instance of a class implementing the Connection interface. The Driver interface provides methods which returns an instance of Connection.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Overview of API related to connection and interaction with database<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">A Java application, which would like to interact with any database, would have to first obtain an instance of Connection, before it can start interacting with the database. A single Java application may like to interact with different types of databases. ie. the application may have to manage multiple instances of the Driver. In {java. sql package, we have a class called DriverManager which manages various instances of Driver and helps the application to get a instance of Connection by using the appropriate instance of Driver from the various Driver instances which it is managing.<\/p>\n<p>&nbsp;<\/p>\n<p>There are three common interactions, which are performed by an application with a database.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 executing any kind of SQL<\/p>\n<p>\u2022\u00a0 executing pre-compiled SQL<\/p>\n<p>\u2022\u00a0 executing stored-procedures and functions<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The three kinds of interaction require instances of Statement for executing any kind of SQL, PreparedStatement for executing pre-compiled SQL and CallableStatement for executing stored- procedures and functions. We have methods in the Connection interface to obtain instances of the Statement,\u00a0 PreparedStatement \u00a0and the \u00a0CallableStatement.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Connecting to a database<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">There are two steps which are normally used to make a connection to a particular database. Step one, is to register the appropriate instance of Driver implementation class with the DriverManager. Second step is to use the DriverManager to obtain the Connection instance, which represents the connection to the database. The first step is normally done by simply loading the class which implements the Driver interface. The static block of all the Driver implementation classes, carry out the task of registering an instance of the class, with the DriverManager. The DriverManager class has a static method to register a Driver instance.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">To load any class, we can use the static method forName() in the Class class. The method signature of the static method in the Class class is as given below:<\/p>\n<p>&nbsp;<\/p>\n<p>public static Class forName(String classname) throws ClassNotFoundException<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The forName method has only one parameter, which is used to pass the fully qualified name of the class to be loaded, and it returns the Class instance for the class which has been loaded. This method can throw a ClassNotFoundException. So, if we want to use the JDBC-ODBC driver class from the JDK. In this case the name of the driver class is &#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;. If we are using some other JDBC driver, then we would normally have a jar file, containing the driver class. This jar file should be made available in the CLASSPATH. We should also know the name of the class implementing the java. sql .Driver inter- face. This would be available from the documentation, for the JDBC driver being used. So the first step in an application using JDBC for connecting to a database would appear something like<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Class.forName(&#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Note: Use the appropriate class name in the above call, depending on the JDBC driver we are using.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The second step is to establish a connection \u00a0to \u00a0the \u00a0database. \u00a0To \u00a0get \u00a0an \u00a0instance \u00a0of \u00a0Connection, \u00a0we use the static method getConnection() in the DriverManager class, as shown below:<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<p>&nbsp;<\/p>\n<p>Connection con = DriverManager.getConnection(&#8220;jdbc:odbc:&lt;dsn&gt;&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The static method getConnection() takes as parameter a String which is the jdbc-url. The jdbc-url is of the form &#8220;jdbc:&lt;sub-protocol&gt;:&#8230;&#8221;. This string is dependent on the driver used. Different drivers would have different requirements. The jdbc-url syntax has lot of things which depend on the JDBC driver being used. When we call the getConnection() method, the DriverManager would check each of the registered drivers, if they support the specified jdbc- url. The driver instance which accepts such a url would return the Connection to the DriverManager, which is in turn returned to the application by the DriverManager. Let us say, we would like to connect to the PostgreSQL database, and we have downloaded the pg73jdbc3.jar file, which contains the jdbc driver for connecting to PostgreSQL ver.<\/p>\n<p>7.3. Then the sequence would be something like:<\/p>\n<p>&nbsp;<\/p>\n<p>Class.forName(&#8220;org.postgresql.Driver&#8221;) ;<\/p>\n<p>Connection con = DriverManager.<\/p>\n<p>getConnection(&#8220;jdbc:postgresql:\/\/&lt;host&gt;\/&lt;database&gt;&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The static method getConnection() in DriverManager is overloaded and we may use it to separately supply \u00a0the \u00a0authentication \u00a0info.\u00a0 like<\/p>\n<p>&nbsp;<\/p>\n<p>public static Connection getConnection(String jdbcurl)<\/p>\n<p>public static Connection getConnection(String jdbcurl, String user,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String password)<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Interacting with the database<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">As mentioned in the above section there are mainly three \u00a0kinds \u00a0of \u00a0interactions. \u00a0Executing \u00a0SQL, executing pre-compiled SQL and executing stored-procedures and functions. \u00a0Let \u00a0us \u00a0look \u00a0at \u00a0how \u00a0we use the JDBC API to carry out each of these tasks.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Executing SQL using Statement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">Once we have the instance of Connection, and we want to execute a SQL, then we may use the createStatement() on the Connection instance to obtain the instance of Statement. The createStatement() method in Connection interface is overloaded as shown below:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">Statement createStatement()<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">Statement createStatement(int resultSetType, int resultSetConcurrency)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">We have the first method without parameters, and the second method takes two integer parameters. The int values for resultSetType and resultSetConcurrency are defined in the java.sql.ResultSet interface. When we use a select query to get results from a database, it would return an instance of ResultSet. The Statement instance is used for executing SQL. So the two parameters define the type of resultSets returned by this Statement when we execute any select query \u00a0using \u00a0this \u00a0statement. \u00a0The valid values for resultSetType are:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ResultSet.TYPE_FORWARD_ONLY ResultSet.TYPE_SCROLL_SENSITIVE<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ResultSet.TYPE_SCROLL_INSENSITIVE<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">The valid values of resultSetConcurrency are:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ResultSet.CONCUR_READ_ONLY ResultSet.CONCUR_UPDATABLE<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">By default the ResultSet type is forward only and read only. Let \u00a0us \u00a0look \u00a0at \u00a0how \u00a0to \u00a0execute \u00a0an \u00a0SQL. Once we have an instance of\u00a0 Statement. The Statement interface\u00a0 has the following \u00a0methods\u00a0\u00a0 for executing a SQL.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">Boolean execute(String sql) int executeUpdate(String sql)<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ResultSet executeQuery(String sql)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">All the three methods can be used for executing any kind of SQL. The SQL to be executed is passed as the parameter. All the methods have a different return type. Depending on the type of SQL to be executed, we choose the method with the appropriate return type. The execute() method is more commonly used for executing the DDL statements like CREATE and DROP. The executeUpdate() method is more commonly used for executing the DML statements like INSERT, UPDATE and DELETE. This method returns the number of rows affected by the DML statement. The executeQuery() method is more commonly used for executing the SELECT statement. It returns an instance of ResultSet, which contains the results of the SELECT query.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The instance of Statement can be used for executing multiple SQL \u00a0statements \u00a0in \u00a0a \u00a0batch. \u00a0The Statement interface has methods to \u00a0manage \u00a0SQL \u00a0statements \u00a0into \u00a0a \u00a0batch, \u00a0and \u00a0a \u00a0method \u00a0to \u00a0execute all the SQL statements which have been added into the batch. The following \u00a0methods \u00a0are \u00a0used \u00a0for batch \u00a0execution.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void addBatch(String sql) public void clearBatch( )<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public int[] executeBatch( )<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">The size of the int [] returned by executeBatch() method is the number of \u00a0SQL \u00a0statements \u00a0in \u00a0the batch, and it contains an int value corresponding to each SQL statement executed from the batch. The int value is the number of rows affected by the DML statement, and in case of SELECT statement it contains the value of -1.\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Executing a precompiled SQL using PreparedStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">An instance of Statement is used to execute any kind of SQL. There is a \u00a0sub-interface \u00a0of \u00a0Statement called PreparedStatement, which is used to execute precompiled SQL. An instance of PreparedStatement is also created from the instance of Connection by using the following methods of Connection\u00a0\u00a0 interface:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">PreparedStatement prepareStatement(String sql)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">P<\/span><span style=\"text-align: initial; font-size: 1em;\">reparedStatement prepareStatement(String sql, int rstype, int rsconcur)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Here unlike the Statement the SQL is specified at the time of creating the in- stance of PreparedStatement. The SQL, which is passed as parameter can have place holders which are introduced by using the \u2019 ?\u2019 character within the SQL string passed as parameter. These place holders are referred to as parameters for the PreparedStatement.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">PreparedStatement ps = con.prepareStatement(&#8220;select * from account where acno=? and type = ?&#8221;);<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">PreparedStatement ps = con.prepareStatement(&#8220;insert into account values(?,?,?,?)&#8221;);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The first \u2019 ?\u2019 in the SQL string is the first parameter of the PreparedStatement, and so on. So, \u00a0the number of parameters in a\u00a0 PreparedSTatement will be\u00a0 the number of \u2019 ?\u2019\u00a0 characters used in the SQL string. The parameters can only be used in place for values in the SQL statement. These \u00a0parameter values need to be setup before the precompiled SQL corresponding to the PreparedStatement can be executed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now the PreparedStatement interface has additional methods for executing the SQL. These are \u00a0the same three methods as in Statement interface, \u00a0but \u00a0without \u00a0any \u00a0parameters. \u00a0In \u00a0Statement \u00a0interface the parameter\u00a0 to the\u00a0 three execute methods was\u00a0 the SQL to be\u00a0 executed. In case\u00a0 of PreparedStatement this has already been specified at the time of creating the instance using the parameter in \u00a0the \u00a0prepareStatement() \u00a0method.\u00a0 So,\u00a0 the \u00a0methods\u00a0 for \u00a0executing\u00a0 the \u00a0precompiled \u00a0SQL \u00a0in a PreparedStatement are:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean execute() int executeUpdate()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">ResultSet executeQuery()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Executing Stored Procedures and functions using CallableStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is also\u00a0 a sub-interface\u00a0 of PreparedStatement\u00a0 called the CallableStatement, this\u00a0 is used to execute stored procedures and functions. The instance of CallableStatement is created by the method on Connection as<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement prepareCall(String callstatement)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement prepareCall(String callstatement,int rstype,int rsconcur)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The CallableStatement inherits methods from the PreparedStatement and some more additional methods. let us look at how to create an instance of CallableStatement. In the above methods \u00a0the syntax for the callstatement is:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">{ [?] = call &lt;proc-name or function name&gt; (?,&#8230;} }<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">if it is a stored procedure which is to be invoked we create CallableStatement as<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement cs = con.prepareCall(&#8220;{ call myproc(?,?,?) }&#8221;);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to execute a stored procedure or<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">CallableStatement cs = con.prepareCall(&#8220;{ ? = call myfunctino(?, ?, ?) }&#8221;);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to execute a function, which returns some value.<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify;\"><strong>In Summary:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\">Java applications use JDBC to access any kind of databases. The JDBC is a standard way of interaction with different databases, available from different vendors. The JDBC is defined using the set of interfaces and classes available in the java . sql package. There are various implementations of these standard for different types of database. These JDBC drivers are categorized into four types based on how they have been implemented.<\/li>\n<li style=\"text-align: justify;\">The Type-1 driver is the one which talks to the ODBC driver available for connected to a database. This driver makes use of native calls in order to make the ODBC calls. The type 2 driver is the one which directly talks to the database, but is not a pure Java driver, it makes use of native libraries available from the database vendor to make connection and talk to the databse. The type 3 driver is a pure Java driver, unlike the type 1 and type 2. but it does not directly talk to the database, instead it talks to some network absed service which in turn talks to the database. and type 4 driver is the one which is a pure Java driver directly communicating with the database.<\/li>\n<li style=\"text-align: justify;\">The first task in interacting with database for a Java application is to obtain an instance of Connection. This is then used for all other interactions with the database. The instance of a class implementing the Driver interface is used to obtain instance of Connection. The DriverManager class can manage various implementations of Driver, which are first required to be registered. Whenever any implementation of Driver is loaded, it would register with the DriverManager. The DriverManager has method getConnection(), which can be used to obtain a connection for a given url. The DriverManager will select the appropriate instance of Driver and use it to return the required Connection.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on JDBC1<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/sOEy-XPlatg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><strong>Suggested Reading:<\/strong><\/div>\n<div><\/div>\n<div>1. Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/div>\n<div>2. The class of JAVA by Pravin Jain, Pearson Education.<\/div>\n<div>3. Database programming with JDBC and Java by George Reese. O&#8217;Reilly Media<\/div>\n<div>4. https:\/\/docs.oracle.com\/javase\/tutorial\/jdbc\/basics\/<\/div>\n","protected":false},"author":4,"menu_order":8,"template":"","meta":{"_acf_changed":false,"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-59","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":22,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/59\/revisions"}],"predecessor-version":[{"id":395,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/59\/revisions\/395"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/59\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=59"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=59"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}