Java форма ввода пароля

Java Guides

In this tutorial, we will learn how to create a simple Login application using Java Swing and we authenticate login user with a database using JDBC and MySQL.

Video Tutorial

Tools and Technologies used

What we’ll build?

Database Setup

create database swing_demo;
CREATE TABLE student ( id int NOT NULL, name varchar(250) NOT NULL, password varchar(250) );
INSERT INTO student (id, name, password) VALUES (1, 'Ramesh', 'Ramesh@1234');

Create a Simple Java Project

Let’s create a simple Java project in Eclipse IDE, name this project as «swing-registration-from-example».

Connecting With Database

In order to connect our Java program with the MySQL database, we need to include MySQL JDBC driver which is a JAR file, namely mysql-connector-java-8.0.13-bin.jar.

Develop User Login Form

package com.javaguides.javaswing.login; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class UserLogin extends JFrame < private static final long serialVersionUID = 1L; private JTextField textField; private JPasswordField passwordField; private JButton btnNewButton; private JLabel label; private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) < EventQueue.invokeLater(new Runnable() < public void run() < try < UserLogin frame = new UserLogin(); frame.setVisible(true); > catch (Exception e) < e.printStackTrace(); > > >); > /** * Create the frame. */ public UserLogin() < setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(450, 190, 1014, 597); setResizable(false); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("Login"); lblNewLabel.setForeground(Color.BLACK); lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46)); lblNewLabel.setBounds(423, 13, 273, 93); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.PLAIN, 32)); textField.setBounds(481, 170, 281, 68); contentPane.add(textField); textField.setColumns(10); passwordField = new JPasswordField(); passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32)); passwordField.setBounds(481, 286, 281, 68); contentPane.add(passwordField); JLabel lblUsername = new JLabel("Username"); lblUsername.setBackground(Color.BLACK); lblUsername.setForeground(Color.BLACK); lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 31)); lblUsername.setBounds(250, 166, 193, 52); contentPane.add(lblUsername); JLabel lblPassword = new JLabel("Password"); lblPassword.setForeground(Color.BLACK); lblPassword.setBackground(Color.CYAN); lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 31)); lblPassword.setBounds(250, 286, 193, 52); contentPane.add(lblPassword); btnNewButton = new JButton("Login"); btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 26)); btnNewButton.setBounds(545, 392, 162, 73); btnNewButton.addActionListener(new ActionListener() < public void actionPerformed(ActionEvent e) < String userName = textField.getText(); String password = passwordField.getText(); try < Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root", "root"); PreparedStatement st = (PreparedStatement) connection .prepareStatement("Select name, password from student where name=? and password=?"); st.setString(1, userName); st.setString(2, password); ResultSet rs = st.executeQuery(); if (rs.next()) < dispose(); UserHome ah = new UserHome(userName); ah.setTitle("Welcome"); ah.setVisible(true); JOptionPane.showMessageDialog(btnNewButton, "You have successfully logged in"); > else < JOptionPane.showMessageDialog(btnNewButton, "Wrong Username & Password"); > > catch (SQLException sqlException) < sqlException.printStackTrace(); > > >); contentPane.add(btnNewButton); label = new JLabel(""); label.setBounds(0, 0, 1008, 562); contentPane.add(label); > >

In this above example, we no need to register a JDBC driver because since Java 1.6 and JDBC 4.0 API, it provides a new feature to discover java.sql.Driver automatically, it means the Class.forName is no longer required. Just put any JDBC 4.x driver in the project classpath, and Java is able to detect it.

Читайте также:  My First JavaScript Program!

Develop User Home Page

package com.javaguides.javaswing.login; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; public class UserHome extends JFrame < private static final long serialVersionUID = 1 L; private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) < EventQueue.invokeLater(new Runnable() < public void run() < try < UserHome frame = new UserHome(); frame.setVisible(true); > catch (Exception e) < e.printStackTrace(); > > >); > public UserHome() < >/** * Create the frame. */ public UserHome(String userSes) < setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(450, 190, 1014, 597); setResizable(false); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnNewButton = new JButton("Logout"); btnNewButton.setForeground(new Color(0, 0, 0)); btnNewButton.setBackground(UIManager.getColor("Button.disabledForeground")); btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 39)); btnNewButton.addActionListener(new ActionListener() < public void actionPerformed(ActionEvent e) < int a = JOptionPane.showConfirmDialog(btnNewButton, "Are you sure?"); // JOptionPane.setRootFrame(null); if (a == JOptionPane.YES_OPTION) < dispose(); UserLogin obj = new UserLogin(); obj.setTitle("Student-Login"); obj.setVisible(true); > dispose(); UserLogin obj = new UserLogin(); obj.setTitle("Student-Login"); obj.setVisible(true); > >); btnNewButton.setBounds(247, 118, 491, 114); contentPane.add(btnNewButton); JButton button = new JButton("Change-password\r\n"); button.setBackground(UIManager.getColor("Button.disabledForeground")); button.addActionListener(new ActionListener() < public void actionPerformed(ActionEvent e) < ChangePassword bo = new ChangePassword(userSes); bo.setTitle("Change Password"); bo.setVisible(true); > >); button.setFont(new Font("Tahoma", Font.PLAIN, 35)); button.setBounds(247, 320, 491, 114); contentPane.add(button); > >

Develop Change Password Form

package com.javaguides.javaswing.login; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class ChangePassword extends JFrame < private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField textField; private JLabel lblEnterNewPassword; /** * Launch the application. */ public static void main(String[] args) < EventQueue.invokeLater(new Runnable() < public void run() < try < >catch (Exception e) < e.printStackTrace(); > > >); > /** * Create the frame. */ public ChangePassword(String name) < setBounds(450, 360, 1024, 234); setResizable(false); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.PLAIN, 34)); textField.setBounds(373, 35, 609, 67); contentPane.add(textField); textField.setColumns(10); JButton btnSearch = new JButton("Enter"); btnSearch.addActionListener(new ActionListener() < public void actionPerformed(ActionEvent e) < String pstr = textField.getText(); try < System.out.println("update password name " + name); System.out.println("update password"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root", "root"); PreparedStatement st = (PreparedStatement) con .prepareStatement("Update student set password=? where name=?"); st.setString(1, pstr); st.setString(2, name); st.executeUpdate(); JOptionPane.showMessageDialog(btnSearch, "Password has been successfully changed"); > catch (SQLException sqlException) < sqlException.printStackTrace(); > > >); btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 29)); btnSearch.setBackground(new Color(240, 240, 240)); btnSearch.setBounds(438, 127, 170, 59); contentPane.add(btnSearch); lblEnterNewPassword = new JLabel("Enter New Password :"); lblEnterNewPassword.setFont(new Font("Tahoma", Font.PLAIN, 30)); lblEnterNewPassword.setBounds(45, 37, 326, 67); contentPane.add(lblEnterNewPassword); > >

Demo

Let’s run the UserLogin.java file and the below screenshot shows the step by step execution of this application.

Источник

Читайте также:  Java char to number one

How can we create a login form in Java?

We can develop a login form in Java using Java Swing technology. In this example, we can create two labels username and password, two text fields for the user to enter valid credentials and finally one submit button. Once the user is able to enter the valid credentials in the two text fields, we can able to see Hello admin in the login form.

Example

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginDemo extends JFrame implements ActionListener < JPanel panel; JLabel user_label, password_label, message; JTextField userName_text; JPasswordField password_text; JButton submit, cancel; LoginDemo() < // Username Label user_label = new JLabel(); user_label.setText("User Name :"); userName_text = new JTextField(); // Password Label password_label = new JLabel(); password_label.setText("Password :"); password_text = new JPasswordField(); // Submit submit = new JButton("SUBMIT"); panel = new JPanel(new GridLayout(3, 1)); panel.add(user_label); panel.add(userName_text); panel.add(password_label); panel.add(password_text); message = new JLabel(); panel.add(message); panel.add(submit); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Adding the listeners to components.. submit.addActionListener(this); add(panel, BorderLayout.CENTER); setTitle("Please Login Here !"); setSize(450,350); setVisible(true); >public static void main(String[] args) < new LoginDemo(); >@Override public void actionPerformed(ActionEvent ae) < String userName = userName_text.getText(); String password = password_text.getText(); if (userName.trim().equals("admin") && password.trim().equals("admin")) < message.setText(" Hello " + userName + ""); >else < message.setText(" Invalid user.. "); >> >

Источник

Оцените статью