How to Create Login Form in Java-9 using SQL Server 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 SQL Server 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 SQL Server, how to drag and drop controls like panel, textbox, label and Button.
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:
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 form. 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. Create one database in SQL server named loginnew and create one table named loginnew with two column name username and password with datatype varchar and then save the table. now right click on the table and click on Edit Top 200 Rows. New window will appear then give the username and password there:
8. Now you have to connect ODBC driver for SQL Server. For this go to: Control Panel > Administrator Tools > ODBC Data Source (64 -bit). After clicking on ODBC Data Source new window will appear then click on add. select SQL Server from the option. Then give Name and select Server. Click next and next then you have to select your database you have created in SQL Server. For this select the change the default database to check box and select the database. Finally click on finish.
9. Now go back to the NetBeans IDE and and go to your project. you have created the login form double click on Login Button and write the following code:
Connection con=null; ResultSet rs = null; PreparedStatement pst=null;
Write the above code inside: public class loginform extends javax.swing.JFrame
and write the following code by double clicking on Login Button:
String sql ="select * from login where username=? and password=?"; try { con=DriverManager.getConnection("jdbc:sqlserver://RAN-PC:1433;user=sa;password=******;DatabaseName=login"); //write your pc name, username and password of sql server //"jdbc:sqlserver://YOUR-PC-NAME-HERE;user=DATABASE-USERNAME-HERE;password=DATABASE-PASSWORD-HERE;DatabaseName=DATABASE-NAME-HERE" pst =con.prepareStatement(sql); pst.setString(1,username.getText()); pst.setString(2,password.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); }
10. Now download Microsoft JDBC Driver 6.0 for SQL Server from here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
11. After downloading, extract that file and copy the extracted file in C:\Program Files:
12. Now from the left side of your Netbeans IDE under the project sub menu you will see the libraries right click on libraries and then click on add JAR/Folders and add the sqljdbc4.jar from the C:\Program Files\Microsoft JDBC Driver 6.0 for SQL Server\sqljdbc_6.0\enu and then click open.
13. After this, right click on the Jframe and click on Run Files:
14. Now the login form will appear, if you enter the username and password that is enter in the database you will see the message username and password matched otherwise you will see username and password do not matched.
[/sociallocker]
If you confused then you can watch this video for more details:
Thank you so much for reading and watching this video, if you are new to this channel subscribe that channel also share this article. For more stay connected to this blog. if you encounter some error related to this article please let me know comment the issues in the comment section below.