Connect MySQL to Java in IntelliJ 2024 | Full JDBC Tutorial for DB Connectivity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we will be installing MySQL server and workbench I'll also show you how to connect your Java application to your mySQL database so first you'll want to go to the site here and I'll have the links in the description below so we're just going to leave everything by default and click on the top download button so it's going to take you to this page just click on the link below it's going to tell you to save the file somewhere and then once the file has been downloaded just click on it to start the installer so since we only want MySQL server and workbench we're going to do a custom installation and then we just want to add MySQL server I chose the most updated version so just select the most top version and we'll want to do the same thing for your workbench so we're just going to find workbench hit next and we're going to download these files so just hit execute wait for the files to be downloaded I'm gonna hit next so now we're going to install it just hit execute again and once it's complete just hit next hit next we're just gonna leave everything default here so just hit next again again next all right so here we need to place a password for our server is going to be very important so keep this password safe and don't forget it because we're going to need it to connect to our server so hit next leave everything by default hit next again so now we're gonna apply all the configurations that we just made and just hit execute all right so hit finish and we're just gonna launch workbench now so I'm going to show you guys how to create a new MySQL connection so we're just going to hit this plus button here and just name the connection anything you want I just named it users database so here I'm going to test the connection and it's important that you know the password because that's how you're going to connect to your database click on the schema tab here to view all the current schemas that we have currently we only have one and this comes by default so we're going to create our own schema and we do this by clicking on this button here and you can name your schema anything you'd like I'm gonna name it login schema once you're done just hit the apply button below and then just hit apply again then finish so now we've successfully created our schema so now let's create our table so in order to do that click on the drop down menu but you want to right click and click on create table so you can name the table anything you want I'm going to name it users so now to create Fields you just need to select one of these check boxes and it'll add a new field for you we're going to make the ID field first and it's going to be our primary key for our table and it's also going to be non-nullable which basically just means that the value can't be null and we're also going to select AI which means Auto increment and what this basically does is we're going to give MySQL our database the authority to manage this value for us essentially it's just increasing the value of the ID which is an integer by one for every new data that gets entered into our database so now I'm going to enter two more Fields it's going to be username and password and the data type for these would be string but in MySQL they don't have a string data type but they have a VAR chart data type which is essentially the same once you're done hit the apply button then you want to hit the apply button again and our users table should be made check this by clicking down the drop down menu to see our table and then we can also see the fields of our table which is going to be the ID username and password so let's test this table out we're going to do a simple insert statement and this is an easy way to do it just right click and then copy the clipboard and select the insert statement in which will generate an insert statement code for us so remember I chose Auto increment for the ID well because of that we don't need to set a value for ID users we can just remove it like this and the value will be managed by our database so I'm just going to enter username and password that we're going to enter into our database like this and hit this execute button this lightning bolt button and it'll execute the xql code and it's going to insert the data into our database you can see here that it tells us that it was successfully entered so let's actually check this out we're going to create a select statement we're going to select all the data from our users table and we should see the data that we just inserted and there you go our data is right here and notice that in ID the value is auto incremented to one so now I'll show you how to connect your mySQL database with your Java application so you'll want to go to this side here I'll also include the link in the description below for the operating system just select platform independent then download the zip file which is going to be the bottom download button click on the link below and you're going to save the file somewhere and you just wait for download to finish once it's done extract the file to a destination folder so what I do is I have a folder named Java libraries where I store all of my external Java libraries so I already have the folder extracted but I'm going to do it again just to show you guys so this jar file will contain methods that we can use to access our mySQL database and we're going to import it to our Java application alright so now let's go to IntelliJ first we will create a live folder where we will place the draw file that we just downloaded to do that right click on the project folder and hit new and then click on directory so we're going to name the folder lib X will store a copy of the jar file into the slip folder so find the jar file and then copy it just directly copy it to the lib folder so next what we want to do is include the path to our jar file in our project structure in order to do that click on file at the top left then click on Project structure go to the modules tab and then make sure you're in the dependencies tab which is right here next hit the plus button and then click on add a new jar file so find the jar file in your projects live folder once you found it highlight it and hit OK and hit apply and then OK so let's see how we can use this in a quick example so first let's make a class I'm going to call it my jdpc and my gdpc basically stands for Java database connectivity all right so let's make our main method first we need to connect to the mySQL database and we do this by creating a connection object this class can only be called when we imported the library that we did earlier and we'll do this using the get connection method so this method will take in three different parameters the database URL the database username and the database password so for the database URL it's structured like this is going to be jdbc colon MySQL colon slash slash and the URL in the my workbench we can find it in here so we'll just put that value in it's going to be one two seven point zero point zero point one column Dash 3306 any Dash and the schema that we are going to be working in which is going to be the login schema so the username password that we're going to use is going to be the one that we created earlier in the setup to create a query we're going to be creating a statement object using the statement class and we are going to store the results of the query in a result set object we're going to create a simple query selecting all of the data stored in the users table so we're going to execute this query and it's going to return to the result set so as long as there's data to be read we're going to keep iterating through our result set until we reach the end so to get the username and password data from our result set we're going to be using the get string method which will take in the field name as its parameter like this keep in mind though you may need to use a different method like get int if the field is returning a different data type so we're getting an error right now because we need to put these lines of code in a try catch block we're going to be catching an SQL exception and we're just going to print out the error if something happens we're going to take these lines of code and move them to the try catch block like this and it should be okay so now let's run the app so the values that we're expecting should be the username and password that we created just a moment ago yep like that so to compare the answers let's see what MySQL has so we're going to use the same select statement and we execute it yeah there you go so the values are the same if this tutorial helped you out I heard that leaving a like and subscribing will help improve your programming skills and if you are interested in doing some beginner projects you should check out this tutorial
Info
Channel: TapTap
Views: 11,854
Rating: undefined out of 5
Keywords: ASMR programming, Java Swing tutorial, ASMR sounds, Java programming tutorial, Relaxing programming, ASMR triggers, Swing GUI, ASMR whispers, ASMR tingles, Java Swing GUI tutorial, ASMR programming sounds, Java Swing coding, ASMR for programmers, ASMR keyboard sounds, Programming with Java Swing, ASMR for coders, Java Swing components, ASMR programming triggers, Java Swing development, Java Swing GUI development, Java Swing layout, ASMR for Java developers
Id: 9ntKSLLDeSs
Channel Id: undefined
Length: 11min 5sec (665 seconds)
Published: Thu Aug 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.