{"id":231,"date":"2018-07-27T05:20:58","date_gmt":"2018-07-27T05:20:58","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=231"},"modified":"2018-08-02T08:26:34","modified_gmt":"2018-08-02T08:26:34","slug":"jdbc-introduction","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/jdbc-introduction\/","title":{"rendered":"JDBC \u2013 INTRODUCTION"},"content":{"raw":"<div>\r\n\r\n<strong>\u00a0 \u00a0 JDBC<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">JDBC stands for Java Database Connectivity. It is used for accessing databases from Java applications.Java is a very standardized programming language, but there are many versions of SQL databases.<\/p>\r\n&nbsp;\r\n\r\n<strong>ODBC vs. JDBC<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Open DataBase Connectivity API can be used to access relational databases. ODBC can be used with Java applied as the form of JDBC-ODBC Bridge. ODBC is not appropriate for direct use from the Java programming language. It uses C interface. There occur a number of drawbacks in the security, implementation, robustness, automatic portability during the calls from Java to native C code.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Programs that are developed using Java-JDBC API are platform and vendor independent. JDBC involves the logical quote of \u201cwrite once, compile once, run anywhere\u201d. It provides a standard API for tool or database developers and makes it possible to write database applications using pure Java API. JDBC driver manager and\u00a0 JDBC drivers provide the bridge between the database and Java world.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong> JDBC Architecture<\/strong>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-232 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135.png\" alt=\"\" width=\"619\" height=\"113\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 26.1 Architecture of JDBC<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 26.1 shows the architecture of the working of JDBC. The application program written in Java code calls the JDBC library. JDBC loads a driver and through the driver, the application code talks to a particular database. A machine can have more than one driver and more than one database. The ideal thing about JDBC is that it can change database engines without changing any application code.<\/p>\r\n&nbsp;\r\n\r\n<strong>JDBC Drivers<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are four types of JDBC Drivers listed below,\r\n\r\n&nbsp;\r\n\r\n\u2022 Type I: Bridge\r\n\r\n\u2022 Type II: Native\r\n\r\n\u2022 Type III: Middleware\r\n\r\n\u2022 Type IV: Pure\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 JDBC Driver Types<\/strong>\r\n\r\n&nbsp;\r\n\r\nFigure 26.2 shows the types of Drivers.\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-233 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136.png\" alt=\"\" width=\"529\" height=\"273\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 26.2 Types of JDBC Drivers<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Type I driver is an JDBC-ODBC bridge driver that uses ODBC driver to connect to the database. This JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls.<\/p>\r\n<p style=\"text-align: justify\">The Type II drivers are the Native API driver that uses the client-side libraries of the database. This driver converts JDBC method calls into native calls of the database API. It is not written entirely in Java.<\/p>\r\n<p style=\"text-align: justify\">The Type III drivers are called the Network Protocol driver that uses middleware which is an application server that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in Java.<\/p>\r\n<p style=\"text-align: justify\">The Type IV drivers are the thin driver that converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language.<\/p>\r\n&nbsp;\r\n\r\n<strong>Type I Drivers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">These drivers uses bridging technology. They require installation\/configuration on client machines.These drivers are not recommended for Web. Example of Type I Driver is ODBC Bridge.<\/p>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Type II Drivers<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">These drivers are native API drivers. They require installation\/configuration on client machines. Type II drivers are used to leverage existing CLI libraries and are usually not thread-safe, mostly obsolete now. Examples include Intersolv Oracle Driver, WebLogic drivers.<\/p>\r\n&nbsp;\r\n\r\n<strong>Type III Drivers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">These drivers call middleware server, usually on database host. They are very flexible and allow access to multiple databases using one driver. There is only the need to download one driver, but it is another server application to install and maintain. An example of it is Symantec DBAnywhere.<\/p>\r\n&nbsp;\r\n\r\n<strong>Type IV Drivers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There is full 100% percentile Pure Java referred as the Holy Grail. These drivers use Java networking libraries to talk directly to database engines. A disadvantage here is the need to download a new driver for each database engine. Examples include Oracle, mySQL etc.<\/p>\r\n&nbsp;\r\n\r\nLet us now look in detail about JDBC Drivers.\r\n\r\n&nbsp;\r\n\r\n<strong>JDBC Driver<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe Driver class names for each of the database is given below,\r\n\r\nIn Oracle, the driver class name can be given as,\r\n\r\noracle.jdbc.driver.OracleDriver\r\n\r\nIn MySQL, the driver class name can be given as,\r\n\r\ncom.mysql.jdbc.Driver\r\n\r\nIn MS SQL, the driver class name can be given as,\r\n\r\ncom.microsoft.jdbc.sqlserver.SQLServerDriver\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 <\/strong>\r\n\r\n<strong>JDBC Limitations\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The limitations of working with JDBC is that there are no scrolling cursors available and no bookmarks can be made while processing the records thereby it allows only sequential processing.<\/p>\r\n&nbsp;\r\n\r\n<strong>Working with JDBC<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Package to be Imported<\/strong>\r\n\r\n&nbsp;\r\n\r\nJDBC is implemented via classes in the java.sql package.\r\n\r\nimport java.sql.*\r\n\r\n&nbsp;\r\n\r\n<strong>Loading a Driver Directly<\/strong>\r\n\r\n&nbsp;\r\n\r\nDriver d = new foo.bar.MyDriver();\r\n\r\nConnection c = d.connect(...);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can create a Driver and load the driver. Using the Driver object we need to create a Connection object . This is not recommended but instead we can use a Driver Manager but this is useful if we know that we need this particular driver.<\/p>\r\n&nbsp;\r\n\r\n<strong>DriverManager<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Driver Manager tries all the drivers. It uses the first one that works. When a driver class is first loaded, it registers itself with the Driver Manager. Therefore, to register a driver, we need to just load the driver.<\/p>\r\n&nbsp;\r\n\r\n<strong>Registering a Driver<\/strong>\r\n\r\n&nbsp;\r\n\r\nEither of the below two methods can be used to register a driver.\r\n\r\n&nbsp;\r\n\r\n1. Statically load driver\r\n\r\nClass.forName(\u201cfoo.bar.MyDriver\u201d);\r\n\r\nConnection c = DriverManager.getConnection(...);\r\n\r\n2. Use the jdbc.drivers system property\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0JDBC Object Classes<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe classes that are used from the imported java.sql.* package is listed below.\r\n\r\n&nbsp;\r\n\r\n\u2022 DriverManager\r\n\r\nThis class is used for loading and choosing the drivers.\r\n\r\n\u2022 Driver\r\n\r\nThis class performs the connection to actual database.\r\n\r\n\u2022 Connection\r\n<p style=\"text-align: justify\">\u00a0 \u00a0The Connection class creates the connectivity through a series of SQL statements to and from the database.<\/p>\r\n\u2022 Statement\r\n\r\nThe Statement object created using this class represents a single SQL statement.\r\n\r\n\u2022 ResultSet\r\n\r\nThe ResultSet object represents a set of the records returned from a statement.\r\n\r\n&nbsp;\r\n\r\n<strong>JDBC URL's<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe format of JDBC URL is given below that has components like subprotocol and source.\r\n\r\n&nbsp;\r\n\r\nFor example,\r\n\r\n&nbsp;\r\n\r\njdbc:subprotocol:source\r\n\r\nHere each driver has its own subprotocol . Each subprotocol has its own syntax for the source.\r\n\r\nThe URL for Type I Driver which is an ODBC Bridge is given below.\r\n\r\njdbc:odbc:DataSource\r\n\r\nExample: jdbc:odbc:jnf\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The URL to connect to MySQL database is given below,<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">jdbc:mysql:\/\/host[:port]\/database<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Example: jdbc:mysql:\/\/foo.nowhere.com:4333\/jnf<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0DriverManager<\/strong>\r\n\r\n&nbsp;\r\n\r\nConnection getConnection (String url, String user, String password)\r\n<p style=\"text-align: justify\">This connects to given JDBC URL with given user name and password and returns a Connection object. It can throw java.sql.SQLException when connection cannot be established.<\/p>\r\n&nbsp;\r\n\r\n<strong>Connection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Connection represents a session with a specific database. Within the context of a Connection, SQL statements are executed and results are returned. A program can have multiple connections to a database. A connection provides \u201cmetadata\u201d -- information about the database, tables, and fields, along with the methods to deal with transactions.<\/p>\r\n&nbsp;\r\n\r\n<strong>Obtaining a Connection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The snippet code for obtaining the connection with JDBC-ODBC bridge is given below. First we need to dynamically load and register the driver. Then establish a connection by creating a Connection object using the DriverManager class.<\/p>\r\n&nbsp;\r\n\r\nString url = \"jdbc:odbc:jnf\";\r\n\r\ntry {\r\n\r\nClass.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");\r\n\r\nConnection con = DriverManager.getConnection(url);\r\n\r\n}\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch (ClassNotFoundException e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{ e.printStackTrace(); } catch (SQLException e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{ e.printStackTrace(); }<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong>Connection Methods<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the context of a Connection, SQL statements are executed and results are returned. Statement objects can be created using createStatement() method on the Connection object. Similarly PreparedStatement object can be created when we use prepareStatement() method on the Connection object and CallableStatement object can be created when we use prepareCall() method on the Connection object. The syntax of these methods are given below.<\/p>\r\n&nbsp;\r\n\r\n1. Statement createStatement()\r\n\r\nThis returns a new Statement object.\r\n\r\n2. PreparedStatement prepareStatement(String sql)\r\n\r\nThis returns a new PreparedStatement object.\r\n\r\n3. CallableStatement prepareCall(String sql)\r\n\r\nThis returns a new CallableStatement object.\r\n\r\n&nbsp;\r\n\r\n<strong>Statement<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Statement object is used for executing a static SQL statement and obtaining the results produced by it.<\/p>\r\n&nbsp;\r\n\r\n<strong>Statement Methods<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are three methods which works with the Statement object.\r\n<p style=\"text-align: justify\">ResultSet executeQuery(String)<\/p>\r\n<p style=\"text-align: justify\">This method executes an SQL SELECT statement that returns a single ResultSet.<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">int executeUpdate(String)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">This method is used to execute an SQL INSERT, UPDATE or DELETE statement and returns the number of rows changed.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">boolean execute(String)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">This method is used to execute an SQL statement that may return multiple results.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 <\/strong>\r\n\r\n<strong>ResultSet<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A ResultSet provides access to a table of data generated by executing a Statement. Only one ResultSet per Statement can be open at once. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The 'next()' method is used to move the cursor to the next row.<\/p>\r\n&nbsp;\r\n\r\n<strong>ResultSet Methods<\/strong>\r\n\r\n&nbsp;\r\n\r\nBelow is the list of methods to work with the ResultSet object.\r\n<ul>\r\n \t<li>boolean next()<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method activates the next row, the first call to next() activates the first row and returns false if there are no more rows.<\/p>\r\n\r\n<ul>\r\n \t<li>void close()<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0This method disposes the ResultSet. It allows you to re-use the statement that created it automatically called by most statement methods.<\/p>\r\n\r\n<ul>\r\n \t<li>Type getType(int columnIndex)<\/li>\r\n<\/ul>\r\nThis method returns the given field as the given type fields indexed starting at 1 (not 0).\r\n<ul>\r\n \t<li>Type getType(String columnName)<\/li>\r\n<\/ul>\r\nThis method performs the same function as before, but uses name of field and less efficient.\r\n<ul>\r\n \t<li>int findColumn(String columnName)<\/li>\r\n<\/ul>\r\nThis method looks up column index given column name.\r\n<ul>\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">String getString(int columnIndex)<\/span><\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li>boolean getBoolean(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a boolean in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li>byte getByte(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as a byte in the Java programming language.<\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li>short getShort(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a short in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li>\u00a0int getInt(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.<\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li>\u00a0long getLong(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a long in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li>\u00a0float getFloat(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a float in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li>double getDouble(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a double in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li>\u00a0 Date getDate(int columnIndex)<\/li>\r\n<\/ul>\r\n<\/div>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">Time getTime(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.<\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li>Timestamp getTimestamp(int columnIndex)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Timestamp object in the Java programming language.<\/p>\r\n&nbsp;\r\n\r\n<strong>isNull<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In SQL, NULL means the field is empty. It is not the same as 0 or \u201c\u201d. In JDBC, you must explicitly ask if a field is null by calling,<\/p>\r\n<p style=\"text-align: justify\">ResultSet.isNull(column)<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module gave an introduction about JDBC API that defines how a client may access a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Database\">database. <\/a>It also discussed about the JDBC Architecture that uses a driver manager and database-specific drivers to provide a transparent connectivity to heterogeneous databases. The module also provides us with a basic understanding about the design of JDBC API, the classes, methods and relations in them.<\/p>\r\n&nbsp;\r\n\r\n<strong>References<\/strong>\r\n<ul>\r\n \t<li><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/ResultSet.html\">https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/ResultSet.html<\/a><\/li>\r\n \t<li><em>www.javatpoint.com \/jdb c-driver<\/em><\/li>\r\n<\/ul>","rendered":"<div>\n<p><strong>\u00a0 \u00a0 JDBC<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">JDBC stands for Java Database Connectivity. It is used for accessing databases from Java applications.Java is a very standardized programming language, but there are many versions of SQL databases.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ODBC vs. JDBC<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Open DataBase Connectivity API can be used to access relational databases. ODBC can be used with Java applied as the form of JDBC-ODBC Bridge. ODBC is not appropriate for direct use from the Java programming language. It uses C interface. There occur a number of drawbacks in the security, implementation, robustness, automatic portability during the calls from Java to native C code.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Programs that are developed using Java-JDBC API are platform and vendor independent. JDBC involves the logical quote of \u201cwrite once, compile once, run anywhere\u201d. It provides a standard API for tool or database developers and makes it possible to write database applications using pure Java API. JDBC driver manager and\u00a0 JDBC drivers provide the bridge between the database and Java world.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong> JDBC Architecture<\/strong><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-232 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135.png\" alt=\"\" width=\"619\" height=\"113\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135.png 619w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135-300x55.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135-65x12.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135-225x41.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-135-350x64.png 350w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 26.1 Architecture of JDBC<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 26.1 shows the architecture of the working of JDBC. The application program written in Java code calls the JDBC library. JDBC loads a driver and through the driver, the application code talks to a particular database. A machine can have more than one driver and more than one database. The ideal thing about JDBC is that it can change database engines without changing any application code.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>JDBC Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are four types of JDBC Drivers listed below,<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Type I: Bridge<\/p>\n<p>\u2022 Type II: Native<\/p>\n<p>\u2022 Type III: Middleware<\/p>\n<p>\u2022 Type IV: Pure<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 JDBC Driver Types<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Figure 26.2 shows the types of Drivers.<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-233 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136.png\" alt=\"\" width=\"529\" height=\"273\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136.png 529w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136-225x116.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-136-350x181.png 350w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 26.2 Types of JDBC Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Type I driver is an JDBC-ODBC bridge driver that uses ODBC driver to connect to the database. This JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls.<\/p>\n<p style=\"text-align: justify\">The Type II drivers are the Native API driver that uses the client-side libraries of the database. This driver converts JDBC method calls into native calls of the database API. It is not written entirely in Java.<\/p>\n<p style=\"text-align: justify\">The Type III drivers are called the Network Protocol driver that uses middleware which is an application server that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in Java.<\/p>\n<p style=\"text-align: justify\">The Type IV drivers are the thin driver that converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Type I Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">These drivers uses bridging technology. They require installation\/configuration on client machines.These drivers are not recommended for Web. Example of Type I Driver is ODBC Bridge.<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Type II Drivers<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">These drivers are native API drivers. They require installation\/configuration on client machines. Type II drivers are used to leverage existing CLI libraries and are usually not thread-safe, mostly obsolete now. Examples include Intersolv Oracle Driver, WebLogic drivers.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Type III Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">These drivers call middleware server, usually on database host. They are very flexible and allow access to multiple databases using one driver. There is only the need to download one driver, but it is another server application to install and maintain. An example of it is Symantec DBAnywhere.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Type IV Drivers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There is full 100% percentile Pure Java referred as the Holy Grail. These drivers use Java networking libraries to talk directly to database engines. A disadvantage here is the need to download a new driver for each database engine. Examples include Oracle, mySQL etc.<\/p>\n<p>&nbsp;<\/p>\n<p>Let us now look in detail about JDBC Drivers.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>JDBC Driver<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The Driver class names for each of the database is given below,<\/p>\n<p>In Oracle, the driver class name can be given as,<\/p>\n<p>oracle.jdbc.driver.OracleDriver<\/p>\n<p>In MySQL, the driver class name can be given as,<\/p>\n<p>com.mysql.jdbc.Driver<\/p>\n<p>In MS SQL, the driver class name can be given as,<\/p>\n<p>com.microsoft.jdbc.sqlserver.SQLServerDriver<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 <\/strong><\/p>\n<p><strong>JDBC Limitations\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The limitations of working with JDBC is that there are no scrolling cursors available and no bookmarks can be made while processing the records thereby it allows only sequential processing.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Working with JDBC<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Package to be Imported<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>JDBC is implemented via classes in the java.sql package.<\/p>\n<p>import java.sql.*<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Loading a Driver Directly<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Driver d = new foo.bar.MyDriver();<\/p>\n<p>Connection c = d.connect(&#8230;);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can create a Driver and load the driver. Using the Driver object we need to create a Connection object . This is not recommended but instead we can use a Driver Manager but this is useful if we know that we need this particular driver.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>DriverManager<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Driver Manager tries all the drivers. It uses the first one that works. When a driver class is first loaded, it registers itself with the Driver Manager. Therefore, to register a driver, we need to just load the driver.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Registering a Driver<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Either of the below two methods can be used to register a driver.<\/p>\n<p>&nbsp;<\/p>\n<p>1. Statically load driver<\/p>\n<p>Class.forName(\u201cfoo.bar.MyDriver\u201d);<\/p>\n<p>Connection c = DriverManager.getConnection(&#8230;);<\/p>\n<p>2. Use the jdbc.drivers system property<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0JDBC Object Classes<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>The classes that are used from the imported java.sql.* package is listed below.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 DriverManager<\/p>\n<p>This class is used for loading and choosing the drivers.<\/p>\n<p>\u2022 Driver<\/p>\n<p>This class performs the connection to actual database.<\/p>\n<p>\u2022 Connection<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0The Connection class creates the connectivity through a series of SQL statements to and from the database.<\/p>\n<p>\u2022 Statement<\/p>\n<p>The Statement object created using this class represents a single SQL statement.<\/p>\n<p>\u2022 ResultSet<\/p>\n<p>The ResultSet object represents a set of the records returned from a statement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>JDBC URL&#8217;s<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The format of JDBC URL is given below that has components like subprotocol and source.<\/p>\n<p>&nbsp;<\/p>\n<p>For example,<\/p>\n<p>&nbsp;<\/p>\n<p>jdbc:subprotocol:source<\/p>\n<p>Here each driver has its own subprotocol . Each subprotocol has its own syntax for the source.<\/p>\n<p>The URL for Type I Driver which is an ODBC Bridge is given below.<\/p>\n<p>jdbc:odbc:DataSource<\/p>\n<p>Example: jdbc:odbc:jnf<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The URL to connect to MySQL database is given below,<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">jdbc:mysql:\/\/host[:port]\/database<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Example: jdbc:mysql:\/\/foo.nowhere.com:4333\/jnf<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0DriverManager<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Connection getConnection (String url, String user, String password)<\/p>\n<p style=\"text-align: justify\">This connects to given JDBC URL with given user name and password and returns a Connection object. It can throw java.sql.SQLException when connection cannot be established.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Connection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Connection represents a session with a specific database. Within the context of a Connection, SQL statements are executed and results are returned. A program can have multiple connections to a database. A connection provides \u201cmetadata\u201d &#8212; information about the database, tables, and fields, along with the methods to deal with transactions.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Obtaining a Connection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The snippet code for obtaining the connection with JDBC-ODBC bridge is given below. First we need to dynamically load and register the driver. Then establish a connection by creating a Connection object using the DriverManager class.<\/p>\n<p>&nbsp;<\/p>\n<p>String url = &#8220;jdbc:odbc:jnf&#8221;;<\/p>\n<p>try {<\/p>\n<p>Class.forName(&#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;);<\/p>\n<p>Connection con = DriverManager.getConnection(url);<\/p>\n<p>}<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch (ClassNotFoundException e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{ e.printStackTrace(); } catch (SQLException e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{ e.printStackTrace(); }<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong>Connection Methods<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the context of a Connection, SQL statements are executed and results are returned. Statement objects can be created using createStatement() method on the Connection object. Similarly PreparedStatement object can be created when we use prepareStatement() method on the Connection object and CallableStatement object can be created when we use prepareCall() method on the Connection object. The syntax of these methods are given below.<\/p>\n<p>&nbsp;<\/p>\n<p>1. Statement createStatement()<\/p>\n<p>This returns a new Statement object.<\/p>\n<p>2. PreparedStatement prepareStatement(String sql)<\/p>\n<p>This returns a new PreparedStatement object.<\/p>\n<p>3. CallableStatement prepareCall(String sql)<\/p>\n<p>This returns a new CallableStatement object.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Statement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Statement object is used for executing a static SQL statement and obtaining the results produced by it.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Statement Methods<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are three methods which works with the Statement object.<\/p>\n<p style=\"text-align: justify\">ResultSet executeQuery(String)<\/p>\n<p style=\"text-align: justify\">This method executes an SQL SELECT statement that returns a single ResultSet.<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">int executeUpdate(String)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">This method is used to execute an SQL INSERT, UPDATE or DELETE statement and returns the number of rows changed.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">boolean execute(String)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">This method is used to execute an SQL statement that may return multiple results.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 <\/strong><\/p>\n<p><strong>ResultSet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A ResultSet provides access to a table of data generated by executing a Statement. Only one ResultSet per Statement can be open at once. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The &#8216;next()&#8217; method is used to move the cursor to the next row.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ResultSet Methods<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Below is the list of methods to work with the ResultSet object.<\/p>\n<ul>\n<li>boolean next()<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method activates the next row, the first call to next() activates the first row and returns false if there are no more rows.<\/p>\n<ul>\n<li>void close()<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0This method disposes the ResultSet. It allows you to re-use the statement that created it automatically called by most statement methods.<\/p>\n<ul>\n<li>Type getType(int columnIndex)<\/li>\n<\/ul>\n<p>This method returns the given field as the given type fields indexed starting at 1 (not 0).<\/p>\n<ul>\n<li>Type getType(String columnName)<\/li>\n<\/ul>\n<p>This method performs the same function as before, but uses name of field and less efficient.<\/p>\n<ul>\n<li>int findColumn(String columnName)<\/li>\n<\/ul>\n<p>This method looks up column index given column name.<\/p>\n<ul>\n<li><span style=\"text-align: initial;font-size: 1em\">String getString(int columnIndex)<\/span><\/li>\n<\/ul>\n<\/div>\n<div>\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.<\/p>\n<ul>\n<li>boolean getBoolean(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a boolean in the Java programming language.<\/p>\n<ul>\n<li>byte getByte(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as a byte in the Java programming language.<\/p>\n<ul style=\"text-align: justify\">\n<li>short getShort(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a short in the Java programming language.<\/p>\n<ul>\n<li>\u00a0int getInt(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.<\/p>\n<ul style=\"text-align: justify\">\n<li>\u00a0long getLong(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a long in the Java programming language.<\/p>\n<ul>\n<li>\u00a0float getFloat(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a float in the Java programming language.<\/p>\n<ul>\n<li>double getDouble(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a double in the Java programming language.<\/p>\n<ul>\n<li>\u00a0 Date getDate(int columnIndex)<\/li>\n<\/ul>\n<\/div>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.<\/p>\n<ul>\n<li style=\"text-align: justify\">Time getTime(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0This method retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.<\/p>\n<ul style=\"text-align: justify\">\n<li>Timestamp getTimestamp(int columnIndex)<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 This method retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Timestamp object in the Java programming language.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>isNull<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In SQL, NULL means the field is empty. It is not the same as 0 or \u201c\u201d. In JDBC, you must explicitly ask if a field is null by calling,<\/p>\n<p style=\"text-align: justify\">ResultSet.isNull(column)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module gave an introduction about JDBC API that defines how a client may access a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Database\">database. <\/a>It also discussed about the JDBC Architecture that uses a driver manager and database-specific drivers to provide a transparent connectivity to heterogeneous databases. The module also provides us with a basic understanding about the design of JDBC API, the classes, methods and relations in them.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>References<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/ResultSet.html\">https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/ResultSet.html<\/a><\/li>\n<li><em>www.javatpoint.com \/jdb c-driver<\/em><\/li>\n<\/ul>\n","protected":false},"author":4,"menu_order":24,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-231","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":12,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/231\/revisions"}],"predecessor-version":[{"id":580,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/231\/revisions\/580"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/231\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=231"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=231"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}