Okhunjon Gaibullayev's blog

Java dasturlash tilida dastur tuzamizmi?

Java ilova


Salom blogimning o’quvchilari! Yaqinda universitetdan Java dasturlash tilida qandaydir loyiha tuzish to’g’risida topshiriq berildi. Men ham ishga kirishib bir nimalar tayyorladim.

Ushbu maqolada manashu tuzgan dasturimning kodlarini taqdim etmoqchiman. O’ylaymanki bu ko’pchilik dasturlashga qiziqadiganlarga ma’qul ish bo’ladi.

Dasturni yozish jarayonida videoga olishni iloji bo’lmadi, shuning uchun kodlarni tanishtirib ya’ni har qaysi kodlar nima vazifa bajarishini ko’rsatib beradigan video yozdim, sababi video ko’rib o’rganish, tushinish oson deb o’ylayman.

Dasturda asosan ro’yxatdan o’tib shaxsiy kabinet ochish tizimi, agar kirish parolini unitib qo’ysa qayta tiklash tizimi ko’rsatilgan. Bunda MySql ma’lumotlar bazasi bilan Java dasturni ulash orqali shu tizimlar bajarilgan.

DIQQAT E’LON! Java dasturlash tillari bo’yisha video kurs tayyor. Video kurs haqidagi ma’lumotni quyidagi rasm ustiga bosib o’qishingiz mumkin: 

javani o`rganishning oson yo`llari

Quyida dasturning kodi va videoni joylashtirib qo’yaman. Videoning vaqti uzun bo’lgani uchun uni besh qismga bo’lib qo’ydim, sababi video ko’p davom etsa tomosha qilish zerikarli tuyilishi mumkin.

MainFrame – Asosiy klass (sinf) dastur ishga tushishni shu sinfdan boshlaydi.

import java.awt.Color;

import java.awt.Component;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.Icon;

import javax.swing.ImageIcon;

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;

public class MainFrame extends JFrame {

private JLabel label1,label2;

private JTextField username;

private JPasswordField passwordfield;

private JButton createAccount;

private JButton forgetPassword;

private JButton btnlogin;

private JPanel panel;

JFrame frame;

public static void main(String[] args) {

MainFrame object=new MainFrame();

}

public MainFrame() {

super(«Okhunjon»);

getContentPane().setLayout(null);

getContentPane().setBackground(Color.WHITE);

label1=new JLabel(«Enter username:»);

label1.setBounds(100, 30, 140, 20);

getContentPane().add(label1);

username =new JTextField();

username.setBounds(100, 50, 140, 20);

getContentPane().add(username);

username.setColumns(10);

label2=new JLabel(«Enter password:»);

label2.setBounds(100, 70, 140, 20);

getContentPane().add(label2);

passwordfield=new JPasswordField();

passwordfield.setBounds(100, 90, 140, 20);

getContentPane().add(passwordfield);

passwordfield.setColumns(10);

btnlogin=new JButton(«Log in»);

btnlogin.setBackground(Color.BLUE);

btnlogin.setForeground(Color.WHITE);

btnlogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

CheckuserNamePassword();

}

});

btnlogin.setBounds(100,120,70,28);

getContentPane().add(btnlogin);

forgetPassword=new JButton(«Forgot password?»);

forgetPassword.setBackground(Color.BLUE);

forgetPassword.setForeground(Color.WHITE);

forgetPassword.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

ForgetPassword forget= new ForgetPassword();

forget.setVisible(true);

dispose();

}

});

forgetPassword.setBounds(100,160,150,28);

getContentPane().add(forgetPassword);

createAccount=new JButton(«Create an Account»);

createAccount.setBackground(Color.BLUE);

createAccount.setForeground(Color.WHITE);

createAccount.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

RegistrationUser register = new RegistrationUser();

register.setVisible(true);

dispose();

}

});

createAccount.setBounds(100,200,150,28);

getContentPane().add(createAccount);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(600,500);

setVisible(true);

}

public void CheckuserNamePassword() {

String sql=«select * from Register where user_name=? and user_password=?»;

try {

Connection myConn= DriverManager.getConnection(«jdbc:mysql://localhost:3306/JavaIutProject»,«root»,«»);

PreparedStatement ps=myConn.prepareStatement(sql);

ps.setString(1, username.getText());

ps.setString(2, passwordfield.getText());

ResultSet rs=ps.executeQuery();

if(rs.next())

{

int id=rs.getInt(«ID»);

UserAccount user = new UserAccount(username.getText(),id);

user.setVisible(true);

dispose();

}else {

JOptionPane.showMessageDialog(null,«Username or password is wrong! Try again»,«», JOptionPane.ERROR_MESSAGE);

}

}catch(Exception exc) {

JOptionPane.showMessageDialog(null,exc);

exc.printStackTrace();

}

}

}

ForgetPassword – Kirish parolini unitib qo’yganda chaqiriladigan sinf.

import java.awt.Color;

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 java.sql.Statement;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class ForgetPassword extends JFrame{

private JLabel reset,reset2;

private JTextField first_n,second_n,user_n,e_mailaddress;

private JButton btnreset,btnback ;

Random parol=new Random();

String a=parol.toString();

int b=10000000+parol.nextInt(100000000)+parol.nextInt(10000000);

public ForgetPassword() {

super(«Password»);

 

getContentPane().setLayout(null);

getContentPane().setBackground(Color.WHITE);

reset=new JLabel(«RESET PASSWORD»);

reset.setForeground(Color.BLUE);

reset.setBounds(30, 30, 200, 30);

getContentPane().add(reset);

reset2=new JLabel(«Provide information below»);

reset2.setBounds(30, 70, 200, 30);

getContentPane().add(reset2);

JLabel firstlabel=new JLabel(«Enter firstname:»);

firstlabel.setBounds(30, 110, 150, 25);

getContentPane().add(firstlabel);

first_n=new JTextField();

first_n.setBounds(30, 130, 100, 25);

getContentPane().add(first_n);

JLabel secondlabel=new JLabel(«Enter secondname:»);

secondlabel.setBounds(30, 160, 150, 25);

getContentPane().add(secondlabel);

second_n=new JTextField();

second_n.setBounds(30, 180, 100, 25);

getContentPane().add(second_n);

JLabel userlabel=new JLabel(«Enter username:»);

userlabel.setBounds(30, 210, 150, 25);

getContentPane().add(userlabel);

user_n=new JTextField();

user_n.setBounds(30, 230, 100, 25);

getContentPane().add(user_n);

JLabel emaillabel=new JLabel(«Enter email:»);

emaillabel.setBounds(30, 260, 100, 25);

getContentPane().add(emaillabel);

e_mailaddress=new JTextField();

e_mailaddress.setBounds(30, 280, 100, 25);

getContentPane().add(e_mailaddress);

btnreset=new JButton(«RESET»);

btnreset.setBackground(Color.BLUE);

btnreset.setForeground(Color.WHITE);

btnreset.setBounds(50, 330, 80, 30);

getContentPane().add(btnreset);

btnreset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

checkData();

}

});

btnback=new JButton(«<-BACK»);

btnback.setBackground(Color.BLUE);

btnback.setForeground(Color.WHITE);

btnback.setBounds(400, 60, 100, 30);

btnback.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

MainFrame frame =new MainFrame();

frame.setVisible(true);

dispose();

}

});

getContentPane().add(btnback);

JButton btnlogin = new JButton(«LOGIN»);

btnlogin.setBackground(Color.BLUE);

btnlogin.setForeground(Color.WHITE);

btnlogin.setBounds(400, 100, 100, 30);

btnlogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

MainFrame frame =new MainFrame();

frame.setVisible(true);

dispose();

}

});

getContentPane().add(btnlogin);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(600,500);

setVisible(true);

}

public void checkData() {

String sql=«select * from Register where first_name=? and second_name=? and e_mail=? and user_name=?»;

try {

Connection myConn= DriverManager.getConnection(«jdbc:mysql://localhost:3306/JavaIutProject»,«root»,«»);

PreparedStatement ps=myConn.prepareStatement(sql);

String f=first_n.getText();

String s=second_n.getText();

String e=e_mailaddress.getText();

String u=user_n.getText();

ps.setString(1, f);

ps.setString(2, s);

ps.setString(3, e);

ps.setString(4, u);

ResultSet rs=ps.executeQuery();

if(rs.next()) {

String pass=Integer.toString(b);

String update=«UPDATE Register SET user_password='»+pass+«‘ WHERE first_name='»+f+«‘ and second_name='»+s+«‘ and e_mail='»+e+«‘ and user_name='»+u+«‘»;

PreparedStatement pstmt=myConn.prepareStatement(update);

pstmt.executeUpdate()

JOptionPane.showMessageDialog(null,«Your password: «+b,«Success»,JOptionPane.INFORMATION_MESSAGE);

}else {

JOptionPane.showMessageDialog(null,«The information is wrong!»,«», JOptionPane.ERROR_MESSAGE);

}

}catch(Exception exc) {

JOptionPane.showMessageDialog(null,exc);

exc.printStackTrace();

}

}

}

RegistrationUser – Ro’yxatdan o’tish va shaxsiy kabinet ochish.

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.sql.*;

import java.util.regex.Pattern;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JSeparator;

import javax.swing.JTextField;

public class RegistrationUser extends JFrame{

private JLabel registerpage,label1,label2,label3,label4,label5;

private JTextField first_name;

private JTextField second_name;

private JTextField user_name;

private JPasswordField user_password;

private JPasswordField confirm_password;

private JTextField e_mailfield;

private JButton signinbutton;

private JTextField phonenumber;

private JLabel valid, isvalid;

private int id;

String fname;

public RegistrationUser() {

super(«Registration»);

getContentPane().setLayout(null);

getContentPane().setBackground(Color.WHITE);

registerpage=new JLabel(«AFFILIATE REGISTRATION:»);

registerpage.setForeground(Color.RED);

registerpage.setBounds(20, 20, 200, 30);

getContentPane().add(registerpage);

JSeparator separator = new JSeparator();

separator.setBounds(20, 60, 305, 47);

separator.setBackground(Color.RED);

getContentPane().add(separator);

label1=new JLabel(«Enter firstname*»);

label1.setForeground(Color.RED);

label1.setBounds(20, 90, 150, 25);

getContentPane().add(label1);

first_name=new JTextField();

first_name.setBounds(20, 110, 150, 25);

getContentPane().add(first_name);

label2=new JLabel(«Enter second name*»);

label2.setForeground(Color.RED);

label2.setBounds(20, 140, 150, 25);

getContentPane().add(label2);

second_name=new JTextField();

second_name.setBounds(20, 160, 150, 25);

getContentPane().add(second_name);

label3=new JLabel(«Enter e-mail address*»);

label3.setForeground(Color.RED);

label3.setBounds(20, 190, 150, 25);

getContentPane().add(label3);

isvalid=new JLabel();

e_mailfield=new JTextField();

e_mailfield.setBounds(20, 210, 150, 25);

getContentPane().add(e_mailfield);

label4=new JLabel(«Enter username*»);

label4.setForeground(Color.RED);

label4.setBounds(20, 240, 150, 25);

getContentPane().add(label4);

user_name=new JTextField();

user_name.setBounds(20, 260, 150, 25);

getContentPane().add(user_name);

JLabel passwordlabel=new JLabel();

passwordlabel.setForeground(Color.RED);

passwordlabel.setBounds(20,290,150,25);

getContentPane().add(passwordlabel);

label5=new JLabel(«Choose password:(at least 8 character)»);

label5.setForeground(Color.RED);

label5.setBounds(20, 290, 300, 20);

getContentPane().add(label5);

user_password=new JPasswordField();

user_password.setBounds(20, 310, 150, 25);

getContentPane().add(user_password);

JLabel confirmlabel=new JLabel(«Confirm password*»);

confirmlabel.setForeground(Color.RED);

confirmlabel.setBounds(20,340,150,25);

getContentPane().add(confirmlabel);

confirm_password=new JPasswordField();

confirm_password.setBounds(20, 360, 150, 25);

getContentPane().add(confirm_password);

JButton btnback=new JButton(«<-BACK»);

btnback.setBounds(400, 60, 100, 30);

btnback.setBackground(Color.BLUE);

btnback.setForeground(Color.WHITE);

btnback.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

MainFrame m=new MainFrame();

m.setVisible(true);

dispose();

}

});

getContentPane().add(btnback);

signinbutton=new JButton(«Sign up»);

signinbutton.setBounds(20,410,100,30);

signinbutton.setBackground(Color.BLUE);

signinbutton.setForeground(Color.WHITE);

getContentPane().add(signinbutton);

signinbutton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

int fflag=0,sflag=0,eflag=0,uflag=0,pflag=0,cflag=0;

String f=first_name.getText();

String s=second_name.getText();

