0

how can I create a bar chart by retrieving data from database using eclipse in java ? I have to retrieve data from (mySql) DB Browser and should connect to the graph . So here is the code below .Graph should shows lecture room and the Laboratory incrementation , Statistics is the package name and the class name is StatisticsGraph

package Locations;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import Connection.DBConnection;
import Lecturers_Management.AddingLecturers;

import java.awt.EventQueue;
public class AddLocations {

    private JFrame frame;

    PreparedStatement pstd=null;
    PreparedStatement pst = null;
    PreparedStatement ptsID=null;
    ResultSet rs = null;
    Connection conn;
    
    private JTextField BuildingName;
    private JTextField RoomName;
    private JTextField Capacity;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddLocations window = new AddLocations();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public AddLocations() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1254, 739);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JPanel panel_1 = new JPanel();
        panel_1.setLayout(null);
        panel_1.setBackground(Color.LIGHT_GRAY);
        panel_1.setBounds(10, 88, 233, 615);
        frame.getContentPane().add(panel_1);
        
        JButton btnNewButton = new JButton("Home");
        btnNewButton.setForeground(Color.BLACK);
        btnNewButton.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton.setBackground(Color.CYAN);
        btnNewButton.setBounds(10, 10, 213, 38);
        panel_1.add(btnNewButton);
        
        JButton btnNewButton_1= new JButton("Lecturers");
        btnNewButton_1.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_1.setBackground(Color.CYAN);
        btnNewButton_1.setBounds(10, 65, 213, 38);
        panel_1.add(btnNewButton_1); 
        
        JButton btnNewButton_2 = new JButton("Students");
        btnNewButton_2.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_2.setBackground(Color.CYAN);
        btnNewButton_2.setBounds(10, 124, 213, 38);
        panel_1.add(btnNewButton_2);
        
        JButton btnNewButton_3= new JButton("Subjects");
        btnNewButton_3.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_3.setBackground(Color.CYAN);
        btnNewButton_3.setBounds(10, 181, 213, 38);
        panel_1.add(btnNewButton_3);
        
        JButton btnNewButton_4 = new JButton("Working days/Hours");
        btnNewButton_4.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_4.setBackground(Color.CYAN);
        btnNewButton_4.setBounds(10, 235, 213, 38);
        panel_1.add(btnNewButton_4);
        
        JButton btnNewButton_5 = new JButton("Sessions");
        btnNewButton_5.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_5.setBackground(Color.CYAN);
        btnNewButton_5.setBounds(10, 289, 213, 38);
        panel_1.add(btnNewButton_5);
        
        JButton btnNewButton_6 = new JButton("Locations");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddLocations addlocations=new AddLocations();
                addlocations.main(null);
                frame.setVisible(false);
            }
        });
        btnNewButton_6.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_6.setBackground(Color.CYAN);
        btnNewButton_6.setBounds(10, 344, 213, 38);
        panel_1.add(btnNewButton_6);
        
        JButton btnNewButton_7 = new JButton("Tags");
        btnNewButton_7.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_7.setBackground(Color.CYAN);
        btnNewButton_7.setBounds(10, 400, 213, 38);
        panel_1.add(btnNewButton_7);
        
        JButton btnNewButton_8 = new JButton("Session Rooms");
        btnNewButton_8.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_8.setBackground(Color.CYAN);
        btnNewButton_8.setBounds(10, 456, 213, 38);
        panel_1.add(btnNewButton_8);
        
        JButton btnNewButton_9 = new JButton("Generate Timetables");
        btnNewButton_9.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_9.setBackground(Color.CYAN);
        btnNewButton_9.setBounds(10, 512, 213, 38);
        panel_1.add(btnNewButton_9);
    
        
        JButton btnNewButton_10 = new JButton("Statistics");
        btnNewButton_10.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_10.setBackground(Color.CYAN);
        btnNewButton_10.setBounds(10, 567, 213, 38);
        panel_1.add(btnNewButton_10);
        
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(Color.CYAN);
        panel.setBounds(10, 10, 1216, 68);
        frame.getContentPane().add(panel);
        
        JLabel lblNewLabel = new JLabel("TIMETABLE MANAGEMENT");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 22));
        lblNewLabel.setBackground(Color.BLUE);
        lblNewLabel.setBounds(430, 0, 329, 68);
        panel.add(lblNewLabel);
        

        JPanel panel_2 = new JPanel();
        panel_2.setLayout(null);
        panel_2.setBackground(Color.BLUE);
        panel_2.setBounds(253, 88, 973, 76);
        frame.getContentPane().add(panel_2);
        
        JButton btnNewButton_11 = new JButton("Add Location");
        btnNewButton_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddLocations addlocations=new AddLocations();
                addlocations.main(null);
                frame.setVisible(false);
            }
        });
        btnNewButton_11.setForeground(Color.BLACK);
        btnNewButton_11.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_11.setBackground(Color.LIGHT_GRAY);
        btnNewButton_11.setBounds(10, 10, 266, 56);
        panel_2.add(btnNewButton_11);
        
        JButton btnNewButton_12 = new JButton("Manage Location");
        btnNewButton_12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ManageLocations managelocations=new ManageLocations();
                managelocations.main(null);
                frame.setVisible(false);
            }
        });
        btnNewButton_12.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_12.setBackground(Color.LIGHT_GRAY);
        btnNewButton_12.setBounds(697, 9, 266, 58);
        panel_2.add(btnNewButton_12);
        
        

        JPanel panel_3 = new JPanel();
        panel_3.setBounds(253, 174, 973, 529);
        frame.getContentPane().add(panel_3);
        panel_3.setLayout(null);
        
        JLabel lblNewLabel_1 = new JLabel("Building Name");
        lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 19));
        lblNewLabel_1.setBackground(new Color(148, 0, 211));
        lblNewLabel_1.setBounds(10, 10, 148, 41);
        panel_3.add(lblNewLabel_1);
        
        JLabel lblNewLabel_2 = new JLabel("Room Name");
        lblNewLabel_2.setFont(new Font("Times New Roman", Font.BOLD, 19));
        lblNewLabel_2.setBackground(new Color(148, 0, 211));
        lblNewLabel_2.setBounds(10, 90, 148, 41);
        panel_3.add(lblNewLabel_2);
    
        JLabel lblNewLabel_3 = new JLabel("Room Type[![enter image description here][1]][1]");
        lblNewLabel_3.setFont(new Font("Times New Roman", Font.BOLD, 19));
        lblNewLabel_3.setBounds(10, 168, 148, 41);
        panel_3.add(lblNewLabel_3);
        
        JLabel lblNewLabel_4 = new JLabel("Capacity");
        lblNewLabel_4.setFont(new Font("Times New Roman", Font.BOLD, 19));
        lblNewLabel_4.setBounds(10, 248, 148, 41);
        panel_3.add(lblNewLabel_4);
        
        
        JTextField BuildingName = new JTextField();
        BuildingName.setBackground(Color.LIGHT_GRAY);
        BuildingName.setFont(new Font("Times New Roman", Font.BOLD, 20));
        BuildingName.setBounds(145, 12, 730, 41);
        panel_3.add(BuildingName);
        BuildingName.setColumns(10);
        
        JTextField RoomName = new JTextField();
        RoomName.setBackground(Color.LIGHT_GRAY);
        RoomName.setFont(new Font("Times New Roman", Font.BOLD, 20));
        RoomName.setBounds(145, 90, 730, 41);
        panel_3.add(RoomName);
        RoomName.setColumns(10);
    
        JComboBox RoomType = new JComboBox();
        RoomType.setModel(new DefaultComboBoxModel(new String[] {"Lecture Hall", "Laboratory"}));
        RoomType.setFont(new Font("Times New Roman", Font.BOLD, 20));
        RoomType.setBackground(Color.LIGHT_GRAY);
        RoomType.setBounds(145, 168, 730, 41);
        panel_3.add(RoomType);
        
        JTextField Capacity = new JTextField();
        Capacity.setBackground(Color.LIGHT_GRAY);
        Capacity.setFont(new Font("Times New Roman", Font.BOLD, 20));
        Capacity.setBounds(145, 248, 730, 41);
        panel_3.add(Capacity);
        Capacity.setColumns(10);
        
        

        JButton btnNewButton_14 = new JButton("Clear");
        btnNewButton_14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                BuildingName.setText(null);
                RoomName.setText(null);
                RoomType.setSelectedItem(null);
                Capacity.setText(null);
                
                
            }
        });
        btnNewButton_14.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_14.setBackground(Color.CYAN);
        btnNewButton_14.setBounds(435, 452, 157, 41);
        panel_3.add(btnNewButton_14);
        
        
        
        JButton btnNewButton_15 = new JButton("Save");
        btnNewButton_15.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
    
                    
                    
                    
                    
                
                try {
                    
                    
                    if(BuildingName.getText().equals("") ||RoomName.getText().equals("")||RoomType.getSelectedItem().equals("")||Capacity.getText().equals("")) {
                        JOptionPane.showMessageDialog(null, "Please fill the form and then save it");
                        
                         
                            
                        
                    }else if(!(Capacity.getText().trim().matches("^[0-9]{3}"))){
                        JOptionPane.showMessageDialog(null, "Invalid capacity amount");
                    
                    }else{
                        
                        Connection connec = DBConnection.connect();
                        
            
                            
                            
                            String BName=BuildingName.getText();
                            String RName=RoomName.getText();
                            String RType=RoomType.getSelectedItem().toString();
                            String capacity=Capacity.getText();
                            
                               

                            String msg = "" + BName;
                            msg += " \n";
                            
                         
                            
                            

                            Connection con = DBConnection.connect();

                            String query = "INSERT INTO Locations values(null,'" + BName + "','" + RName + "','" + RType + "','" +
                                    capacity + "')";

                            java.sql.Statement sta = con.createStatement();
                            int xx = sta.executeUpdate(query);
                          
                            
                            
                            
                            JOptionPane.showMessageDialog(null,
                                    "Your , " + msg + "has succesfully inserted");
                            
                            con.close();
                            BuildingName.setText(null);
                              RoomName.setText(null);
                              RoomType.setSelectedItem(null);
                              Capacity.setText(null);
                            
                           
                        
                            
                            
                            
                            
                            
                            
                        
                    }
                    }
                    
                    
                catch(Exception w){
                    JOptionPane.showMessageDialog(null, w );
                    
                }
                
                finally{
                    try{
                         Connection pst = null;
                        pst.close();
                                Connection rs = null;
                                rs.close();
                    }catch(Exception r){
                        
                    }
                }
            
                    }
                  
                
                
                    
            
        });
        btnNewButton_15.setFont(new Font("Times New Roman", Font.BOLD, 20));
        btnNewButton_15.setBackground(Color.CYAN);
        btnNewButton_15.setBounds(719, 452, 157, 41);
        panel_3.add(btnNewButton_15);
    }
}

enter image description here enter image description here

greg-449
  • 109,219
  • 232
  • 102
  • 145
uzman
  • 21
  • 2
  • 3
    I see that you're using `setBounds` and `null-layout` which is a terrible approach at making UIs, see [an example](https://stackoverflow.com/a/42521097/2180785) of why. – Frakcool Apr 07 '21 at 18:20
  • 4
    And I have a question for you: Is your problem related to you not being able to connect to the DB? If so, all Swing code is unrelated to the question, otherwise drop off the code to the DB and post a proper [mre] with hardcoded data, where all you do is try to make the charts. Another option is to use [JFreeChart](https://www.jfree.org/jfreechart/) inside your project and let it do everything with the data you pass to it – Frakcool Apr 07 '21 at 18:20
  • 2
    If you're going to create your own bar chart, you'll need to draw (paint) the chart on a drawing JPanel. The Oracle tutorial, [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html), will take you through the basic steps of drawing on a JPanel. Be prepared to spend a lot of development time creating your own chart, which is why JFreeChart is a popular option. – Gilbert Le Blanc Apr 08 '21 at 06:42

0 Answers0