Java Database Connectivity in NetBeans IDE and MYSQL workbench
By- Vikrant thakur (Data Science Student)
For development of any kind of application like web application, Mobile application or any other real time software a good connection between Project (Front End) and its Database is required. JDBC (Java Database Connectivity) is the process of establishing a connection between application interface development in an IDE and database through a server.
The JDBC interface consists of two layers:
1) The JDBC API supports communication between the Java application and the JDBC manager.
2) The JDBC driver supports communication between the JDBC manager and the database driver.
Before JDBC, ODBC API was the database API to connect and execute the query with the database. But ODBC API uses ODBC driver which was written in C programming language (i.e. platform dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
We can use JDBC API to handle database using Java program and can perform the following activities:
- Connect to the database
- Execute queries and update statements to the database
- Retrieve the result received from the database
Java is one of the most used general programming languages for such purpose in this world. Most of the modern application’s are developed in java. There are many environments used to utilize the power of java. Some of them include- Android studio, NetBeans, Eclipse and many more. MySQL workbench is one tool which is used to create and manage database using SQL.
To understand JDBC in java NetBeans we will development a simple java application. Following are the steps for the same-
Step 1 — Installing all required tools and libraries
Step 2 — Developing User-interface Login page in database
Step 3- Creating a proper database in MySQL workbench
Step 4 — Establishing connection though JDBC
Requirements-
1) Java JDK- Latest
2) Java NetBeans
3) MySQL workbench
4) JDBC Driver and MYSQL connection libraries
We will use make use of some features of NetBeans which one should know before beginning-
1) Jframe- For user interface development
2) Java swing & applet- Provides User-Interface components, services
3) Services- To start and stop external services
After successful installation and setup of the above tools let’s start our project
- Create a new Java application project named- ‘Loginpage’
2) Now, a java file will be created for the application. So, click on the files at the left of the IDE and right click on the source option and then choose Jframe.
You will see a new page is added to the java project.
Next, we will simply Drag and drop labels and TextField and one button for login page as shown below- (Note — Adding image is optional)
Also create a new Jframe again same as previously and name it as ‘user’.
After this we will simply create a user table in new database in MySQL Workbench containing two columns username and password.
Below SQL query for the same is given-
>> Create table user(name varchar(20), password varchar(20));
>>insert into user values(‘vikrant’, ‘123456’);
After this click on project at the left of the NetBeans IDE and then right click on the libraries option and then add the MYSQL JDBC driver file after downloading from the above given link.
Download MySQL JDBC Driver https://dev.mysql.com/downloads/connector/j/
In the Jform we created simply right click on the button and navigate in Event>>Action>>Action performed after which you will be redirected towards the source code file of the Jform.
After this add the following code there with changes according to your file names.
Let’s understand the above code starting from meaning of objects Connection, Statement, and ResultSet.
‘Class.forName(com.mysqk.jdbc.Driver);’ — This calls the JDBC MySQL driver in actions
Connection –
To provide information of the target database and table with its details for access.
Statement –
It Includes the SQL query to be implemented
‘java.sql.Statement’ is an interface. It provides some methods through which we can submit SQL queries to the database.
ResultSet –
‘java.sql.ResultSet’ also an interface and is used to retrieve SQL select query results
We have simply used IF-Else loop to authenticate user from the information given in the TextField by user.
We have successfully provided the target database details and how to compare the values stored in JTextField’s by the user and user table values (username and password). If the JDBC connection is successfully established then the code under ‘If’ section of the above code will run.
‘new user (). setVisible(true)’ is the code to open user page. For that you have to create another Jframe in the same project named user which will then automatically open on successful login authentication of user.
Note- Don’t forget to add ‘Import java.sql. *’ at the top of the source file to avoid error as it provides all SQL features like Connection, ResultSet and others
Finally run your application and test the result.
YouTube Link- https://www.youtube.com/watch?v=jwIRhkvflSU
GitHub Source code- https://github.com/vikrant65-byte/MovieRecomendationinpython.git
For any help or assistance connect with me at my LinkedIn account -