16.2 Java Database Connectivity Practical

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens this one have been ready from tellus for learnings and in this video we'll talk about Java database connectivity so once we have seen the theory of it now this time we'll talk about the practical implementation so if you have not seen my three topics just search for Java database connectivity Theory theory by telesco okay that's my channel name so if you search for this on YouTube you will see my theory video so it's not compulsory to see that video is just once you see that with you you will you will get a better understanding of how to work with how to work with JDBC so let's make a quick recap recap what we have done so whenever you work with JDBC you have to follow seven steps right if you follow the seven steps it is very easy for you to connect your java application with database and you can fetch whatever you want one use you want you can insert values you can fetch values you can modify values you can do all those stuff right so the first step here is to import the package the second step will be to load and register drivers so that is register the driver right and the third step you have to do is we have to create a connection so we have to create a connection and then the fourth step is you have to create let's create a statement the fifth step will be you have to execute the query okay and then the sixth step will be you have to process the results okay which is this and the last step here is it's your clothes okay this is not the exact seven steps you can actually modify this for your requirement so this is what we do when we want to do connect with database right we have to write these seven steps now which state which package to input so the package name is Java dot SQL package next is whenever you work with JDBC so we have four types of drivers and JDBC okay and we need to want we need to load one of the driver so let's say if you are walking with miss access so you have to work with je a Java JDBC one driver or JDBC type one driver which is JDBC ODBC bridge the problem is with the advance of the new new version of Java doesn't support or DBC so now we cannot use ODBC feature using Java 7 or Java 8 so we need to go with the specific type of drivers for the specific type of DBMS so let's say if you are working with Oracle we need to go with Oracle driver so if you're working with mask you'll you have to go with MySQL driver in this example we try to connect our java application with mask UL so for that we need to use mask your driver now how to connect mask ula was so first you have to load the driver so the driver is comm dot mask ul JDBC dot driver so we need to load this class flash driver which belongs to a package which is comm dot masculine EBC but the problem is it is not dead in your system by default ok you will not find this in your Eclipse by default if you are using NetBeans well then good we have this driver inbuilt but if you are using Eclipse you have it download this this thing from the internet you had just have to go to and you have to search for mask you'll connect our jar so you have to search for this statement mask you'll connect a jar and the best place to download anything is mavin repository so if you don't know about mavin mavin is just a global repository where you have you will have all the libraries available doesn't matter with Java framework you walk with you have library there ok so just go to mask UL connector go to maven and search for mysql connector you can select any version which unselecting five point one point three eight which is released in december 2015 and that's ok for us if you click on this version you can actually use dependency if you if you're familiar with Navin if you are not then it's ok we can use mask you'll JDBC type for driver ok and we can just click on download here and you can see it is getting downloaded so we have a file of approx a 1mb okay now let's go back to eclipse so we need to load this driver okay so we have to load a relational driver so whenever you work with external Java files we need to add that jar file here okay so you can see I've already added that let me show you how to disturb - how to add that just right click on your project and go to build path configure build path here let me remove this and I will show you how to add it once again just click on add external jar and so I have that file in my downloads folder okay so that's downloads and you can select this file here and click on open so you can see we got our a jar file here and click on OK so once you have imported that file wait to load and register how to do that we'll see then we have to create connection objects so we have to create the object of connection interface and then we need to create object of statement interface then we have to execute the query and then we have to process the results again then we have to close the connection so let's talk the first step the first step is you have to import the package right now which package to input so this is let me write this the first line itself so we write here the import statement which is import Java dot SQL dot star because we want all the interfaces and classes so that is the first step the second step would be you have to load and race your driver now when you say load and register what we do here is we have to use something called as class dot for name so class dot for name so this for them is a method which will help you to load the class you just have to mention the class name ok the class name here is driver ok once you specify driver this class dot for name is responsible to load the driver for you you just need to make sure that you are throwing the exception you because this Ford name method will itself it will throw the checked exception so you have to do here those exceptional you can either we can also use to I catch now the problem is this driver what we need to also specify the packages for this because we to provide the qualified name so we have to say this is comm dot mask you are Gary B C dot driver so we need to mention the qualified name okay if you want to if you want to understand exactly how this class dot Fortnum works so I have a video on that just search for class for name telesco okay so make sure anyway search funny topic just mentioned that is for there okay so that you will get my video oh it's very difficult to explain each and every topic in one video how that exactly was what's important is whenever you work with a jdbc you have to use fourth name ok time to just remember that if you are so if you are curious about that I searched for this video you will understand how class recording works not just for JDBC betwee you can use class dirt for new for multiple things ok but the next step is you have to create the connection object right so for that way to say connection con right so going to also import the package for this or we have already done that we have say we have said starred right but the problem is if I go to connection it said interface you can see it's an interface the problem is you cannot create object of interface directly right so we need to search for a class which implements connection all we need to search for a method which will give you the instance of connection ok let me repeat you need to search for the method which will give you the instance of connection and luckily we have a method palace gate connection so gate connection is a method in Java which will give you the instance of connection now this gate connection is a method of a class College Java manager okay so drama is the class in which you have on Methos get connection which is the static method okay so if you go to driver manager you can see it belongs the same package which you are working with with java or SQL and in this package or in this class driver manager we have lots of classes available right and one of the classes which we want to use here is get connection because this get connection method is a static method which returns the which returns the instance of connection right that's what we want here so to achieve that a way to pass three parameters as you can see here so we need to pass the URL we need to pass the okay that's not the method we want to work with we want to work with the other type of get connections yeah so this is the gate connection method you want to walk with we have to pass the URL we need to pass username and password right so just go back to our class here you have to mention three things the first thing is the URL so what we'll do in this main function itself let me create a string and we'll name that as URL so that's the first thing we need to send the second thing is the username so we'll say this is you name okay and the third string main to pass the password okay now what is the username here so let me start with username it is for my system it is root and I have not specified any password here okay so I will keep it blank and the URL is so you need to mention a thing which is your database name but hold on we don't have database yet so before creating a database let's mention URL you name and we - pass pass okay that's the password you can see there is no problem now so it says draw manage or gate connection but for that way to first create a database so for database we can use any type of DBMS you can work with Oracle you can work with mask you'll you can work with MS access you can work with any type of DBMS you like okay so for this example I am taking mask you'll so first step you have to install mask you'll just go to again and search for masks you'll download or mask you'll serve er it will take you to the official website of masks you'll just make sure you download the mask you'll from here okay and the problem is when you work with masks you lits a command line interface you have to execute everything using command line that will be no that will not be that flexible right so what you can do is you can use a GUI for it which is mask you'll mask you'll workbench so just go to google and again search for MySQL workbench ok workbench so this is a GUI tool where you can work with database creation table creation all those stuff okay so I have already done that I have installed mask ul in my system so if you want to learn about a mask you'll our SQL stuff just again go to my channel search for okay search for MySQL tutorial telesco okay so just to give you a tool to my channel so just go to youtube and search for telesco learnings okay so that's my channel name so if you go to my channel i have lots of playlists okay so you can learn everything here so he can learn about a java tutorial you can learn about android you can learn about java eight new features you can learn about design patterns c programming oops concept you can also not about map in here and the most important thing for this is you can learn always wears masks oh it should be somewhere here so let me search for it yeah so you can see we have a mask you'll to toriel here so you can just go to this playlist and you can watch all this video to understand how mask you'll work okay and after this you can learn lots of things here how to use eclipse and you can learn about EJB GUI stuff all the stuff here okay yeah so just search for masks well tutorial and you will understand how to use workbench again let's get back to work bench now and here we need to create a table in fact I have already created table here with student occasional wasting of time abdicated a table with student so I first if I say select star from student I think I'll be already having some data because I have taken this session for a batch okay we have only one record which is one Vani so it means that some values here so we'll say insert into oh okay what's the table name its student then we say values so we to specify the first thing let's say it is a user ID - and we'll insert our name as Naveen okay that's the first thing let's run this code okay now we have the second record let's enter one more so let's say three which is Mahesh and if I run this code we got three rows now so if I don't this code okay so we have three rows what we'll do in this code is will will will give the value and we'll try to face the name okay and the query for that will be it is select we need to fetch the username so user name from student where user ID ok user ID is equal to three right in file in this code regard Mahesh right so that's what we want to do in Java so oh so we first we have to mention the database name the database name here is alien so you can see we have we are walking with a dub database which is aliens here right and okay but the problem is masculine ormally walks with okay so when talk about the URL patterns or URL we have to specify JDBC : MySQL : aliens so that's the composite things because it specifies that you are walking with Jeri BC and the DBMS you are walking with is masculine and inside masculine have aliens but mask you'll is a server level organ is a land land type of DBMS where you can have the database on other machine you can access with other machine so wheat also specify the IP address here since it is then in the local machine I will say localhost we need to also mention the port number so I am using a port number which is 3 3 0 7 if you are installing masks you'll for the first time the port number will be getting is 3 3 0 6 ok so you might be having a port number which is 3 3 0 6 ok so make sure you you know the port number now once we have done with the creating creation of the connection now let's go to the next step which is here we need to create a statement which is very easy now so you just have to say statement ok we'll say st equals u now in order to create object of statement you just have to say con dot create statement even this statement is an interface also we have to use create statement method because this create statement returns the object of statement right and now if I done with the statement now we have to actually execute the query so before executing let's straight the query here itself so in double quotes we'll mention the query so let's copy the query in the from the mask you'll which we have already typed and let's paste it here so that's the query we have right so we want to fetch the name of the user with roll number or with user ID 3 now how to execute the query it's very simple just say st dot execute query and then you have to pass the query here now whenever you execute this query let's execute this query here in mask you also you can say we are getting a data so what data you are able to see is it just Mahesh and there says no right we are not just able to see Mahesh we are also saying this user name as the as the label right so this thing will return you not just Mahesh it will give you the whole table not exactly the whole table but it will give you Mahesh in the table structure so way to store that thing in result set because result set has the power to store chunk of data and tabular structure okay so in this RS you have your data now now how to fetch the data it is very simple we can store the data somewhere so we say string or name this is where you want to store data so we'll say RS dot get string and you have to mention the column number so let's say if you are fetching multiple columns so you have to mention the column number or you can also mention the column name by in double quotes just right username so we want a data with the column name username okay you can have multiple columns side so you have to be very specific which data you want that's why you have to mention username there okay we can also mention the number so let's say you can mention one you can mention two or you can mention the column number right so that's it if I if I try to print this value which is named okay it should work now and it should print Mahesh if it doesn't print Mahesh then there is some problem with the code so let's try before that let's do the last step which is closing stuff right so we have done with the processing let us close it how to close very simple it's first you have to close the statement so you have to say st dot close and then we have to get we have to close the connection also we'll say connection dot close let us run this code and boom they got the error the error is before start of result set it's because whenever you run the query by default even see the pointer here right like so this pointer by default this pointer will be before Mahesh okay so way to do you know in order to fetch the value you have to say are s dot next so RS dot next will take your pointer to the next record and this time you can actually fetch my edge so before fetching the value we just have to say our s dot next because this is responsible to take you to the next element okay so that's how you use JDBC steps okay now we can do some more stuff here we can insert the values in fact we can also fetch the whole table not just one record we can fetch the whole table and we can also insert some values and in the next video we'll talk about how to insert values and how to fetch the whole table so I hope you liked this video so thanks so much for watching and and and before going off let me ask you a question it's just I have this habit of asking questions so the question for this session is a who is the founder of Apple okay so Apple Inc so you have to tell me the founder of Apple just use the comment section to answer the question so thank you so much for watching and do subscribe for further videos
Info
Channel: Telusko
Views: 540,438
Rating: undefined out of 5
Keywords: jdbc, java Database connectivity, mysql java, how to connect java programm with mysql database, steps to establish jdbc connections, how to connect java with mysql database, what are the steps to connect jdbc with mysql database, java database connectivity using eclipse, jdbc connections, statement, connection, java tutorials, jdbc tutorials, telusko, learnings, navin, reddy, naveen
Id: 5vzCjvUwMXg
Channel Id: undefined
Length: 20min 22sec (1222 seconds)
Published: Sun Apr 03 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.