16.1 JDBC | Java Database Connectivity Theory Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens this is I've been ready from police for learnings and in this video we'll talk about JDBC so JDBC stands for Java database connectivity now think about this world think about this information world what matters is not the websites not the software what matters is data right so if you talk about this concept like information technology so everything moves or everything works around the information right example think about what's my views what's up to share information right why views this banking software to check our bank balance all those things right so the the whole funder of having this software world is because of data so the center of attraction for everything is data right so when you build a software why you build software because you want to process data and where you store this data so we acquired a database so you will be having a database in your system where they was told all your information now this database can be flat file system this can be our DBMS this can be no SQL this can be Big Data doesn't matter it's a database wide so let's say you are using our DBMS which is relational DBMS you can talk about MySQL we can talk about Oracle SQL servers those are your a DBMS wide if you talk about MongoDB so that's no SQL if you talk about Hadoop Big Data so that's a different topic if we talk about files the difference of databases right so we'll be working with our DBMS here so if you have a database user that's your database and you have a java code because to fetch data from the database we have to file SQL queries right so to work with database we have to file SQL queries now if you are working for a company you are not a IT expert let's say you're you're working for a bank now you want to fetch details of an F of a customer you will not file a query right you will click on a button so that means you need to create a GUI where you will click on the button and the data will be fetched from database that means you have to connect your front-end application so that's your front-end application with the back-end database this is your database and to connect Java with database we require a connectivity right and that concept of connecting Java with database is called as je BC so Genesis stands for Java database connectivity right so whenever you want to connect your java application with database we require JDBC now here we have four different types of drivers because when you want to connect java application with database we were to use one driver so we have for choice we can use a native driver which is ODBC bridge basically so we have what you visibility which is used for ms access we can use native API we can use pure Java drivers depend upon you what you use so there for certain there are four types of drivers so main intention is not to talk about drivers but to connect your java application with database so whenever you want to connect your java application with the database you have to use seven steps so the first step so we have to follow seven step so from one to seven so the first step of the connectivity is you have to input the package so that will be your first step now which package that we'll talk that we'll talk about this later so we have the package second step is you have to load that load and register the driver so first step is you have to import the package second step is you have to load and register a driver the third step would be connecting our establish the connection establish the connection the fourth step so first step is import the package second is load race driver authorities connect establish the connection the first step is create the statement the fifth step as process or ah let's say this is execute the query and the six step will be process results and the last step is you have to close the connection on the statement so these are the seven step forgery we see the first step is input the package the second one is load and register driver the third step is establish the connection fourth as create this statement fifth process the results or fifth is execute the query six this possess results and seventh will be close now to want to remember these steps just imagine you want to call someone you want to call your friend and you will ask him as let's say your polygraph Lyonya college and then you asking as results are out or not so you will call him guide so before calling what you need a phone so of course you want a phone right so let's input the package next you need a network you need a network and you should be able to call so that is load and ROI so driver you reversing card right now once you have a mobile phone once you have a SIM card or working SIM card the next stage you have to establish the connection so you have to call em you have a dialer number and you have to place on dial button it will call then you have to think what you have to say so you have to say results are out or not so that's this statement you have to mention that statement next you have to speak that statement it is execute the query then after saying you will get a response that is you have to get the response that is it out or now so do you will be getting yes or no so that process the results and that once everything is done that's the close right so if you follow the seven steps you can easily write any JDBC application now question arise which package you are talking about so this is input de package right so the package we have to use here is Java dot SQL package so that's the package way to use use with Java or SQL dot star the second step so we can break down the second step into two parts so we can say the first part is loaded driver and the second part is registered so we can say this is point a and point B so we have to a which is load and to B which is register now how to do that so which driver so depend upon which type of BMS you're working with so if you are working with MySQL so we have a different driver for Oracle we have a different driver for Postgres we have a defend driver but let's say in this X in this example we are talking about MySQL right so for the driver from ask ul is palm dot mask you ought Jerry be C dot driver so does it die one name we have so I just call dot mask you'll dot Jerry be C dot driver so you have to load this driver and here to register but hold on when you say you want to load it but from where we're from which location you want to load it so you have to download a jar file a library from the internet and the library name is it is mask you'll so that's mask you'll connector so if you are using NetBeans in NetBeans we have this connector in bill so just you just have to right click on the degrees and import the library again will see that once we start with the practical ok so so we required this leverage so if you are using NetBeans it is n built if you are using Eclipse you have to go to Google search for mask you'll connector and download the jar file ok it will be approximately 172 kb from not wrong so if you download that software then you have to load it in your project and again we'll see how to load in the practical session and then you have to register now once this 2 step is done now ok ok we are remaining with this register how to register to register this we need to use a statement which is we need to use a method which is called as for name so for them is a method which is a reflection API method which will load which will load your driver so you just have to mention here comm dot mask you lot so this this driver name JDBC driver so you have to write this method which is for name ok that that's how you register our method Malaysia driver so this method for name belongs to a class class so so we have to say plus dot for name so that syntax we have okay don't worry I can write the I will write the code for this once again yeah so we have the next step is establish the connection now in order to establish the connection you have to instantiate the interface Paris or not that this one it is connection so there is an interface called as connection and you have to instantiate that object or that interface next is create the statement statement will be of two types one is normal statement entering it's of three types one is normal statement then we have prepared statement and the last one is Colleyville statement so we have three types of statements here one is statement prepare statement and caliber statement so when you want to execute a query so you can execute the query with the help of statement object when you have a query which is in Bell so you have a predefined query but you have different values you can use prepare statement again we'll see that how to use it and then you want to execute the PL which is the programming procedural language in SQL so you can use Colleyville statements so this is for stored procedures okay now once we got this statement you have to execute the query now whenever you execute a query you will get a response right so let's say you want to fetch the data from the tape database so you will get a table structure so the response can be a table structure second let's say you are not fetching you're inserting a value so whenever you insert the value into database it will return the number of rows affected so you are getting something so you can process the results whatever you get and then everything is done you can simply close it how to close get the object of statement close it get the condition get the object of connection close it so that's how you do the seven steps now how to write the seven steps in a programming way so this is the steps right so before continuing just pause the video write down all the steps on your notebook so that we can refer okay so let me remove all the statements now okay so let me write this in our coding format so let's say we have a main method oh so that's your mean okay again this is public static void main in that you have string arguments so that's that's that's you can that you can imagine right so the first arrests are the faster the first type is you have to import the package so the package name is you have to write import statement and the package will be Java dot SQL dot star so does the package we have to use right the second step is you have to load the driver now to note the driver you have to right-click on your library fold library folder inside your project property so on the right side when you open that beans on eclipse you will be having a library folder there you have to add a mask your connector driver okay I can we'll see how to download that in the practical session on how to assign the value how to assign the library now once we got in ivory basic the the second part of the second step which is to point B or 2b is you have to register a driver for that the driver name is comm dot mask you'll doubt it's JDBC dot it's oh it's fun it's driving right and then you have to load this with the help of a method which is for name so for them is a method which will load your driver and this for the method belongs to a class parlors Plus this C need to be capital because this is a class so it is class dot for name in bracket e optimation a driver name now once we got the driver name or laudatory driver they store it next step is create the connection how to get a connection we have to say connection we can say connection count as the object name or the reference name the problem is connection is an interface right so we cannot say connection corn equal to new connection because we cannot instantiate the object the interface directly right so we need to search for a method which will give me the object of connection so someone has implemented the object of connection some somebody has implemented the interface connection and now they are providing the object so the method name is it is gate connection so gate connection is a method which will give you the object of connection okay how it does doesn't matter right because we are not concerned about how it works we are concerned only about the connection object now this connection object will take three parameters the first one then we have second one and then we have third one okay so it takes three parameters so the first parameter is the you are L the second parameter is username and the third parameter is password okay so the URL for your database because in your DBMS let's say if you are using MySQL you may have multiple DBMS so which database you want to work with so you have to mention that here second to axis mask you'll we have to specify the username and password so this is username and password again we'll replace that thing once we got the link now how to call this gate connection method right because it is a method right it will belongs to a class so the class name is driver manager okay so this gate connection this is a static method that's why we are using a class name it'll drive manager now once we got the object of connection which is gone the next step is you have to create the object of statement how to do that so we can say statement st equal to now again this statement is an is an interface so we can use the object which is gone dot create statement right so once you got the statement object so this is your to be step this is your third step and this is your fourth step this is your first term okay now the fifth step is you have to execute the query so how to execute we can simply say st dot execute query so that's the method you have to use and in bracket you have to specify the query so let's say I want to fetch the whole table so I will say select star from student so let's say I have this table which is student now it will fetch all the data right so this query will fetch all the data so what we to do wait to say waited save it as data somewhere right because if you father's query I will be getting a table structure because it's in this table I have two rows one is roll number and second is name right and so I'll be reading that table in this will in this one will be having some record so let's say one naveen - let's say large and three Mona so we have this three names right so we have Naveen Raj and Mona so now if I fire this query this table will be stored inside a object of result set so we'll say RS so this set is a interface which off which if you are creating the object which is RS it can hold it at table now this six step how to process results so - to fetch all these values there is a method with RS koalas get string or get int so to fetch roll number we can say get int to face the name we can say get string okay but the problem is if I say get get end which into a you'll be getting this one second one or third one that's the question right so initially your pointer will be somewhere here okay before the first value so their point is there so you have to shift your pointer to the first location and how to shift we have to say are this dot next as soon as you say are s dot next it will shift your pointer from this location to one now if you say Alice dot get end and if you specify the column number you have to specify which column the first column the second column so I can say the first column it will give that value one okay and you can store this value anywhere or you can just print it let's say I want to print both the values so I can say I can use a space in between so it attached your first value second we can say either start get because the second column is a string so we can say get string and in bracket you can mention two so this two will give you the second column data which is Naveen so we are the first row if I say get in first column it will give me one get string second column it will give me the beam if you have to go to next row it will say again our is dot next now this will take you to the next row now if I like the same line here I will be getting two and Raj right so we have to do this for three to four times depend upon how much those you have the sheriff writing this code repeatedly we can use of loop right so we can use a loop which is while loop and you can put this RS dot next inside a while loop and we can print this data with the help of system dot out dot println right so this is system dot out dot println but will it work inside of a loop because while loop needs true or false right we want to check how many rows we have maybe if you are if you are here how can you go to next line when you don't have that row so this are s dot next will do two things the first thing it will shift the pointer second it will check if you have that next line right so that's that's how you this is dot next works and once you have processed all your data the last step will be you have to close this statement object which is st dot close and corn dot close sounds good so that's how you write your application using JDBC so you if you if you execute this code this is your seven step if execute this code you can connect with it database so we have written this to fetch the value we can also write the methods or we all we can also add up query for inserting the difference will be when you talk about SQL so we have in SQL we have something called as DD l we have d ml and DQ l we also have TCL but that's not important here so DD l is whenever you create this structure of a database on a table that's d dl whenever you want to insert delete the values it is manipulation and whenever you want to far the query for fetching the values that is DQ l so for DQ l we use execute query let's say you want to insert the values that is that comes out DM l right so for that you have to use a method which is called as update execute update again we'll see that once we start with the practical okay so that's it from this and so before ending the session so normally in after every video now i will be asking you some questions about the technological world so for today's question is who is the founder of oracle if you know this answer just commit in the comment section let's see who was us first so that's just on JDBC see you on the next video
Info
Channel: Telusko
Views: 768,644
Rating: 4.9164429 out of 5
Keywords: java database connectivity tutorial, how to connect java progamm with database, database connectivity, database and java example, java connectivity tutorial, database, steps to connect java progrmmto database, java database example, database connection, theory with example, database driver, how to use database driver in connection, navin reddy, naveen reddy, telusko, learnings
Id: y_YxwyYRJek
Channel Id: undefined
Length: 21min 18sec (1278 seconds)
Published: Tue Mar 29 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.