How to Create a Login Form in Java using MySQL Database and NetBeans IDE?[With Source Code]
Hello guyz, Today i am here with totally new topic. Today i am going to write about how to create a login form in java-9 using MySQL Databse and NetBeans IDE 8.1. This is a simple project in java but this is the most important stage you have to learn if you want to create a more complex project in java. This article will show you how to connect NetBeans IDE to the MySQL Database, how to drag and drop controls like panel, textbox, label and Button. Download Netbeans IDE and any Server Application.
i am using wamp server so:
Wamp server: http://www.wampserver.com/en/
Netbeans IDE: https://netbeans.org/downloads/
It is important for all project to connect them to the database to access some files or to store some information back to the database. So, This article will show you all the basic things that you should know to create a big project. I am going to explain each and every steps here:
[sociallocker]
1. First of all you have to install Java JDK or JRE in your PC, it’s better in install JDK so download and install it from here: https://jdk9.java.net/download/
2. after you install it, change the environment variable of your system by going: Control panel > System and then click on advance system setting from left side of that window. After clicking on advance system setting a new window will appear then you have to click on Environment Variables. After this, you will obtain new window and you will see the path in the bottom of the window and double click on the path. After this click on new tab and copy the java path from C where you have install the java. In my case my java path is this : C:\Program Files\Java\jdk-9\bin\ and then click on OK. which will looks like this:
How to Create Login From in Java
3. Now open Netbeans IDE and click on file > new project. Select categories as java and project as java application from that window:
4. Click next and then give your project name and then click finished.
5. Now right click on your project and select new > JFrame Form:
6. After this new window will appear from this window drag Panel and extend it, and drag two label, two Text Field, one Button and drop to the jframe from. change the name of the label to username and password and change the variable name of the Text Field by right clicking it and named it as user and pass respectively which will looks like this:
7. After download and installing wamp server open it, and then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty). And then click on new and create one database having any name you want and the click ok, then create table having two field in and click on go. After that, you need to insert the field name like: username and password. and then select the length as well as data type as your requirement and then click on save. You can watch the video for this designing part, you may understand better in video.
8. Now you need to have MySQL connector for java so download it.
https://dev.mysql.com/downloads/connector/j/
9. Now double click on login button, after doing this you will go to the source code file. Scroll up and find the main class of your project and under that class declare these variable:
Connection con = null; PreparedStatement pst = null; ResultSet rs = null;
10. Goto the jframe form and then double click on the login button and write the following piece of code and run your project by right clicking the the jframe form from the project explorer and then click on run file but before running your file you need to add the MySQL Connector for java. So right click on the libraries folder from the project explorer, then click on add JAR/folder and select that downloaded jar file and click on add.
String sql="select * from login where username=? and password=?"; try{ con = DriverManager.getConnection("jdbc:mysql://localhost/testsql","root",""); pst=con.prepareStatement(sql); pst.setString(1, user.getText()); pst.setString(2,pass.getText()); rs=pst.executeQuery(); if(rs.next()) { JOptionPane.showMessageDialog(null,"username and password matched"); } else { JOptionPane.showMessageDialog(null, "username and password do not matched"); } } catch(SQLException | HeadlessException ex) { JOptionPane.showMessageDialog(null,ex); }
That’s it guyz.
[/sociallocker]
if you have any confusion, you can watch the following video: