RSS

Login Form with database in JAVA Swing

29 Noy

Bugün sizə mysql ile login nümünəsini göstəəcəm.

1.ilk öncə users adinda bir table olmalidı

2.users.java adinda class

3.DBConnection (mysql)

4.LoginPage  (Jframe) login form səhifəmiz.

Table yaradiriq.

create table mydb.users (
id int AUTO_INCREMENT Primary key,
username varchar(30) ,
password varchar(30)
);

table

Users.java classimizi yaradib Construktor və get set  metodlarini yazırıq.

package aqil.isa;

/**
*
* @author AqilI
*/
public class Users {
private int id;
private String username;
private String password;

public Users() {
}

public Users(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}

Connection classımızı yaradib login methodumuzu yaziriq.

package aqil.isa;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* @author AqilI
*/
public class DBConnection {

private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private String user = “root”;
private String password = “sizin şıfrəniz“;
private String url = “sizin mysqlDB url“;

public void getConnection() throws SQLException {
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException cnf) {
cnf.printStackTrace();
System.out.println(“Mysql Db baglanmada xeta: ” + cnf.getMessage());
}
}

public void closedConnection() {
try {
if (conn != null) {
conn.close();
}
if (ps != null) {
ps.close();
}
} catch (Exception e) {
System.out.println(“Connection Baglanmadi! ” + e.getMessage());
e.printStackTrace();
}
}

public boolean usersLogin(String username, String password) {
Boolean b = false;
try {
getConnection();
String sql = “select * from mydb.users where username= ‘” + username + “‘ and password= ‘” + password + “‘”;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
return rs.next();

} catch (SQLException ex) {
ex.printStackTrace();
System.out.println(“User Login xeta ” + ex.getMessage());
return false;
} finally {
closedConnection();
}

}
public static void main(String[] args) {
DBConnection db= new DBConnection();
System.out.println(db.usersLogin(“aqil“, “1234“));
}

}

//test edek gore bazaya qoşula bilirik.

// DBConnection classımızı run edirik.rsult

True qaytardi demeli metodumuz duzgun çalışır.

artıq login pəncərəmizi hazırlayib login ola bilərikş hazirlayaq.

package aqil.isa;

import java.awt.Color;
import javax.swing.JOptionPane;

/**
*
* @author AqilI
*/
public class LoginPage extends javax.swing.JFrame {
DBConnection dbc = new DBConnection();
public LoginPage() {
initComponents();
setLocationRelativeTo(this);
setTitle(“Login Page Example”);
}
@SuppressWarnings(“unchecked”)
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtUsername = new javax.swing.JTextField();
txtPassword = new javax.swing.JPasswordField();
btnLogin = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(91, 113, 135));

jLabel1.setFont(new java.awt.Font(“Times New Roman”, 1, 14)); // NOI18N
jLabel1.setForeground(java.awt.SystemColor.window);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/itmim/images/user.png”))); // NOI18N
jLabel1.setText(“Username”);

jLabel2.setFont(new java.awt.Font(“Times New Roman”, 1, 14)); // NOI18N
jLabel2.setForeground(java.awt.SystemColor.window);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/itmim/images/password.png”))); // NOI18N
jLabel2.setText(“Password”);

btnLogin.setFont(new java.awt.Font(“Times New Roman”, 1, 14)); // NOI18N
btnLogin.setForeground(java.awt.SystemColor.window);
btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/itmim/images/sign.png”))); // NOI18N
btnLogin.setText(“Login”);
btnLogin.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
btnLogin.setContentAreaFilled(false);
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(97, 97, 97)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(38, 38, 38)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addComponent(txtPassword))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(174, Short.MAX_VALUE)
.addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(168, 168, 168))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txtPassword)
.addGap(1, 1, 1)))
.addGap(18, 18, 18)
.addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);

jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
if (dbc.usersLogin(txtUsername.getText().trim(), txtPassword.getText().trim())) {
txtUsername.setForeground(Color.GREEN);
txtPassword.setForeground(Color.GREEN);
JOptionPane.showMessageDialog(rootPane, “Your login successful!”);
} else {
JOptionPane.showMessageDialog(rootPane, “Your login failed!”);
txtUsername.setText(“”);
txtPassword.setText(“”);
}
}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(() -> {
new LoginPage().setVisible(true);
});
}

// Variables declaration – do not modify
private javax.swing.JButton btnLogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField txtPassword;
private javax.swing.JTextField txtUsername;
// End of variables declaration
}

login page görüntü.

txtuser

test edek:

Advertisements
 
3 Şərh

Müəllif Açıq 29 Noyabr 2016 in SWİNG

 

Login Form with database in JAVA Swing” üçün 3 cavab

  1. Anonim

    29 Noyabr 2016 at 11:38

    ela;

    tərəfindən bəyənildi 1 şəxs

     
  2. Coşqun Nağızadə

    02 Fevral 2017 at 21:59

    very nice , beautiful )))

    Bəyən

     

Bir şərh yazın

Sistemə daxil olmaq üçün məlumatlarınızı daxil edin və ya ikonlardan birinə tıklayın:

WordPress.com Loqosu

WordPress.com hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

Twitter rəsmi

Twitter hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

Facebook fotosu

Facebook hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

Google+ foto

Google+ hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

%s qoşulma

 
%d bloqqer bunu bəyənir: