How to Connect Java With MySQL database in NetBeans

How to Connect Java With MySQL database in NetBeans

HI! My name is Bintu and today I am going to teach
you, how to connect MySQL and NetBeans IDE. NetBeans IDE (Integrated Development Environment). You know that, it is very popular IDE for
developing Java apps. While developing Java applications, you need
to save information into the database. It might be the information regarding the
items that you are selling or users or customers registering at your site. I believe that MySQLand NetBeans are already
installed on your machine. If you haven’t, please do it and if you want
any guidance and the the steps of knowing how MySQL is installed on a machine, please
refer to my youtube videos, one is in English language and another is in Hindi language. These are the URLs for that. So, after installing and configuring MySQL,
you can go ahead with this tutorial. There are two phases of connecting MySQL with
NetBeans. One is connecting MySQL with NetBeans will
enable you to create database and tables from NetBeans IDE. You can manage the rows, you can alter the
table, you can alter the information in the tables. One more phase is that, you can access the
database content through your Java apps. So, let us begin with it. When you launch NetBeans, you have to first
ensure that MySQL database is running. If it is not, the first job is to run it. Let us do it practically. I am going to start with NetBeans. This is my NetBeans. The first screen that you get is this one. Go to the Services window, you find these
databases that are installed on your machine. And you can see that MySQL is initially in
disconnected mode. So, the first job is to connect it. Right? So, you right click on it and you click on
Start button. It will make it running. You have to authenticate before running the
MySQL server. It is asking you to enter the root password. This is the password that you specified while
installing MySQL server. So, click on OK. You will find that MySQL started running. And you will be able to see all the databases. These are the databases, that are built in. They are already provided to you. You can create your own database also. Right? So, you can click here. Create Database. It is asking you to enter the name of the
database. Say, suppose, I enter the database name as
“shopping”. See, whenever you create any database, the
admin , the root is given privileges to access it. If you want to give privileges or permissions
to the specific users also to use this database. you have to click on “Grant full access to”
and here from the drop down menu , you have to select the user to whom you want to give
the permission to access the database. Right now, I will keep it unchecked. I want only the root to access this shopping
database. Click on OK. The shopping database is, you can see it , it
is created here. And one more thing, you can see is that, this
is created for you. This is your complete node. This proves that, your shopping database is
created, you can see the tables and everything here. But the tables node is empty here because
right now at this moment, the shopping database is totally empty. If you want to create a table, click here,
create table. Let us name the table as “customers”. Add a column, say email_address. Select the data type as varchar and the size
of email_address is assumed to be 50. Make it a primary key and when you click on
primary key, it will automatically check this “Unique” box because the primary key has to
be unique. OK. You click on OK. This column is added, email_address. Let us add one more column, password. Again, make it varchar. Let us make the size 50. This Null means, you can enter null value
also. You can leave this field as blank also. Anyway, we will leave it as such as default. Click OK. So, these are the two columns been created
in the customers table. Click OK. So, you can see that the customers table is
created with the two columns. You can enter some data here also. Right click, click on View Data. View Data will enable you to see the content. Now from here, you can even add the rows. Can you see it here? This icon? Insert Record. You click here. It is asking you to enter the content of email_address
column. Password make it “diamond”. Add row for another . Click on Add Row to
add another row and enter something like, [email protected] anything. Here, say “gold”. Let us keep the two rows only. Click on OK. Can you see that, two rows are been inserted. OK. Now, this is that. So, this is your connecting of your NetBeans
and MySQL so, that you are able to create database, you are able to manage database
but there is one more phase of it. You want to connect NetBeans with MySQL to
enable the programs to access the database content. Let us begin with that phase also. For that, let us create one project. New project, select Java category, Java application. Click on Next. Let us give the name as AccessingMySQLInfo. Click on Finish. It will create one application for you, AccessingMySQLInfo. This is that. Right? You have to add. Here. Library. Can you see that? Here, you have to add one jar file. What is the jar file? Let us understand that. Let us have a view of what we did. We opened NetBeans, you were ensuring that
MySQL database is running. So, what you did, you went into the Service
window, you right click here on MySQL node. The you click on started. You were prompted for MySQL root password. After that, you created a database. After creating a database, you created the
tables also. For accessing the MySQL content through Java
programs, you need one MySQLJavaconnnector/j. And you have to download it from this URL. At the time of this lecture, the latest version
available for the this MySQL connector is 5.1.38. Obviously, this connector establishes the
link between the NetBeans and MySQL. And it helps in establishing communication
between the Java programs and MySQL. So, when you go to this URL, you will be able
to download this connector in the format of zip file. You extract this zip file , you will find
one jar file inside that zip file. And what you need is that, you have to first
copy this jar file into the Java’s library’s folder. I have jdk 8 installed on my machine. So, you have to copy this jar file into this
location Program Files java and this jdk’s lib directory. So, let us do it. How to do that. So, I am going to your desktop. This is that. MySQL connector, I have unzipped it. Here, when you unzip it, you will find this
jar file. You have to copy this jar file first to java’s. Where your java is installed. Java’s lib folder. This is my folder. Let me tell you, this is the location. Program Files/Java/jdk1.8.0_11/lib. Here, you have to copy that jar file. So, it is copied here. You need administrator’s privileges to copy
that. First thing is that. Secondary, you have to copy it into the Project
that you are creating. So, we have to go to NetBeans. Here, I want this. Do one thing. Right click on the project, go into the Properties. We will get this Project Properties window,
go into the Library’s category. Here, in this Compile time Libraries dialog
box, you will find one button Add Jar/folder button. Click here. Select the jar file, wherever it is. Click here. Click on Open. And you will find the jar file is mentioned
here now. Click on OK. So, you will find that your jar file is copied
here. Now this is your project and it can access
the database tables in the project because, now through this jar file, your Java code
will be able to communicate with MySQL content. Let us see it how it is done. We will add one JFrame form . We are making
GUI project to see how the database is accessed. On the package node, right click it. Click on New, JFrame . So, give the any name
to this JFrame that is accessing or say DisplayingCustomers. Make it Finish. So, this is your JFrame opened. Right? Because you want to show the content of the
customers table , you need a Table element here. So, drag and drop one Table element on the
JFrame window. Here it is. It is very simple to connect your database
now. Go to Services, this is customers table here. Click and just drop it on the table. That’s it. It is connecting it. The JTable element is connected with your
customers table . And the proof is that. Can you see the two columns of the customers
table being displayed here? Let us run it and check it whether really
it is connected or not. Right click on it and click on Run File option. Can you see that? The rows that you entered in the customers
table are displayed. That means your MySQL and NetBeans are connected
and your project is enabled to access the content. Mail me at [email protected] and for watching
more video tutorials, search for bintu harwani . There are lots of videos for you. Thank you so much. Have a nice day.

Danny Hutson

4 thoughts on “How to Connect Java With MySQL database in NetBeans

  1. sir i have error:java.math.BigInteger cannot be cast to java.lang.long at mysql server properties in netbeans

Leave a Reply

Your email address will not be published. Required fields are marked *