String e=e_mailfield.getText();

String u=user_name.getText();

String p=user_password.getText();

String c=confirm_password.getText();

if(isValid(e)) {

if(f.isEmpty()) {

fflag=1;

}

if(s.isEmpty()) {

sflag=1;

}

if(e.isEmpty()) {

eflag=1;

}

if(u.isEmpty()) {

uflag=1;

}

if(p.isEmpty()) {

pflag=1;

}

if(c.isEmpty()) {

cflag=1;

}

if(fflag==1||sflag==1||eflag==1||uflag==1||pflag==1||cflag==1) {

JOptionPane.showMessageDialog(null, «Every field is required to fill», «Note», JOptionPane.PLAIN_MESSAGE);

}

else {

if(p.length()<8) {

JOptionPane.showMessageDialog(null, «You need to enter at least 8 character for password», «Strong password», JOptionPane.PLAIN_MESSAGE);

}else {

if(p.equals(c)) {

Register(f,s,e,u,p);

}

else {

JOptionPane.showMessageDialog(null, «THE PASSWORD IS NOT THE SAME!», «Password confirmation», JOptionPane.PLAIN_MESSAGE);

}

}

}

}

else {

JOptionPane.showMessageDialog(null, «Invalid e-mail! See example: example@gmail.com», «E-mail», JOptionPane.ERROR_MESSAGE);

}

}

});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(600,500);

setVisible(true);

}

public void Register(String fname, String sname, String e_mail, String uname, String upassword) {

try {

Connection myConn= DriverManager.getConnectionjdbc:mysql://localhost:3306/JavaIutProject»,«root»,«»);

PreparedStatement ps=myConn.prepareStatement(«INSERT INTO Register « +«( first_name, second_name, e_mail, user_name, user_password )» +«VALUES ( ?, ?, ?, ?, ? )»);

ps.setString(1, fname);

ps.setString(2, sname);

ps.setString(3, e_mail);

ps.setString(4, uname);

ps.setString(5, upassword);

ps.executeUpdate();

JOptionPane.showMessageDialog(null, «You successfully created account», «Account», JOptionPane.PLAIN_MESSAGE);

JOptionPane.showMessageDialog(null, «REMEMBER your password and username», «Account», JOptionPane.PLAIN_MESSAGE);

String sql=«select * from Register where first_name='»+fname+«‘ and second_name='»+sname+«‘ and e_mail='»+e_mail+«‘ and user_name='»+uname+«‘ and user_password='»+upassword+«‘»;

PreparedStatement pstmt=myConn.prepareStatement(sql);

ResultSet rs=pstmt.executeQuery();

if (rs.next()) {

int id=rs.getInt(«ID»);

}

UserAccount user=new UserAccount(uname,id);

user.setVisible(true);

dispose();

}catch(Exception exc) {

JOptionPane.showMessageDialog(null, exc);

exc.printStackTrace();

}

}

public boolean isValid(String email) {

String emailRegex=«^[a-zA-Z0-9_+&*-]+(?:\\.»+ «[a-zA-Z0-9_+&*-]+)*@» + «(?:[a-zA-Z0-9-]+\\.)+[a-z» + «A-Z]{2,7}$»;

Pattern pat = Pattern.compile(emailRegex);

if (email == null)

return false;

return pat.matcher(email).matches();

}

}

UserAccount – Shaxsiy cabinet va undagi sozlanmalar sinfi.

import javax.swing.*;

import javax.swing.border.EmptyBorder;

import java.awt.*;

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.Statement;

public class UserAccount extends JFrame{

private JLabel welcome;

private JButton btnback, statistic;

private JInternalFrame internalFrame;

int flag=0;

JLabel balance;

public UserAccount(final String username,final int id) {

super(«Account»);

getContentPane().setLayout(null);

getContentPane().setBackground(Color.WHITE);

welcome=new JLabel(«WELCOME «+username+» !»);

welcome.setBounds(30, 30, 300, 30);

getContentPane().add(welcome);

btnback=new JButton(«SIGN OUT ->»);

btnback.setBackground(Color.BLUE);

btnback.setForeground(Color.WHITE);

btnback.setBounds(400, 60, 120, 30);

btnback.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

MainFrame o=new MainFrame();

o.setVisible(true);

dispose();

}

});

getContentPane().add(btnback);

JButton homepage=new JButton(«Home»);

