Affichage des articles dont le libellé est Create JMenu Using MySQL In Java. Afficher tous les articles
Affichage des articles dont le libellé est Create JMenu Using MySQL In Java. Afficher tous les articles

Java Create Menu Using MySQL

How To Create A Menu With MySQL DataBase Using Java NetBeans

create menu from database using java



In this Java Tutorial we will see How To Create A Menu Using MySQL DataBase By Creating JMenu And JmenItems Dynamically From Database With Images And Text In Java NetBeans .




Project Source Code:


// function to get the connection
    public Connection getConnection()
    {
         try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
             System.out.println(ex.getMessage());
        }
        
        Connection con = null;
        
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/s_t_d", "root", "");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return con;
    }
    
// Create And Add JMenu To JMenuBar
    public void _addMenu(){
      
        Connection con = getConnection();
        Statement st;
        ResultSet rs;
        
        try {
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM `mypics`");
            
            while(rs.next()){
                ImageIcon img1 = new ImageIcon(rs.getBytes("pic"));
                Image img2 = img1.getImage().getScaledInstance(70, 70, Image.SCALE_SMOOTH);
                ImageIcon img3 = new ImageIcon(img2);
                
                // create new jmenu
                JMenu jm = new JMenu(rs.getString("name"));
                jm.setIcon(img3);
               // add jmenuitems to the jmenu
                _addMenuItem(jm, rs.getInt("id"));
                jMenuBar1.add(jm);
            }
        } catch (SQLException ex) {
            Logger.getLogger(menu_from_mysql.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    
// function to add JmenuITems to Jmenu
    public void _addMenuItem(JMenu jm, int id){
     
        Connection con = getConnection();
        Statement st;
        ResultSet rs;
        
        try {
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM `pics2` WHERE idMyPics = "+id);
            
            while(rs.next()){
                ImageIcon img1 = new ImageIcon(rs.getBytes("pic"));
                Image img2 = img1.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
                ImageIcon img3 = new ImageIcon(img2);
                
                JMenuItem jmi = new JMenuItem(rs.getString("name"),img3);
                jm.add(jmi);
            }
        } catch (SQLException ex) {
            Logger.getLogger(menu_from_mysql.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    } 

OutPut:

Java Menu Using DataBase




Java Add And Create JMenu Using MySQL

How To Create And Add JMenu To JMenuBar Using MySQL And Java NetBeans

create jmenu from mysql in java



In this Java Tutorial we will see How To Create A JMenu Dynamically From MySQL Database By Geting Image And Text From Database And Set Them Into JMenu In Java NetBeans .




Project Source Code:

// get connection to the database
    public Connection getConnection()
    {
         try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
             System.out.println(ex.getMessage());
        }
        
        Connection con = null;
        
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/s_t_d", "root", "");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return con;
    }

    
    public void _addMenu(){
        
        Connection con = getConnection();
            Statement st;
            ResultSet rs;
            
        try {
            
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM mypics");
            
            while(rs.next()){
                
                // get the image from database
                // resize the image
                // create the jmenu and add the image to it
                
                ImageIcon img1 = new ImageIcon(rs.getBytes("pic"));
                Image img2 = img1.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
                ImageIcon img3 = new ImageIcon(img2);
                
                JMenu jm = new JMenu(rs.getString("name"));
                jm.setIcon(img3);
                
                // add the jmenu to the jmenubar
                jMenuBar1.add(jm);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(JMenu_From_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    } 

OutPut:

Make JMenu Using Java And MySQL