Skip to content

How To – Create Login Menu Using Java Netbeans 8.1

This time i’ll share about create menu login using java netbeans, with requirements:

– OpenJDK 8u91
– Netbeans 8.1
– Kde Plasma
– Slackware-Linux current x86_64 version

This tutorial is simple for beginners and i also, i’m still learn about world progamming.

In this post i use MySql for database, before going to discuss first create database as follows:

The database name is logindb and for table name is login. In column i fill user “admin” and password “admin”.

After create now open netbeans, when netbeans opened click create new package and choose java application give a project name which you want.

After create package, now right click in java package then select new -> JPanel Form. Modify your form login as you like, you can like here:

After designed or modified, before to code first add libraries with right click in libraries and choose mysql jdbc driver.

After adding libraries mysql jdbc driver, now create class like this screenshot:

The class name i namely “Class”, then copy paste this script into your class:

package rbdb;

import com.mysql.jdbc.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

/**
*
* @author arief-jr
*/
public class Conn {
private static Connection rbdb;

public static Connection GetConnection () Throws SQLException{
if (rdbd==null){
new Driver();

rbdb=DriverManager.GetConnection("jdbc:mysql://localhost:3306/logindb","root","12345");

}
return rbdb;
}
}

The explanation this script is call package rbdb because the name package is rbdb, call variable like here and create a connection to my database. In this case my database user is root and password is 12345.

After create the class, now go back to form 1 then type is code:

package rbdb;

import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import rbdb.Conn;

/**
*
* @author arief-jr
*/
public class Login extends javax.swing.JFrame {

/**
* Creates new form Login
*/
public Login() {
initComponents();
}

private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}

private void BtnLoginActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Statement statement=(Statement)Conn.GetConnection().createStatement();
ResultSet result=statement.executeQuery("select*from logindb where"+"user='"+TxtUser.getText()+"'");
if(result.next()){
if(TxtPass.getText().equals(result.getString("pwd"))){
new Menu().show();
this.dispose();
}else {
JOptionPane.showMessageDialog(rootPane,"Wrong Password");
TxtPass.setText("");
TxtUser.requestFocus();
}
}else{
JOptionPane.showMessageDialog(rootPane,"User Not Found");
TxtUser.setText("");
TxtPass.setText("");
TxtUser.requestFocus();
}
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane,"Failed");
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//


/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton BtnCancel;
private javax.swing.JButton BtnLogin;
private javax.swing.JPasswordField TxtPass;
private javax.swing.JTextField TxtUser;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

Explanation: The above script is for form 1 i.e login, in script new Menu().show() for open menu if success login, so i called Menu in form2. This optionally, you can create or not.

This my discuss about create form login using java netbeans, so if you less understood about this post or if in the post not plain you can ask me with box comment in below this post. If i wrong please correct me, Thanks!

Leave a Reply