Transcript for:
Overview of JDBC Application Development

Hi friends, welcome to Naresh I.T. My name is Natras. In this I am going to talk about another important discussion of JDBC that is standard steps of developing JDBC application. So let me talk something about JDBC. You know that JDBC is part of JSC module. It is a technology and it provides rules and guidelines to develop JDBC driver software. JDBC driver software acts as a bridge between Java application and database software. We take the the support of this JDBC driver software to write the JDBC code which is nothing but persistence logic. What is persistence logic? It is the logic that interacts with what? Database software and manipulates database data. What do you mean by manipulating database data? It is all about performing what? Insert, update, delete, select operations on the database software. Insert means inserting record. Delete means deleting record, updating means modifying record, select means retrieving record. So, the ultimately we perform curd operations, what operations? Curd operations on database software using persistence lasing. What operations are curd operation? What is curd? C for create, U for update. R for read, D for delete. So, read is nothing but select operation. Delete is nothing but remove operation. Okay. Create is nothing but insert operation. Update is nothing but modify operation. If you read a book given by foreign author, an article written by foreign author, you never see the word called curd because they do not know curd. They do not know what is curd because only in Indian culture curd is there. So that is why you see the same term in the form of what? Kurud operations. What operations here? Crude operation. Same word, but just changed the letters here and there and made it as what? Crude operation. You may ask me what is crude. You might have heard the term called crude oil. Whenever petrol, diesel prices are increased in Indian market, government says we are helpless. In the international market, crude oil prices are increased. So, that term crude. So, ultimately JDBC is a technology using which we can develop the persistence logic in Java application. Using that persistence logic, Java application talks to the database software and performs code operations on the database. So, what is the importance of code operations in real time practices? You may ask me that question. You take in one hospital project here. You take one hospital project. New patient joined. His record should be inserted in the table. Insert operation. He has changed his phone number. Because let us assume he has got BSNL phone number and you know that BSNL number works fine in the villages but never works in the cities. So he decided to have Airtel number and he want to update the phone number. So that time you have to go for what? Update operation. operation. Next, you want to see is complete details have inserted properly or not or doctor wants to see patient profile before giving proper treatment that time select operation is required. got discharged from certain word detail or certain treatment details is information should be deleted, is record should be deleted. Yes or no? So that means if you take a small hospital application towards patient activities insert, update, delete, select operations are there. Overall conclusion is what? You take any service sector project where services will be given like banking application, hospital application, some other application. where services will be offered to customer, where services will be offered to what users without code operation we cannot see any project yes or no. So that means in real practices persistence logic is important as part of persistence logic code operations are more important got it. So then you may ask me one thing so how to do all this thing go and use SQL prompt of Oracle. doing Java and all these things. It is a good question. If you go to SQL prompt to insert record to update, record to delete, record to select record, you should write SQL queries. As a technical person you may write it, but what about non-technical end users? They cannot do it. They cannot do it. So what do you mean by non-technical end user? Who operates our software application is called non-technical end user. Sorry, they cannot do it from SQL prong because they are not technical people. For example, one bank employee is there. joined in the bank, one customer has come and said I want to open the account in your bank. Employee is happy, but he has collected details from customer and gone to what manager. Manager said hey go to oracle sql prompt write insert query. Since he is a bank employee, how can we expect SQL query from bank employee? He is what? Non-technical person. He has joined in the bank because he do not like IT industry and for that person if he one SQL query in the SQL program, he will run a from the bank job or not. So, that means what you know computers, but you do not know technical part of the computer. You know how type keyboard letters, how to click mouse button is a technical, non-technical end user, is not a technical guy. So, you expect what? One front-end application having button text boxes and all these things. So, he just clicks on what? Text boxes, types content in the text boxes and clicks on the button and activity should happen. Activity should happen. So, to develop such application, we need to use what? JDBC. support in Java application. Does JDBC develop entire front end application? No, no, no, no JDBC just makes a Java application to interact with the database software and to manipulate database data. So we cannot expect everything that JDBC will do. We have to use JDBC with other concepts of Java. Suppose you want to see a standalone application talking to database software. You will take a normal class with main method, right? JDBC code and that application talks to the database software and manipulates the database data. You want to see a GUI application talking to database software, we will write JDBC code either in AWT or Watswing application and that will talk to database software. software. You want to see a web application of Java talking to database software, you will write JDBC code in surlet JSP components which are there to generate the web pages and they will talk to database software. So ultimately JDBC is not there to develop entire front end. JDBC is there just helping regular application talking to database software. If you observe very carefully, I use two terminologies front end and back end. Java application with JDBC code front end or Java web application with JDBC code front end. Back end is what? Database software. Back end is what? So, if I explain in layman terms, what you can see, what you can operate, what end user operates is called front end. What you cannot see, but part of what application and response based on front end instruction is called as what? Back end. Generally, database software is called back end. Java application with the JDBC code,.NET application with some other persistence logic or PHP application with some other persistence logic is called as what? as what? Back end sorry it is called as what front end it is called as what front end. So front end interacts with the back end and gives instruction to back end to manipulate the database data. For this purpose front end takes the support of one or other persistence logic that is nothing but what JDBC code in the Java front end application development. So typically it looks like this. This is my database software, this is my Java Java application acting as front end. This is my database software acting as what back end acting as what back end. So, what this front end contains some components component 1, component 2, component 3. So, here one register button. What are the labels for this? Student number, student name, address or if you want you can take employee example and customer example also, register. So this front end application talks with what? Back end application. So, we enter student details, we click on register button. So, this application internally uses what JDBC code, generates insert SQL query, sends that insert SQL query to database software, executes in database. software and we will get what output back to Java application right. So this is the typical need of JDBC in application development process right. So, when you want to develop Java application as front end either CUI or GUI by having JDBC code as persistence logic to talk with the database software, we must know how to write that JDBC code. So, now let me concentrate on what are the different steps that we need to follow while writing JDBC code. code in our Java application. So, shall we start analyzing that one by one? Yes. Load JDBC driver class and register with driver manager service. Every JDBC driver software there is collection of lot of classes. But if you want to work with JDBC driver software there is no need of knowing all the classes you just need to know only one class name that is a driver class name. Out of so many classes how can you find out certain class as what driver class name. It is the class that implements java.sql.driver interface. You have to load the driver class and you have to make sure. sure that the driver class object is created and registered with what? Driver manager service. If you want to use any JDBC driver in Java application, the JDBC driver must be registered with the driver manager service. How to do that, sir? Create a driver class object and register with what? Driver manager service. Okay. So, first load the driver class and create the object for the driver class. and register with what? Driver-manager service. The most important point here is the moment you get the thought I want to use JDBC driver software, if I arrange driver software in the form of jar file, if I add that jar file to class path, not enough. What is most important? That JDBC driver class object must be placed in what? Driver-manager service. That is nothing but registering JDBC driver with driver. manager service. What is next one? Establish the connection with… Establish the connection with the database software. Since the driver is registered with the driver manager service, now my next step is what? I have to have connection with the database software from Java application. That means I need to establish connection between Java application and database software. What is need of it? Why we are registering driver? To use it. What we want to do with the driver? We want to talk with the database software. To talk with the database software what is the most important one? Establishing connection. This connection acts as a road between Java application and database software. Communication channel between Java application and database software. Why because if you want to do something first of all connection should be there between Java application and database software. Without connection what we do? We cannot do anything. That is why it acts as a road map between Java application and one database software. Okay. Road is ready. Here, you do not worry about how to write the code technically, I am just explaining conceptually what are the different steps that we need to seriously follow to develop the JDBC code. First we understood importance of writing JDBC code in Java application, where and how all this stuff. Here, we are understanding, come on, what are the steps that we need to follow while writing that JDBC code in what Java application. First, what is JDBC code? what we had done? Register driver with a driver manager service, establish the connection with what? Database software. Now, time has come to create the statement. What is this? Create JDBC statement object. What is the importance of it? Road is ready between Java application and database software. We need a vehicle, we need a vehicle to take inputs to database software from Java application. and to gather outputs from database software and to bring them back to Java application. Sir, what inputs are? Basic JDBC code basic requirement is what here? Basic purpose is what? To send queries to database software and to execute them in database software. So to carry those inputs from Java application to database software, we need a vehicle and that vehicle is nothing but what? Statement object. These queries are executed in database. database software some results will be generated. We need to bring those results back to what? Java application. Again vehicle is required. That vehicle is nothing but what? Statement object. So the ultimate conclusion is it acts as a vehicle or courier service between Java application and database software to send SQL queries to database software from Java application as inputs to make them executing in data. database software and to gather results what is that to gather results back to java application from what database software. So who will do all this thing statement object so that means driver software is registered connection is ready that means road is ready and to travel on that road we have what statement object we have what statement object that means even vehicle is ready. So, what is that to do? send and execute SQL query in database software. So, vehicle is ready. We have to use that vehicle to send the queries to database software to execute them in database software. This is my next step. So, once query is sent to database software, executed in database software, definitely some results will be generated. So, we need to gather the results and we need to process the result. results from database software and process the results. For example, if I sent select query to database software for execution, database software gives bunch of records. So, they will come back to Java application. We need to receive them and we need to process them. process will take the support of one more object called result set object. Suppose we send one insert query to database software to insert one record in the table. Once record is inserted, so the database sends one signal whether record is inserted or not. inserted or not. So if inserted we will say certain registration is successful, if not inserted we will say registration failed. Generally insert operation we do towards what email ID registration, student registration, customer registration. So that means first send inputs, make them to do something in database software and gather the results of that operation. So based on that result come to one conclusion whether results are generated properly or not. not, application is what completed required task R naught. So once everything is over, close JDBC objects, close JDBC objects especially connection object. Especially what? Connection object because all JDBC objects are internally I-O streams and also use lot of networking concepts. Why? Database may be there in the same computer, may be there in a different computer. So in core Java networking concepts. Sockets, server sockets all these objects will be utilized internally and these are just sending something, receiving something definitely what streams also there. So JDBC internally uses what lot of networking concepts, I-O stream concept you know that. Any stream that is open should be closed properly at the end of the application. So, what happens if I do not close? Even though task is completed with those objects, with those stream, still they will be engaged to open. That resource that is there at the destination still thinks that, okay, client is still using our resource. So, it is a waste of engaging resource even though we are not utilizing them. So, by keeping... this one in mind it is always good practice to close the all the JDBC object all the streams at the end of the utilization. For example in the JDBC application you created connection object close it statement object close it some other 2, 3 JDBC objects you created yes. close them. So, it is always good practice to close them. You may ask me one question, Sir database is having so many connections, our Java application is only one application not closing connection what happens? Yes, that is a good question. If everyone thinks that my application is the only application not closing the connection with the database what happens? One or other day database may reach to what? Maximum connections and that time what happened? If any new client comes and ask the connection to database, connection for database, database says boss all my connections are busy, I am engaged with lot of client, no connection is free, okay come after some time or error will be thrown. But the fact is what most of the connections are ideal. In that situation database fails to give service to new clients or not. So by keeping this kind of bad practices in mind, a good programmer closes sockets, streams, JDBC objects all the things at the end of the utilization, at least at the end of the application or at the end of the utilization. Here I want to tell a small story to tell the importance of closing JDBC connection object to avoid the laziness of closing what streams and JDBC objects and other sockets. There is a king. One day he said to one minister, okay, all my citizens are very sincere because I am taking care of them like anything. Minister did not like that statement. Minister gave a reply. He has given reply. If chance comes, every citizen can try to cheat the king also. So, King did not like that statement, he argued with minister and some discussion went towards what like a bet kind of thing. So, he gave me chance, I will prove it. Okay, then minister announced in the kingdom, today night one container will be placed in the dark place. He highlighted the word called dark place. All citizens should come and pour what? Milk in that container. And he has given that statement in the middle of the summer season. So, all citizens have come and poured something onto the container that day night. Very sincerely they come and poured. So, night completed. Next day morning, king and minister together went to that particular container and when they opened the container, surprisingly they have seen only water, there is no milk. Then they started analyzing why it happened. Every citizen thought that I am the only one person not pouring what? Milk pouring water, water plus milk is equal to milk only. That is why they stopped pouring, they did not pour any water. milk, they just pour water at the end container is having full of water. So what is the lesson we have to learn from this story? If every Java application or any client application that talks to database software thinks that I am the only client application not closing connection what happens there are so many connections with what database. If everyone thinks like that one or other day what database may reach to maximum connection and what happen database may not give. services to new client. So, by keeping this problem in mind it is always suggested to close the all the streams, all the JDBC objects, all the sockets at the end of the utilization okay. So, these are the standard conceptual steps that we have to follow to develop the JDBC application. In another video I will try to give same steps with the technical coding, same steps with what technical coding. So, can I summarize? Arise all the steps that are there to develop the JDBC application. Number 1, load and register JDBC driver with driver manager service. Next, establish the connection with the database software. Create statement object. Send and execute SQL query in database software. Gather the results collected from database software and process them. At the end, close all the JDBC objects. Here, I will show you how to create a JDBC file. I would like to talk one minute about driver manager service. Driver manager service is what? A built in service in every Java application which is capable of what? Managing JDBC drivers. It is invisible service, not visible to anyone. It is capable of managing what? Set of JDBC drivers. If JDBC driver service is not there, we cannot register the driver and we cannot use the driver in the Java. application. So, that is why every JDBC driver must be registered with what? Driver manager service which is there as a inbuilt invisible service of every Java application. So, I hope you benefited with this video. You can catch me for more videos in Naresh IT channel.