How To Connect Mysql Database in Java Using Netbeans
How To Connect Mysql Database in Java Using Netbeans
Hello Friends, Welcome to another tutorial. In this tutorial, we will learn to connect MySQL
to NetBeans in Java step by step. Most of the time, it usually happens that we face a lot of
problems while connecting our program to the database, but if you follow this tutorial
properly, then you will be able to connect the Java program with the MySQL database using
NetBeans quite easily.
• The first thing you need to do is download and install the NetBeans IDE in your system.
If you have already installed the NetBeans IDE in your system, then you can skip this
part and move forward.
• Go to the official website of Apache NetBeans to download the latest version of
NetBeans IDE by clicking on this link.
https://netbeans.apache.org/download/index.html
• After going to the official website of Apache NetBeans, you will see a download button
to download the latest version of NetBeans IDE for your system.
• You can see the figure below to understand.
www.tutorialsfield.com Page 1
• After clicking on the Download button, you will be redirected to the another web
page, and there you will have an option to download the IDE according to your
Operating System, as you can see in the figure below.
• I am Downloading the IDE for Windows Operating System 64-bit.
www.tutorialsfield.com Page 2
• After you click on the link, you will be again redirected to another web page and now
click on the link as shown in the figure below to start downloading the NetBeans
installer file.
• After clicking, the NetBeans installer file will start to download. Please sit back and
relax till it finishes.
• After downloading the file, locate the file's location in your system and install it the
same way you install any application.
Since you have finished downloading and installing the NetBeans IDE to your system, Now
we will create a database and table in MySQL.
• To create a database and table, you can either choose MySQL command-line client or
MySQL Workbench. MySQL workbench is nothing but a GUI version of the MySQL
console where we can visually design, model, and generate databases.
• If you don't know how to download and install MySQL command-line client and
MySQL Workbench to your system, then you can follow the links given below for the
proper installation process step by step.
www.tutorialsfield.com Page 3
How to Install MySQL
How
ow to Install MySQL Workbench
• I will use MySQL command and-line client in this tutorial, open MySQL
MyS command-line
client, and enter your passwo
ssword so that you can connect to the MySQ
ySQL database.
• This is the same passwordrd yyou provided while installing the MySQ
ySQL database in your
system.
• Now create a database nam amed “studentdb” and a table named “student”
“stud inside it with
columns.
o ROLLNO
o STUDNAME
o DEPT
o CITY
www.tutorialsfield.com Page 4
create database studentdb;
use studentdb;
create table student (ROLLNO varchar(10)not null, STUDNAME varchar(30), DEPT varchar(30),
CITY varchar(30));
• Open NetBeans and click on the New Project under the File Menu section, as shown in
the figure below. ( File>>New Project )
• After that, the New Project dialog box will be opened, where you have to choose the
type of project you want to build with NetBeans.
• Select Java with Ant in the Categories section and then select Java Application in the
Projects section.
• Then click on the Next button to proceed.
www.tutorialsfield.com Page 5
• In the next window, it will ask you to enter the project name. Give a name to your
project (JavaMySQLConnect in this example).
• Tick mark the Create main class option, and it will create the main class. You can also
edit the name of the main class if you want to.
• Lastly, click on the Finish button to complete, as shown in the figure below.
www.tutorialsfield.com Page 6
Adding MySQL connector jar filee in NetBeans
• In order to connect your ur Java program with the MySQL datab atabase, you need to
include MySQL JDBC driver,, whwhich is a JAR file, namely mysql-connec
nnector-java-8.0.27.jar.
The version number of the he Ja
Jar file may be different.
• To download the latest ver version of MySQL connector, you can visit
vis this link (MySQL
Connector Java download).
• After visiting the link, selec
select platform independent from the drop-down
dro menu, as
illustrated in the figure below.
elow.
www.tutorialsfield.com Page 7
• Then download the Platform
form Independent (Architecture Independe
endent), ZIP Archive as
shown in the figure below.
www.tutorialsfield.com Page 8
• After you click on the down
ownload button, you will be redirected to another web page
where you will be asked to login or sign up for the Oracle web account.
acc Since we are
only concerned about down he link No thanks, just
ownloading the file, you can click on the
start my download to start
rt do
downloading the file as shown in the figure
figu below.
www.tutorialsfield.com Page 9
• After downloading the zip archive, you need to extract it, and you will get the jar file.
• Next, you have to include this Jar file into your project so that you will be able to
connect your Java program with the MySQL database.
• Go to the Project panel window and expand your project, and there you will see the
Libraries folder under your project directory.
• Right-click on the Libraries folder, and you will see an option of "Add Jar/Folder.." click
on that.
www.tutorialsfield.com Page 10
• After you click on that optio
option, a new window will be opened, and now you have to
locate the location of thee My
MySQL connector jar file and after selecti
lecting the Jar file, click
on the Open button as shown
hown in the figure below.
• Now you will be able to see that the MySQL connector jar file has been successfully
added under the Librariess fo
folder, as shown in the figure below.
www.tutorialsfield.com Page 11
Connecting Java Program with MyS
MySQL Database
www.tutorialsfield.com Page 12
The second root is the password that you give while installing the MySQL
o
database. If your password is different, then provide that password at that place.
• SQL Exception might occur while connecting to the database, So you need to surround it
with the try-catch block.
• Programming Example is given below.
package javamysqlconnect;
} catch (SQLException e) {
}
}
www.tutorialsfield.com Page 13
Inserting Data into The MySQL Database using Java
As we have successfully connected the MySQL database with Java using NetBeans, Now it’s
time to insert some data into our table. If you are familiar with the Prepared Statement in
Java and about SQL INSERT query, then it will be a lot easier for you to add data to your table.
Let’s quickly look at the steps to insert data into the table.
• Create a PreparedStatement object and use SQL INSERT query to add data to the table.
• Specify the values for each parameter through the PreparedStatement object.
• Execute the query by calling the executeUpdate() method of the PreparedStatement object.
• Programming example is given below.
package javamysqlconnect;
import java.sql.*;
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root",
"root");//Establishing connection
System.out.println("Connected With the database successfully");
//Creating PreparedStatement Object
PreparedStatement preparedStatement =connection.prepareStatement("insert
into student values(?,?,?,?)");
//Executing Query
preparedStatement.executeUpdate();
System.out.println("Data inserted Successfully");
} catch (SQLException e) {
www.tutorialsfield.com Page 14
}
}
use studentdb;
select * from student;
www.tutorialsfield.com Page 15
Updating Data in The MySQL Database using Java
• To update data in the MySQL database, you have to use SQL UPDATE query as shown in the
programming example below.
package javamysqlconnect;
import java.sql.*;
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root",
"root");//Establishing connection
System.out.println("Connected With the database successfully");
//Using SQL UPDATE query to update data in the table
PreparedStatement preparedStatement =connection.prepareStatement("update
Student set CITY=? where ROLLNO=?");
//Updating Value
preparedStatement.setString(1,"Delhi");
preparedStatement.setString(2,"1");
//Executing Query
preparedStatement.executeUpdate();
System.out.println("Data Updated Successfully");
} catch (SQLException e) {
}
}
www.tutorialsfield.com Page 16
• To check whether the data hass be
been updated in the table or not, Open
n the
th MySQL database,
connect it by entering the passwo
sword and write the following query.
use studentdb;
select * from student;
www.tutorialsfield.com Page 17
• In order to get the results, we will use the Java ResultSet object.
• Programming Example is given below.
package javamysqlconnect;
import java.sql.*;
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root",
"root");//Establishing connection
System.out.println("Connected With the database successfully");
//Using SQL SELECT Query
PreparedStatement preparedStatement =connection.prepareStatement("select
* from student");
//Getting Results
while(resultSet.next()){
String rollNo=resultSet.getString("ROLLNO");
String name=resultSet.getString("STUDNAME");
String dept=resultSet.getString("DEPT");
String city=resultSet.getString("CITY");
//Printing Results
System.out.println("Roll no = "+rollNo);
System.out.println("Name = "+name);
System.out.println("Department = "+dept);
System.out.println("City = "+city);
}
} catch (SQLException e) {
}
}
www.tutorialsfield.com Page 18
Deleting Data from the MySQL Database using Java
• To delete data from the table, we will use the SQL DELETE query.
• Programming Example is given below.
package javamysqlconnect;
import java.sql.*;
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root",
"root");//Establishing connection
System.out.println("Connected With the database successfully");
//Using SQL DELETE query to delete data
PreparedStatement preparedStatement =connection.prepareStatement("delete
from student where STUDNAME=?");
//Setting value
preparedStatement.setString(1,"Mehtab");
//Executing Query
preparedStatement.execute();
System.out.println("Data deleted successfully");
} catch (SQLException e) {
}
}
www.tutorialsfield.com Page 19
• Now run your program.
use studentdb;
select * from student;
Conculusion
www.tutorialsfield.com Page 20
Finally, We have concluded that connecting the MySQL database to Java using NetBeans IDE is
not as difficult as we think. All we need is to follow the steps correctly.
Now I am wrapping up this tutorial, connect MySQL to NetBeans in Java and feel free to
comment down below if you have any queries regarding this post. To get these types of
awesome posts, stay tuned with Tutorials Field. HAPPY CODING!!!
www.tutorialsfield.com Page 21