homepage.setBackground(Color.BLUE);

homepage.setForeground(Color.WHITE);

homepage.setBounds(40,100, 100, 30);

getContentPane().add(homepage)

statistic=new JButton(«Statistics»);

statistic.setBackground(Color.BLUE);

statistic.setForeground(Color.WHITE);

statistic.setBounds(140,100, 100, 30);

getContentPane().add(statistic);

JButton order=new JButton(«Orders»);

order.setBackground(Color.BLUE);

order.setForeground(Color.WHITE);

order.setBounds(240,100, 100, 30);

getContentPane().add(order);

JButton reflinks=new JButton(«Ref-links»);

reflinks.setBackground(Color.BLUE);

reflinks.setForeground(Color.WHITE);

reflinks.setBounds(340,100, 100, 30);

getContentPane().add(reflinks);

JButton setting=new JButton(«Settings»);

setting.setBackground(Color.BLUE);

setting.setForeground(Color.WHITE);

setting.setBounds(440,100, 100, 30);

setting.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if(flag==0) {

internalFrame = new JInternalFrame(«Change password»);

internalFrame.setLayout(null);

internalFrame.setBackground(Color.WHITE);

internalFrame.setBounds(30, 200, 200, 200);

//internalFrame.setClosable(true);

JLabel curlab=new JLabel(«Current password:»);

curlab.setBackground(Color.BLUE);

curlab.setBounds(20, 20, 150, 25);

internalFrame.add(curlab);

final JTextField current=new JTextField();

current.setBounds(20, 40, 90, 25);

internalFrame.add(current);

JLabel newlab=new JLabel(«New password:»);

newlab.setBackground(Color.BLUE);

newlab.setBounds(20, 60, 90, 25);

internalFrame.add(newlab);

final JTextField newpass=new JTextField();

newpass.setBounds(20, 80, 90, 25);

internalFrame.add(newpass);

JButton ok=new JButton(«OK»);

ok.setBackground(Color.BLUE);

ok.setForeground(Color.WHITE);

ok.setBounds(50, 120, 60, 30);

ok.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String currentpassword=current.getText();

String newpassword=newpass.getText();

String sql=«select * from Register where ID=? and user_password=?»;

if(newpassword.length()>=8) {

try {

Connection myConn= DriverManager.getConnection(«jdbc:mysql://localhost:3306/JavaIutProject»,«root»,«»);

PreparedStatement ps=myConn.prepareStatement(sql);

ps.setInt(1, id);

ps.setString(2, currentpassword);

 

ResultSet rs=ps.executeQuery();

if(rs.next())

{

String update=«UPDATE Register SET user_password='»+newpassword+«‘ WHERE ID='»+id+«‘ «;

PreparedStatement pstmt=myConn.prepareStatement(update);

pstmt.executeUpdate();

UserAccount ua=new UserAccount(username,id);

ua.setVisible(true);

dispose();

}else {

JOptionPane.showMessageDialog(null,«The current password is wrong!»,«», JOptionPane.ERROR_MESSAGE);

}

}catch(Exception exc) {

JOptionPane.showMessageDialog(null,exc);

exc.printStackTrace();

}

}else {

JOptionPane.showMessageDialog(null, «Password must be at least 8 character «, «New password «, JOptionPane.ERROR_MESSAGE);

}

}

});

internalFrame.add(ok);

getContentPane().add(internalFrame);

internalFrame.setVisible(true);

flag=1;

}

}

});

getContentPane().add(setting);

JLabel link=new JLabel(«YOUR REF-LINK: «+«http://uzinfobiz.ru/ref=»+username);

link.setForeground(Color.BLUE);

link.setBounds(40, 300, 400, 30);

getContentPane().add(link);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(600,500);

setVisible(true);

}

}

Quyidagi videoni kuzating!

Quyida yuqoridagi videoning qismlarga bo’lingan shakli:

1 – qism.

2 – qism.

3-qism.

4-qism.

5-qism.

Shu bilan bugungi maqolaga yakun yasaymiz, savollaringiz bo’lsa kommentariyada qoldiring! Yangi maqolalar uchun saytimga OBUNA bo’ling!

Muallif: Oxunjon G’aybullayev

Рейтинг
( No ratings yet )
Oxunjon G'aybullayev/ author of the article
Понравился этот пост? Поделись с друзьями:
Fikr bildirish

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: