Connecting Java Application to MySQL Database in NetBeans

Since I have started blogging, I have gotten well versed with web development languages like PHP & MySQL along with HTML, CSS and Javascript. So, for the last couple of weeks I have been trying out Java, specifically Java Applications which are standalone programs that can run on any computer provided you have JDK installed.

The quickest way to learn a new programming language is to do projects on them. A while back, I did a PHP project. So, my main goal was to implement the same project using Java. It is easier because the database structure would remain same, only the GUI would be implemented in Java.

Creating a Java Application

So, the first thing that I needed to do was creating a Java Application project. To me, NetBeans and Eclipse are more or less same. Anyway I went with NetBeans. So, the first step would be to create a Java Application project.

java application

Creating a Java Application Project

Make sure when you enter the name of the project, you disable the option of creating the main class of the project. This would allow you to choose a main class for the project during the building of the project.

Adding JFrame to the Project

Java applications generally work using JFrames. So, In the source package of your folder, create a JFrame Form which will be the login form in this demonstration.

java application

Adding JFrame to the Project

Once you have generated the JFrame form ( say login.java ) , you add the swing components for the validation of username and password.

Create the GUI

You can create a very basic GUI for the login screen of your project like I have done below.

java application

Creating the GUI

Testing MySQL Connection

After creating the GUI the only thing left is making the database connection. But, before making the database connection, you can test the connection. And for that you don’t need to write any code.

Netbeans already has the MySQL Connector installed. It is nothing but a JAR file which enables your application to connect with MySQL database. In Eclipse however, you would need to add it as an external JAR.

But make sure you have a MySQL server installed before doing so. You can basically create the database and the tables beforehand (which I prefer). One of the reasons I don’t like creating databases and tables during runtime is because you don’t have full control over it. You never know how many of them are being created and before you know it, you will have used up a lot of unnecessary space. It can also data redundancy which you don’t want.

For example what I have done here is created a database name ‘javademo’ and added a table known as ‘login’ which has 2 columns ‘username’ and ‘password’ both of which has values ‘admin’.

java application

Database and Table Creation in MySQL

Now go to Services and in the Drivers section you will find the MySQL Connector option. Right click on it and click on ‘Connect Using’.

java application

Testing MySQL Connection

Writing the Code

Since the connection was successful, now we can write the code. But before that you need to add the MySQL connector library to the project [ see image below ]. In the project, you will see a ‘Libraries’ option. Right click on it, and select ‘Add Library’. You will get a list of all the libraries netbeans has. Select ‘MySQL JDBC Driver’ from the list.

java application

Adding the JDBC Driver

For a simple login screen like this, we basically have to write very small and very simple code. Component wise here’s what I used –

  • JTextField for Username – jTextField1
  • JPasswordField for Password – jPasswordField1
  • Submit button – jButton1
  • Cancel button – jButton2

Any event occurs on the press of any one of the buttons. First the submit button which will validate the login with data stored in the MySQL database. So, double click on the button and Netbeans will take you straight to the code.

I’m also using a custom function which will validate the login.

You can also add a code to the Cancel button which will close the button when someone presses it.

Final Output

So, now you can run your project. If the field are blank when submit is clicked it will show a message. And it will also show messages for both correct and incorrect validation.

java application

Author: Lahaul Seth

A programmer and a web developer, he is the founder of Lion Blogger. His main hobbies are web design & development and providing writing services to clients.

Share This Post On

20 Comments

  1. I didn’t get the full code for the password snippet, pls can you help?

    Post a Reply
  2. Thank you so much.

    Post a Reply
  3. Thank you so much it is very helpfull for me..

    Post a Reply
  4. am doing a java application ,i need to connect to the cloud database ,how can i connect my application with cloud .is it possible ? if yes can you give my codes for connecting,if you don’t mind please tell me the configuration if any,
    i tried to connect but it showing access denied!please show me how to do exactly please

    Post a Reply
  5. how it wokr with 3 Tables?? first is Profile(user, pwd) Tabel secont File and last Document.
    Profile 1:n file 1:n Document. but file must stay so long as programm runing or logout. MVC Pattern well be gread DAOs and Bean prefer.

    Please help me i looking at 3 Month on it.

    Post a Reply
  6. please where do i put the validate_login() method??

    Post a Reply
  7. I don’t understand this one.
    PreparedStatement pst = conn.prepareStatement(“Select * from login where username=? and password=?”);

    What code should I input to the ‘username = ?’ and ‘password = ?’ ?

    Post a Reply
    • Ega, If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead.
      The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created. The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled.

      Post a Reply
  8. Fantastic , it works after few corrections
    Thanks

    Post a Reply
    • hi, how did you correct some of the errors? did you add some package to handla the connection conn, prepareStatement and ResultSer?

      Post a Reply
  9. Nice Article.. But please guide me, How to use a database of Mysql developed on one system into another system without recreating the database in the new system and use it as earlier when it was connected
    with netbeans

    Post a Reply
    • You can export the MySQL DB from phpMyAdmin and import it to another system. A ‘.sql’ file is created once you export the DB. Use the same file to import the DB on another system. If your code is correct w.r.t DB connection and SQL queries, then it should work.

      Post a Reply
  10. i am not getting the jdbc driver option when I click add library. Any idea?

    Post a Reply
    • You will have to manually download MySQL connector library and add it to your library path. Search on Google. You should get it easily.

      Post a Reply
  11. but there’s an error on my system

    Post a Reply
  12. I’m getting incorrect message every time even on giving the right password.

    Post a Reply
  13. nice incomplete/error tutorial

    Post a Reply
  14. i am getting error by adding validate_login code will you please show me where i have to place this code .

    Post a Reply

Submit a Comment

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

Pin It on Pinterest

Share This