By GokiSoft.com| 19:48 15/06/2020|
Java Advanced

[Share Code] Hướng dẫn tìm hiểu MySQL trong Java - Quản lý user trong Java + MySQL BT1661

#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession11;

import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Test => Lay danh sach user trong CSDL
        List<User> list = UserModify.findAll();
        
        for (User user : list) {
            user.display();
        }
        
        //Test => Them sinh vien vao trong CSDL
        
    }
}


#User.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession11;

/**
 *
 * @author Diep.Tran
 */
public class User {
    int id;
    String fullname, email, birthday, password, address;

    public User() {
    }

    public User(int id, String fullname, String email, String birthday, String password, String address) {
        this.id = id;
        this.fullname = fullname;
        this.email = email;
        this.birthday = birthday;
        this.password = password;
        this.address = address;
    }

    public int getId() {
        return id;
    }

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

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getPassword() {
        return password;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" + "id=" + id + ", fullname=" + fullname + ", email=" + email + ", birthday=" + birthday + ", password=" + password + ", address=" + address + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}


#UserModify.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession11;

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.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class UserModify {
    public static List<User> findAll() {
//        start
        List<User> list = new ArrayList<>();
        
        //lay du lieu tu csdl
        Connection con = null;
        Statement statement = null;
        
        try {
            //Mo ket noi toi CSDL
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quanlysinhvien", "root", "");
            
            //tao truy van
            String sql = "select * from user";
            statement = con.createStatement();
            
            ResultSet resultSet = statement.executeQuery(sql);
            
            while (resultSet.next()) {                
                User user = new User(resultSet.getInt("id"), 
                        resultSet.getString("fullname"), 
                        resultSet.getString("email"), 
                        resultSet.getString("birthday"), 
                        resultSet.getString("password"), 
                        resultSet.getString("address"));
                list.add(user);
            }
        } catch(Exception e) {
            
        } finally {
            //Dong ket noi
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return list;
//        stop
    }
    
    public static void insert(User user) {
//        start
        //lay du lieu tu csdl
        Connection con = null;
        PreparedStatement statement = null;
        
        try {
            //Mo ket noi toi CSDL
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quanlysinhvien", "root", "");
            
            //tao truy van
            String sql = "insert into user(fullname, email, birthday, password, address) values (?, ?, ?, ?, ?)";
            statement = con.prepareStatement(sql);
            
            statement.setString(1, user.getFullname());
            statement.setString(2, user.getEmail());
            statement.setString(3, user.getBirthday());
            statement.setString(4, user.getPassword());
            statement.setString(5, user.getAddress());
            
            statement.execute();
        } catch(Exception e) {
            
        } finally {
            //Dong ket noi
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
//        stop
    }
    
    public static void update(User user) {
//        start
        //lay du lieu tu csdl
        Connection con = null;
        PreparedStatement statement = null;
        
        try {
            //Mo ket noi toi CSDL
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quanlysinhvien", "root", "");
            
            //tao truy van
            String sql = "update user set fullname = ?, email = ?, birthday = ?, password = ?, address = ? where id = ?";
            statement = con.prepareStatement(sql);
            
            statement.setString(1, user.getFullname());
            statement.setString(2, user.getEmail());
            statement.setString(3, user.getBirthday());
            statement.setString(4, user.getPassword());
            statement.setString(5, user.getAddress());
            statement.setInt(6, user.getId());
            
            statement.execute();
        } catch(Exception e) {
            
        } finally {
            //Dong ket noi
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
//        stop
    }
    
    public static void delete(int id) {
//        start
        //lay du lieu tu csdl
        Connection con = null;
        PreparedStatement statement = null;
        
        try {
            //Mo ket noi toi CSDL
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quanlysinhvien", "root", "");
            
            //tao truy van
            String sql = "delete from user where id = ?";
            statement = con.prepareStatement(sql);
            
            statement.setInt(1, id);
            
            statement.execute();
        } catch(Exception e) {
            
        } finally {
            //Dong ket noi
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
//        stop
    }
    
    public static User findById(int id) {
//        start
        //lay du lieu tu csdl
        Connection con = null;
        PreparedStatement statement = null;
        
        try {
            //Mo ket noi toi CSDL
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quanlysinhvien", "root", "");
            
            //tao truy van
            String sql = "select * from user where id = ?";
            statement = con.prepareCall(sql);
            statement.setInt(1, id);
            
            ResultSet resultSet = statement.executeQuery(sql);
            
            while (resultSet.next()) {                
                User user = new User(resultSet.getInt("id"), 
                        resultSet.getString("fullname"), 
                        resultSet.getString("email"), 
                        resultSet.getString("birthday"), 
                        resultSet.getString("password"), 
                        resultSet.getString("address"));
                return user;
            }
        } catch(Exception e) {
            
        } finally {
            //Dong ket noi
            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return null;
//        stop
    }
}


Liên kết rút gọn:

https://gokisoft.com/1661

Bình luận