This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you 16.5 Class forName use in JDBC. Following along are instructions in the video below:
welcome back aliens this is I mean 20 from police car learnings and in this video well talk about one of the methyl which is very famous when you work with JDBC is for name so the class name is class and the method name is Ford name so we use this method to load the driver toy so to load and raise our driver we use class dot Ford name but what exactly does so for that what well do is well try to do a simple code without JDBC so lets say we have a class here which is PQ R okay so we have this class P Q R and in this class we have a static block so if you do not know the static block it is a block which will be which will be executed once the once you load the class so we have a static block and then we have something or this instance block so this is an instance block so lets say if I print here and I will say this is in instance and we print here this out in static okay and if I done this code so you can see we – we are not calling anything from the main method and if I run this code so you
can see we got nothing but as soon as I create object of PQ R so I will say P Q R obj equal to nu P Q R and if I run this code so you can see it executes the static block and then it executes the instance block now why is the case is because the whenever you want to create the object so this is let me just write here in the comments so this is instance block and this one is a static block so whenever you create an object so before creating the object it will first load the class and while loading your class it will call the static method or static block while you are creating the object it will call the instance okay so that means whenever you create the object you get two things the static block and the instance block right but lets say I do not want to create the object but still I want to call the static block so what we can do is we can load the class so in order so if you load the class it will execute a static block right now how to load the class so to load the class we have to use glass dot for name and we can specify
which plus so I want to load the class which is PQ R okay it will it may tone exceptions so lets run this also let us apply the exception if I run this code you can see we got in static because we are not creating the object we are just loading the class and then we have a method which is new instance okay again throws an arrow against yeah and lets run this yeah so if I say dot new instance it will create the object so when you will not when you say dot instance dot new instance it will create the object but if you if you just say without this if you just say dot for name it will be it will execute on the static block so that is the advantage of using class dot for name but let hold on whether would you work with JDBC so does the point that is the funder of class name okay so we use class dot for name to load a class so that when you load the class it will execute the static block right so whenever you work with Jerry basic code right so you write this statement which is driver manager we create the object right so we say connection of con equal to
Java manager and before that line so before this line so this step is you have to load and register driver right so how do you load so whenever you want to load the driver so we need to use something called as Java manager dot dot register register driver in this you have to create object of the driver so thats this is the statement we should be using to register driver because we have to raise to this driver servicio so let us say if you are working with mask you have to know the mask youll drive ur if you are if you are working with Oracle you have to load out a co-driver and to do that we have to first use this Java which is mask youll connect ur so the address is comm dot mascot JDBC dot driver okay so it was one exception okay let me just write in general exception instead of writing all the stuff is the exception so this is the step we have to use so this is the step which is responsible to load the driver right so let us say or they show a driver so lets say you are using Oracle so it will be what I call driver here but instead of doing this stuff what if
just specify comm dot mask you are Gary BC dot driver so this stuff here is same as this thing but the question arise how its same it is because whenever you go to the driver class of your mask color connector in this you have a static block and this static block does the same thing which you you are trying to do some simple right so this thing here so whenever you call cross dot for name it will call it will load the class why this class and whenever it loads it will execute the static block and when we execute static block it will call the method which is the register driver so that means you do not have to write this statement because youre writing Clause dot for name so that means whenever you want to raise to a driver so class dot Fortnum is not the method which low which which raise to a driver it is actually the static block of Java class which loads to driver this all which register driver so this class dot for name its just loads the driver so that you can raise your driver here okay so that is the basic understanding of class dot for name so that is from this video and do subscribe for for the videos
Load class in java, class forname, how to use forname in java jdbc, use of forName in java, java tutorial, jdbc tutorial, how forName is used in jdbc, java j…
Thank you for watching all the articles on the topic 16.5 Class forName use in JDBC. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.