{"id":11557,"date":"2023-01-04T12:48:49","date_gmt":"2023-01-04T12:48:49","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11557"},"modified":"2023-08-21T06:24:58","modified_gmt":"2023-08-21T06:24:58","slug":"jdbc-program-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/","title":{"rendered":"JDBC Program in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg\" alt=\"\" \/><\/p>\n<h2>What is JDBC in Java?<\/h2>\n<p>JDBC in Java stands for Java Database Connectivity, representing a progression from ODBC (Open Database Connectivity). JDBC serves as a standardized API specification designed to facilitate the transfer of data from the frontend to the backend. This API encompasses Java-written classes and interfaces, essentially serving as a conduit \u2013 distinct from the Java programming concept of an interface \u2013 that bridges your Java program and databases. This bridge establishes a connection between the two entities, enabling programmers to transmit data from Java code and securely store it within the database for subsequent utilization.<\/p>\n<ul>\n<li>JDBC-ODBC Bridge Driver,<\/li>\n<li>Native Driver,<\/li>\n<li>Network Protocol Driver, and<\/li>\n<li>Thin Driver<\/li>\n<\/ul>\n<p>Steps to implement JDBC in Java<\/p>\n<h2>1) Import Packages<\/h2>\n<p>We need to import the existing packages for the JDBC program so that the required APIs are available for the program.<br \/>\n<strong>Irrespective of the JDBC Driver, add the following import statement in the Java program.<\/strong><\/p>\n<pre><code>import java.sql.*;\nImport the other classes based on the functionality which you will use in the program. <\/code><\/pre>\n<h2>2) Load Driver<\/h2>\n<p>First, we should load\/register the driver in the program before connecting to the Database. You need to register it only once per database in the program.<br \/>\nWe can load the driver in the following 2 ways:<\/p>\n<ol>\n<li><strong>Class.forName()<\/strong><\/li>\n<li><strong>DriverManager.registerDriver()<\/strong><\/li>\n<\/ol>\n<h2>3) Establish Connection<\/h2>\n<p>After loading the driver, the next step is to create and establish the connection. Once required, packages are imported and drivers are loaded and registered, then we can go for establishing a Database connection.<\/p>\n<p>DriverManager class has the getConnection method, we will use this method to get the connection with Database. To call getConnection() method, we need to pass 3 parameters. The 3 parameters are string data type URL, a username, and a password to access the database.<\/p>\n<p><strong>The getConnection() method is an overloaded method. The 2 methods are:<\/strong><\/p>\n<ul>\n<li><strong>getConnection(URL,username,password);<\/strong> \u2013 It has 3 parameters URL, username, password.<\/li>\n<li><strong>getConnection(URL);<\/strong> \u2013 It has only one parameter. URL has a username and password also.<\/li>\n<\/ul>\n<h2>4) Create And Execute Statement<\/h2>\n<p>Once the connection has established, we can interact with the connected Database. First, we need to create the statement to perform the SQL query and then execute the statement.<\/p>\n<h3>(i) Create Statement<\/h3>\n<p>Now we will create the statement object that runs the query with the connected database. We use the createStatement method of the Connection class to create the query.<\/p>\n<p><strong>There are 3 statement interfaces are available in the java.sql package. These are explained below:<\/strong><\/p>\n<p><strong>a) Statement<\/strong><br \/>\nThis interface is used to implement simple SQL statements with no parameter. It returns the ResultSet object.<\/p>\n<pre><code>Statement statemnt1 = conn.createStatement();<\/code><\/pre>\n<p><strong>b) PreparedStatement<\/strong><br \/>\nThis PreparedStatement interface extends the Statement interface. So, it has more features than the Statement interface. It is used to implement parameterized and precompiled SQL statements. The performance of the application increases because it compiles the query only once.<\/p>\n<p>It is easy to reuse this interface with a new parameter. It supports the IN parameter. Even we can use this statement without any parameter.<\/p>\n<pre><code>String select_query = \u201cSelect * from states where state_id = 1\u201d;\nPreparedStatement prpstmt = conn.prepareStatement(select_query);<\/code><\/pre>\n<p><strong>c) CallableStatement<\/strong><br \/>\nCallableStatement interface extends the PreparedStatement interface. So, it has more features than the PreparedStatement interface. It is used to implement a parameterized SQL statement that invokes procedure or function in the database. A stored procedure works like a method or function in a class. It supports the IN and OUT parameters.<\/p>\n<p>The CallableStatement instance is created by calling the prepareCall method of the Connection object.<\/p>\n<pre><code>CallableStatementcallStmt = con.prepareCall(\"{call procedures(?,?)}\");<\/code><\/pre>\n<h3>(ii) Execute The Query<\/h3>\n<p><strong>There are 4 important methods to execute the query in Statement interface. These are explained below:<\/strong><\/p>\n<ul>\n<li>ResultSet executeQuery(String sql)<\/li>\n<li>int executeUpdate(String sql)<\/li>\n<li>boolean execute(String sql)<\/li>\n<li>int []executeBatch()<\/li>\n<\/ul>\n<p><strong>a) ResultSet executeQuery(String sql)<\/strong><br \/>\nThe executeQuery() method in Statement interface is used to execute the SQL query and retrieve the values from DB. It returns the ResultSet object. Normally, we will use this method for the SELECT query.<\/p>\n<p><strong>b) executeUpdate(String sql)<\/strong><br \/>\nThe executeUpdate() method is used to execute value specified queries like INSERT, UPDATE, DELETE (DML statements), or DDL statements that return nothing. Mostly, we will use this method for inserting and updating.<\/p>\n<p><strong>c) execute(String sql)<\/strong><br \/>\nThe execute() method is used to execute the SQL query. It returns <strong>true<\/strong> if it executes the SELECT query. And, it returns <strong>false<\/strong> if it executes INSERT or UPDATE query.<\/p>\n<p><strong>d) executeBatch()<\/strong><br \/>\nThis method is used to execute a batch of SQL queries to the Database and if all the queries get executed successfully, it returns an array of update counts. We will use this method to insert\/update the bulk of records.<\/p>\n<h2>5) Retrieve Results<\/h2>\n<p>When we execute the queries using the executeQuery() method, the result will be stored in the ResultSet object. The returned ResultSet object will never be null even if there is no matching record in the table. ResultSet object is used to access the data retrieved from the Database.<\/p>\n<pre><code>ResultSet rs 1= statemnt1.executeQuery(QUERY));<\/code><\/pre>\n<p>We can use the executeQuery() method for the SELECT query. When someone tries to execute the insert\/update query, it will throw SQLExecption with the message<br \/>\n<strong>\u201cexecuteQuery method can not be used for update\u201d.<\/strong><\/p>\n<p>A ResultSet object points to the current row in the Resultset. To iterate the data in the ResultSet object, call the next() method in a while loop. If there is no more record to read, it will return FALSE.<\/p>\n<p>ResultSet can also be used to update data in DB. We can get the data from ResultSet using getter methods such as getInt(), getString(), getDate(). We need to pass the column index or column name as the parameter to get the values using Getter methods.<\/p>\n<p>We will get to know more about the ResultSet in the next tutorial.<\/p>\n<h2>6) Close Connection<\/h2>\n<p>Finally, we are done with manipulating data in DB. Now we can close the JDBC connection. We need to make sure that we have closed the resource after we have used it. If we don\u2019t close them properly we may end up out of connections.<\/p>\n<p>When we close the connection object, Statement and ResultSet objects will be closed automatically.<\/p>\n<pre><code>conn.close();<\/code><\/pre>\n<p>From Java 7 onwards, we can close the JDBC connections automatically using a try-catch block. JDBC connection should be opened in the parenthesis of the try block. Inside the try block, you can do the database connections normally as we do.<\/p>\n<p>Once the execution exits the try block, it will automatically close the connection. In this case, we don\u2019t need to close the connection by calling conn.close method in the Java program.<\/p>\n<pre><code>try(Connection conn = DriverManager.getConnection(url, user, password))\n{  \n    \/\/database connection and operation\n}<\/code><\/pre>\n<h2>JDBC Program in Java connection Example<\/h2>\n<p>In this example, you will see how to implement the 6 basic steps to connect with database using JDBC in Java program.<\/p>\n<h3>Create Table<\/h3>\n<p>Before that, first, create one table and add some entries into it.<\/p>\n<p><strong>Below is the SQL query to create a table.<\/strong><\/p>\n<pre><code>create table employee_details (empNum number(10), lastName varchar(50), firstName varchar(50), email varchar(255) , deptNum number(10), salary number(10));<\/code><\/pre>\n<p>Created the \u201cemployee_details\u201d table in Oracle DB.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672836187774-JDBC%20Program%20in%20Java1.png\" alt=\"\" \/><\/p>\n<h3>Insert Data Into Table<\/h3>\n<p>Using the following queries, insert the data into the \u201cemployee_details\u201d table.<\/p>\n<pre><code>insert into employee_details values (1001, 'Luther', 'Martin', 'ml@gmail.com', 1, 13000);\ninsert into employee_details values (1002, 'Murray', 'Keith', 'km@gmail.com', 2, 25000);\ninsert into employee_details values (1003, 'Branson', 'John', 'jb@gmail.com', 3, 15000);\ninsert into employee_details values (1004, 'Martin', 'Richard', 'rm@gmail.com', 4, 16000);\ninsert into employee_details values (1005, 'Hickman', 'David', 'dh@gmail.com', 5, 17000);<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672836243455-JDBC%20Program%20in%20Java2.png\" alt=\"\" \/><\/p>\n<h2>Java JDBC Program<\/h2>\n<p><strong>Download the JDBC jar file and import it into the Java project.<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11533 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11533 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11533 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11533 .wpsm_nav-tabs > li.active > a, #tab_container_11533 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11533 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11533 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11533 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11533 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11533 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11533 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11533 .wpsm_nav-tabs > li > a:hover , #tab_container_11533 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11533 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11533 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11533 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11533 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11533 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11533 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11533 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11533 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11533 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11533 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11533 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11533\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11533\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11533_1\" aria-controls=\"tabs_desc_11533_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11533\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11533_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">package com.STH.JDBC;\r\n    \/\/ import sql package to use it in our program\r\n    import java.sql.*;\r\n \r\n    public class Sample_JDBC_Program {\r\n \r\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {\r\n        \/\/ store the SQL statement in a string\r\n        String QUERY = \"select * from employee_details\";\r\n        \/\/register the oracle driver with DriverManager\r\n        Class.forName(\"oracle.jdbc.driver.OracleDriver\");\r\n        \/\/Here we have used Java 8 so opening the connection in try statement\r\n        try(Connection conn = DriverManager.getConnection(\"jdbc:oracle:thin:system\/pass123@localhost:1521:XE\"))\r\n        {\r\n                Statement statemnt1 = conn.createStatement();\r\n                \/\/Created statement and execute it\r\n                ResultSet rs1 = statemnt1.executeQuery(QUERY);\r\n                {  \r\n                    \/\/Get the values of the record using while loop\r\n                    while(rs1.next())\r\n                    {\r\n                        int empNum = rs1.getInt(\"empNum\");\r\n                        String lastName = rs1.getString(\"lastName\");\r\n                        String firstName = rs1.getString(\"firstName\");\r\n                        String email = rs1.getString(\"email\");\r\n                        String deptNum = rs1.getString(\"deptNum\");\r\n                        String salary = rs1.getString(\"salary\");\r\n                        \/\/store the values which are retrieved using ResultSet and print it\r\n                    System.out.println(empNum + \",\" +lastName+ \",\" +firstName+ \",\" +email +\",\"+deptNum +\",\" +salary);\r\n                    }\r\n                }\r\n        }\r\n        catch (SQLException e) {\r\n            \/\/If exception occurs catch it and exit the program\r\n            e.printStackTrace();\r\n        }\r\n       }\r\n    }\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11533 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11533 a\"),jQuery(\"#tab-content_11533\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672836306462-JDBC%20Program%20in%20Java3.png\" alt=\"\" \/><\/p>\n<p><strong>Key points to be noted:<\/strong><\/p>\n<ul>\n<li>First, we will import the necessary packages in our Java program to establish a JDBC connection. These packages contain classes, subclasses, and interfaces that we will need to interact with the database.<\/li>\n<li>Next, we will register or load the driver with the DriverManager class, which is responsible for managing the JDBC drivers that are available to our program. This step is necessary before we can establish a connection to the database.<\/li>\n<li>Once the driver is registered, we can establish a connection and begin performing database operations. To create and execute SQL queries, we can use the Statement interface for simple queries, or the PreparedStatement interface for insert, update, and delete operations.<\/li>\n<li>The results of a SQL query are stored in a ResultSet object, which we can use to retrieve the data from the database. The next() method of the ResultSet class is used to retrieve more than one record from the results.<\/li>\n<li>Finally, when we are finished with our database operations, it is important to close the connection to free up the resources for other programs to use.<\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, JDBC (Java Database Connectivity) is a pivotal technology that empowers Java programs to seamlessly interact with databases. By providing a standardized API consisting of Java classes and interfaces, JDBC establishes a robust link between the frontend Java application and the backend database. This facilitates the efficient transfer of data, enabling programmers to store, retrieve, and manipulate information within databases, thereby enhancing the functionality and versatility of Java applications.<\/p>\n<h2>Frequently Asked Questions (FAQs) related to JDBC program in Java<\/h2>\n<p>Here are some FAQs related to JDBC Program in Java:<\/p>\n<p><strong>1. What is JDBC, and why is it important for Java developers?<\/strong><br \/>\nJDBC stands for Java Database Connectivity, and it is vital for Java developers as it offers a standardized mechanism to connect and interact with databases. It enables seamless data exchange between Java applications and databases, facilitating tasks such as data retrieval, storage, and manipulation.<\/p>\n<p><strong>2. How does JDBC differ from ODBC?<\/strong><br \/>\nJDBC is an evolution of ODBC (Open Database Connectivity) with a focus on Java applications. While both provide database connectivity, JDBC is tailored specifically for Java programming, utilizing Java classes and interfaces, whereas ODBC is a more general API that can be used with various programming languages.<\/p>\n<p><strong>3. What components are involved in a JDBC program?<\/strong><br \/>\nA JDBC program involves several key components, including the JDBC driver (specific to the database), the DriverManager class for managing drivers, Connection objects for database connections, Statement\/PreparedStatement objects for executing SQL queries, and ResultSet objects for handling query results.<\/p>\n<p><strong>4. What are the steps to establish a JDBC connection to a database?<\/strong><br \/>\nTo establish a JDBC connection, you typically load the appropriate JDBC driver, use the DriverManager to create a Connection object by providing the database URL, username, and password, and then use this Connection object to execute SQL queries and interact with the database.<\/p>\n<p><strong>5. What are the advantages of using PreparedStatement over Statement in JDBC?<\/strong><br \/>\nPreparedStatement offers advantages like improved performance and security. It precompiles the SQL query, reducing database workload, and provides parameter binding to prevent SQL injection attacks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is JDBC in Java? JDBC in Java stands for Java Database Connectivity, representing a progression from ODBC (Open Database Connectivity). JDBC serves as a standardized API specification designed to facilitate the transfer of data from the frontend to the backend. This API encompasses Java-written classes and interfaces, essentially serving as a conduit \u2013 distinct [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-11557","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JDBC Program in Java<\/title>\n<meta name=\"description\" content=\"Learn about JDBC Program in Java. Java Database Connectivity is a Java API to connect and execute the query with the database.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JDBC Program in Java\" \/>\n<meta property=\"og:description\" content=\"Learn about JDBC Program in Java. Java Database Connectivity is a Java API to connect and execute the query with the database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-04T12:48:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-21T06:24:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"JDBC Program in Java\",\"datePublished\":\"2023-01-04T12:48:49+00:00\",\"dateModified\":\"2023-08-21T06:24:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\"},\"wordCount\":1654,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\",\"name\":\"JDBC Program in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg\",\"datePublished\":\"2023-01-04T12:48:49+00:00\",\"dateModified\":\"2023-08-21T06:24:58+00:00\",\"description\":\"Learn about JDBC Program in Java. Java Database Connectivity is a Java API to connect and execute the query with the database.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JDBC Program in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JDBC Program in Java","description":"Learn about JDBC Program in Java. Java Database Connectivity is a Java API to connect and execute the query with the database.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/","og_locale":"en_US","og_type":"article","og_title":"JDBC Program in Java","og_description":"Learn about JDBC Program in Java. Java Database Connectivity is a Java API to connect and execute the query with the database.","og_url":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-04T12:48:49+00:00","article_modified_time":"2023-08-21T06:24:58+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"JDBC Program in Java","datePublished":"2023-01-04T12:48:49+00:00","dateModified":"2023-08-21T06:24:58+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/"},"wordCount":1654,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/","url":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/","name":"JDBC Program in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg","datePublished":"2023-01-04T12:48:49+00:00","dateModified":"2023-08-21T06:24:58+00:00","description":"Learn about JDBC Program in Java. Java Database Connectivity is a Java API to connect and execute the query with the database.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672834024788-JDBC%20Program%20in%20Java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/jdbc-program-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/prepbytes.com\/blog\/category\/java\/"},{"@type":"ListItem","position":3,"name":"JDBC Program in Java"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11557"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11557\/revisions"}],"predecessor-version":[{"id":17678,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11557\/revisions\/17678"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}