Demo of JavaFX Application using JDBC - MYSQL Database.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's class we'll see how to connect to a database using a javafx application so i have a very simple demo here let me run this demo for you so this is the screen that i am going to use this form will ask the username password gender and subjects is a checkbox and country is a compo box so the user can enter some data here name password and select this gender select some subjects select the country and then submit so once when he submits record inserted successfully so you're going to have this record here so you see record has been inserted successfully so that's how we are going to use javafx with jdbc in this demo we'll see how to do that and one more important thing is i have some checks here let me show you what those checks are let us see those checks it's very important like you have these checks if i enter if i keep entering the name please enter the name so if i enter the name and then if i press please enter the password so if i enter the password and if i submit please select your gender so like that for everything we have a check if i select the gender and submit select your subjects if i don't select my country select your country and finally if i select a country and then submit it'll say record inserted successfully so you're going to have this record inserted in your database so this is the demo that we are going to see in today's class and this the real world how it's actually performed is they'll be using fxml to code the user interface so why we use fx mls it's something like html it just takes the logic of the user interface forms away from your core logic but here we are not going to use fxml as i have already told you we are not concentrating on fxml in our syllabus and in our assignments what we are going to do here is we are going to code the ui controls and we are going to utilize them in our demo okay let us now start coding that application this is a java fx application so we are going to have extends application and we'll implement the start method here so this is where we are going to write the code and one important thing is we are working with the student record here student has name password gender subject and country obviously we are going to create a student class so it's going to be class student and we'll have all these data members for the student private the string name and private string password and private string gender we'll also have a private string subjects i'm having it as a string here we can also have it as an array so all the subjects the students are interested in something like that and private string country so the country of the student so we love all these data members for the student and what we'll do is we'll have a construct a parameterized constructor here just to initialize the object so this is the parameters constructor that we have generated and we are going to get all this data from the javafx form and initialize the student object once when we are done with initialization the next thing that we are going to do is we are going to have a public method here public method for inserting student record very similar to what we have done in the last class for installing a student record and for insertion and deletion of a student record to a database we need a database connection so for this what i have done is i have reused the class dbms connection from our previous class lecture so if you're not clear with this please visit our previous class lecture on the jdbc connections i have shown a small application there and i have used this dbms connection class in my application so what this class does is it has url name and password and we initialize the connection and we get connection so this is for initializing the data members of this dbms connection and this is for get connection so we'll pass the url username and password and we'll be able to get a connection and this is for closing connection so this method we have already used it several times and we'll be using that method that class in our demo and we'll create a connection we just reuse our code from the previous class here say whenever i'm working with a database i will create connection i'll put everything inside a try catch block again a very important practice we should have and we'll print this out we'll print e dot get message so this is very important you have to put everything inside the try catch block the next thing is we need to insert the student record yes so now we have to insert the student record for that we have to write this sql string string sql and we're going to insert insert into student value so i am going to use another table called student one year it's not student it's actually i have created another table by name student one so we are going to use the student one table and we are going to insert records so let's go to eclipse so insert into student 1 values what all values are going to insert for everything i will give a question mark so for name for password for gender so everything will have a question mark here and we'll have subjects and the country so we have five fields so for each field we'll have a question mark and then we'll close the query and then we'll end the java statement so next we'll have this prepared statement object of type prepared statement again these things we have used several times in our previous classes so it's statement and it's not new prepared statements it's a connection dot prepare statement and then we are passing the sql string here next thing is we have to set say whatever that has come as input name password gender subjects country we have to map that to the parameters in the query so it's nothing but statement dot set uh string so it's going to be a string that's every argument is a string here so parameter index one what we're going to insert first is a name and the statement dot set string and parameter index 2 what we are going to insert is password and the statement dot set string so parameter index is 3 and what we are going to insert here is gender and please follow the order you have to follow the order the same way it's given say name password gender subject in country so let's go and insert the set string so parameter index 4 will be on subjects and parameter index 5 this is exactly what we have done in the previous class right just inserting a record parameter index 5 will be on country of the student so after setting all these things we'll have this inta is equal to we'll execute the statement dot execute update so this is for running this insert query on the database and if i is greater than 0 we can very well perform the check here like this out record inserted successfully in the database or else we can say else we can say this out record not inserted in the database so this is what we have seen in the previous class and you have to just get the record and then insert that in the database now what we will do is we will now go and create this form we have seen this form in javafx type we need to create that form so for that i am going to create a class registration form here i'm just putting this ui code in a separate class and whatever we add in this form we add a label for that's called a name label for saying enter your name and then we have this text field which is for getting the name from the user say name text i'll say like that we have a label for getting the for showing this information enter your password and then we add this password field right that is password field for getting the password from the user then what all we add we had this radio button so radio button options we add so radio button option 1 and radio button option 2 [Music] so this is for the gender and we also had check box for check box again we had change that is check 1 and we have 3 subjects there check box check 2 all these are options of checkbox in our form check three okay so i've included check box options too and for checkbox we had a label and for gender also we had a label we forgot that label will say gender label just to say select your gender and for checkbox also we add a label that is i mean say label check label or it is a subject label let's be very clear of this and let's have a label for the country we'll ask the student to enter the country right so select the country so country label and then this is a combo box that we you're going to have here combo box it's so first type string and we call this to be country yeah so we have defined all the fields here for our registration form the next thing is we should have a submit button so it's a pattern of type we'll call this to be submit so we also have button and i'm also having one more label here for throwing all the error messages i'll call this error label so i've defined all those objects that will be part of my form the next thing what i need to define is i'll have something called show form i'll have a method here public method that is show form and what this form what this one is going to do is it's going to create this form for the user the very first thing is we'll have this label that is aim label we'll define that name label is equal to new label and then we will say enter your name that's the very first thing in a form likewise we have a text field here let me put this text field there is this text field yeah so actually we have defined this to be named text it's already defined so name text is equal to new text field next what we are going to get from the user we are going to get the password so let's create a new password field password t let's create see why i'm not defining it again here is i just defined it here say password field pass text so we just say what is pass text it's a new password field and for this we should have a label too so let's define a label here so this is a label what is this label called this is a pass label so what is the text we are going to say here enter your password and we have now name password ready the next thing is we have to go with this radio button for this we should have this toggle group so let's create toggle group group is equal to new toggle group and we have to have all the radio buttons here so radio button option one what is the option one it's going to be mail and we'll have the next radio button that is radio button option two what is option two that we have defined that is going to be female so again all these things we have discussed in our javafx ui controls just coding those things here and what we have to do is we have to set option one dot set toggle group you have to give the group here and then we have to also define option two dot set toggle group so all these options belong to our toggle group here so that is group so radio option is done next is uh did we define a label for a radio no so let's define this label for a radio two so this is uh what is this label gender label and we are defining a label here what is that label enter your this is select your gender that's it so we are selecting the gender next comes another label what is that label that is subject label and this is select your subjects i'll say select your favorite subjects and here we have to define a check box let's go and define a check box here there is this let me just put a check box like this okay so check box check one what will be check one that's going to be english say let's put some subjects here let's again go with the check box check box i'm just selecting some templates here check box check two check two will be [Music] let's say dbms let's again create the check box check box so check three will be check box check three we can have it as physics some subjects will have for the student and no need to define checkbox check one again here because we have put that to the class definition data attributes right so no need to define this so let's so we have our checkbox subjects also ready next we should have this label for our country so let's go with this label this will call this to be the country label and what we are going to say here is select your country so he's going to select this country and only to define country level here and we have to define our combo box here so let's go and define a combo box so this combo box is called country that's what we have defined and we have to give some items here country dot it's a country dot get items dot add so first we'll add india again it's country dot get items dot add we'll add us here so these things will be displayed to the user country dot get items dot add so this is uk so we are displaying three countries and finally we should have this button interface too that is the submit button we'll have the submit button and what we are going to have here is submit the option will be submit and we will have an error label too for setting any error messages we have this label called error label and we are not going to have any messages in the error label it is going to be empty for now so that's how our form objects have been defined what next yeah plain simple when the user clicks on submit what should happen that should be the next thing so let's go and use the submit dot set on action and then let's call this event handler and obviously we have this method that should be implemented so handle will be called when the user clicks on submit so when the user clicks on submit what i want to be done is the form should be validated let's call validate form here so validate form we are going to write this method just having the form validation in a separate method that's it and i'll say it's a boolean method say boolean connect is equal to validate form so this validate form is a method that we are going to code inside our registration form itself so public show form we can go and have this method here public method that is validate form and what all we are going to validate here say this is going to be a boolean written so let's say it's boolean so what all we are going to validate here is whether the user has entered his password name and things like that for that i love this boolean proceed is equal to true initially i assume that the user has entered everything and we are we can proceed further but we'll have to make a check here what what check we're going to do we're going to check if name text dot get text dot is empty then and i'm going to have one more check here that is proceed is equal to true then what i'm going to do is i'm just going to have this error label set so here here is where we are going to use this error label dot set text what you're going to say enter your name and we're going to turn this boolean flag to false that is proceed we cannot proceed further that is the concept here so if name is empty and this is true why i'm using this you'll understand when i execute this so error label dot set text will say it's enter your name proceed is equal to false and we can also make other checks if pass text dot get text dot is empty then we can say and and proceed equals true only then we'll make this check see the the point here is if this check fails then proceed will be false then no need to even perform this check but if this check is true only then we will go to this check so if this is like name is not empty then proceed will not be false so only once only when proceed is true we'll go and check whether the password is empty say if name itself is empty why should we go and check the password field that is the point here so what i'm trying to do here is if name is empty then proceed is false so this password check will not even happen will not go and look for the checks that's why i'm performing proceed is equal to true check here every every time so what we can do here is error label dot set text and now we have to say enter your password you have to be very specific with our error messages and we have to say proceed is equal to false so like that you have to check whether the user has selected any of the radio options say option one dot is selected is equal to false and option two so you didn't select any of the options for gender so we have to say selected is equal to false and proceed is equal to true so it means the name is entered password is entered but gender reading select then what we have to say we have to use this error label dot set text we have to say select your gender and we can say proceed equals false so that's how you keep on checking we can also check if you have selected this subject so can you check that if that's a quite an active check here if check one dot is selected dot okay check one dot is selected is equal to false and check two dot is selected is equal to false since it says that he has not selected any of the favorite subjects check three dot is selected is equal to false and he has entered his password name and a selected his channel for that we have to check proceed is true then what we can say error label we have i hope now you know what is the use of error label dot set text we can say select your favorite subjects and proceed equals false we just proceed to be false finally we have this country check which you have to do that is the country dot get value is equal to null and proceed is equal to false then what we can do is we can this is proceed is equal to true i am sorry this is proceed is equal to true then what we can do is we can set the error label as select your country dot set text select your country select your country okay so and then we'll set proceed is equal to false here so we are just checking say everything will be set to false when one of the condition fails it will be set to false finally we will return proceed so what happens here is if proceed is set to false false will be returned or else true will be returned so based on this this connect will be returned say if only when the form is valid if connect one leave when the form is valid what we'll do is we will perform certain other checks what what are the checks we are going to do we will call student dot insert record so only when the form is valid the data is valid only then we can go and call student record for that we have to create an object of type student student s is equal to new student so we'll create an object of type student and what we'll do is we have to pass all these values to the constructor here so i will pass all the values to the constructor let us call the all these values what we are going to pass name dot get text that is the student name okay it's a name text dot get text so that is the student name and the next thing we are going to do is fast text dot get text these are the values that has been entered in the form next we need the gender say gender is either option one or option two right for that you have to check say if uh i love this string called gender yes string string gender it's initialized to nothing here why we are doing this say you're getting this option from the form if option one dot is selected then what we have to say gender is equal to option one dot get text so we're getting the text for enterprise user and setting it to gender and this gender will be passed to our student object likewise else we can just do and if if option two dot is selected then we can say or the gender is option two dot get text so that is option two dot get text so our gender is defined next thing is we can fast change area i hope you all have understood why i'm doing this i'm checking whether option one is selected in the form obviously it is selected the form has returned the valid form so if it is selected then general will be option one dot get text if option two is selected then gender will be option two dot get text and what else we have to do here the next thing is we have to get the subjects again this is going to be the same kind of a thing we'll have a string subjects i'll say selected subjects i'll have it as a string i'll initialize the string there's nothing in the string here now what i'm going to do is if check one dot is selected then what we are going to do is we are going to have selected subjects plus equals we just concatenated this check 1 dot get text so and then we'll also add some space here so that it's visible so it's plus some space also you can continue so we're just moving all the selected subjects to our string called selected subjects it's going to be repetitive check 2 is selected then it is check 2 dot get text and if check 3 is selected then check 3 is selected then it is check three dot get text everything is moved to selected subjects and finally what we do is don't need our space after check three and finally what we are going to do is we are going to populate selected subjects to the database okay finally we have this country how can we get this country is we can do a typecast of this it's actually a string and how can you get this country's country dot get value should we type fastest to a string because it returns an object and whereas we are passing a string to the database so that is important so we are creating this student and then what we have to do is we have to call insert student here for that we will call yes dot insert student record so this is going to insert the student record so here comes the catch when it inserts the student record it's going to perform all that insert statement here it'll say insert and it'll insert or else it'll say record not inserted in the database we can also set this value in our error label itself for that we can say return true if it is inserted or else return false we'll make this as a boolean method return false if it is not inserted even if it is an exception we can say return false and we have to convert this insta insert student to a boolean method let's uh it's not void anymore it's boolean why i am performing this is i can just put the error in the label that's why i'm performing this let's go and check so i'm calling insert student here right next what i can do is i can catch this boolean result is equal to insert student and if result if the student insert was successful they can do error label dot set text we can say record inserted successfully and else then it means record was not inserted so we can just put an error label dot set text we can say record not inserted in the database anyway there will be an exception here that will also be handled so this is with respect to an insert student and we also add this if connect if the form is not valid what we can do we can do we can just turn the contents in the label to red that's what i have done here error label dot set text fill to color frame that's it because when the form is not valid already the info is displayed in the error label i am just turning it to red or else if the form is valid get the data and insert the student record by creating a connection to the database that's it so that's how we have written this code let's go and check whether this one is working i'm not sure whether this one is going to work let's go and delete all the records in our student database i'm i'm just going to use this table called student1 here student i have used this use that in the previous class now let's see how to use the student1 table let's check let me go to sql i have clicked the wrong option here let's uh delete come on quick so let's uh how will you delete it's delete from student and then student one i'm sorry student one and then you click on go finally you have to get all the requests deleted in the student table that's what is our objective and then we will try inserting a record from our form too okay and we are missing certain other things here so what we have to do see the important things we are missing say what we have to start here for this we have to create an object of type registration registration form let's go and create an object of type registration form called student form and what we'll do is we'll do student form dot show show form so what this one does is it creates an object of type registration form and then it tries to show this form so again when we are showing this form see the very important thing i have missed that is we should have a grid pane layout we should have this scene and then things like that so what i will do here is i will use a maybe a vertical layout or a grid pane layout let's use a grid pane itself it takes some time for a grid pane that is grid pane root is equal to nuclear pain and now we need to add all our items to grid pane that is root dot add and then we have to give the child first one is the name label that is the thing column index is zero row index is one first row we're going to have this name and then root dot add and then name text will have name text and then column index will be 1 and row index will be 1. so like that we have to just align everything in a grid layout next comes pass label pass label column index is 0 row index is now 2 likewise likewise root dot add we'll have pass text first x column index is what is the column index it's zero it's one and the row index is two so we have to be very cautious while we are adding things here past label what is the column index column index is uh 0 and row index is 2 likewise name label its column index is 0 and row index is 1. so please be cautious while you're adding things to the grid pane it's very simple like what we have done earlier next we have to add the gender label root dot add gender label and column index is 0 whereas row index is 3 likewise root dot add its gender what we have here for it's option 1 i'm sorry it should be option one because you have to add the options here option one in column zero and row four root dot add option two in row 5 column 0 root dot add now we have to add this subject label and what is the column index 0 and row index is 6 and we have to add the subject checkbox elements it takes some time here check one column zero row seven root dot add and check to column zero row index is eight here i have to add the column index to the column zero take some time we'll finish this up quickly and check one root dot add check 3 column index is 0 9 row index so we are finished with the thing next thing is our country label should be added root dot add that is country label column index is 0 row index is 10 and root dot add country column index is 0 row index is 11. next we'll have the submit button also to be added root dot add so submit column index is 0 row index is 12. okay i have not selected all these column indexes let's say it's zero everything is in the first column itself let's put this to be zero this is also 0 and this is also 0. so we have added all the elements next we have to add this error element root dot add error label column index is 0 and row index is 13. so everything is added to our thing next what we have to do we have to create this scene next is same oh i'm sorry this scene so it is seen [Music] it's a javafx scene we have added the root there next we have to go with primary stage dot set scene that's also done and primary stage dot set width so here is this catch you know while you're using this primary stage whereas primary stage defined primary stage is defined in our start method so when you're calling this dot show form you have to also pass or stage that is primary stage we have to pass the stage because we are using the primary stage or else this is not going to work and we should also receive the primary stage in our show form let's go and receive that stage primary stage so i missed that but now we are passing the primary stage dot set width will have the width to be 500 and primary stage dot set height to be 500 and the primary stage dot show so that's how the form gets shown to the user so all these things we have to take care of so validation is done creation of the form is done we have created an instance and this this area primary stage stage okay this is already defined only to define that what a blunder i am a yeah so let's see whether a code works now registration form is done insert record and then validate form is also done and student class is also done class dbms connection is also done let's check whether we have deleted all the records here yes when we browse there will be no records in our student one table what we'll do now is we'll execute our code and check whether this one inserts a record that is the most important thing let's try it now let's check let's do a ctrl f11 ctrl s ctrl f11 let's see whether the form is getting shown to the user where did this i have quoted this show form did i put everything inside the show form okay the problem is i put everything inside the handle again a big mistake let's do a perform x and ctrl x and then we put that inside the show form maybe after this now our problem will be solved i guess let me run this again yeah certain small mistakes here and there let's hope that the form is now shown to the user okay void show form that's what is okay what is this i'm missing launch args here let's run this again i'm sorry for all these mistakes now we got the form yes it's up and ready now we'll enter a ballot records say let's enter satish he selects gender select some subjects selects a country and submits let's hope that if this gets inserted it says record inserted successfully we have to check your database we have to be sure by checking the database only let's check it out hope that this record gets into a database yes some more successful coding this so a record got inserted successfully so we can also try other things so what are the things we can try is we can [Music] check the other issues or what if the form is not a valid form am i still submitting a record to the database if you submit a form without the data to the database exceptions will arise so we have to check that to for instance if i'm not entering my name and then if i submit say enter your name so if i enter my name sufficient if i submit say now proceed is true for name check right so when i submit enter your password so satish satish123 and if i submit select your gender see how specific i am with the error messages when i submit select your favorite subjects so when i submit select your country so perfectly fine so this one works fine i want you to practice this code and execute this code and see how it works
Info
Channel: Satish C J
Views: 3,217
Rating: undefined out of 5
Keywords: cj satish VIT, cj satish vit, satish cj, satish c j, satish vit, sathish vit, satishcj, javafx application using jdbc demo, javafx using mysql, javafx with jdbc, how to use jdbc with javafx, javafx application, coding a small application using javafx
Id: eQ0yAy152I4
Channel Id: undefined
Length: 44min 56sec (2696 seconds)
Published: Tue Oct 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.