Popular Posts

Java Create Menu Using MySQL

Share it:

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




Share it:

Create JMenu Using MySQL In Java

Create JMenuItem Using MySQL In Java

create menu from mysql database in java

database

java

jMenu

jMenuBar

jMenuItem

Menu

mysql

mysql database

Post A Comment:

0 comments: