как я могу создать гистограмму, извлекая данные из базы данных с помощью eclipse в java? Мне нужно получить данные из (mySql) браузера БД и подключиться к графику. Итак, вот код ниже. График должен отображать аудиторию и приращение лаборатории, Статистика - это имя пакета, а имя класса - 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[![][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);
}
}
И у меня к вам вопрос: ваша проблема связана с тем, что вы не можете подключиться к БД? Если это так, весь код Swing не имеет отношения к вопросу, в противном случае оставьте код в БД и отправьте правильный минимальный воспроизводимый пример с жестко закодированными данными, где все, что вы делаете, это пытаетесь создать диаграммы. Другой вариант - использовать JFreeChart внутри вашего проекта и позволить ему делать все с данными, которые вы ему передаете.
Если вы собираетесь создать свою собственную гистограмму, вам нужно будет нарисовать (раскрасить) диаграмму на JPanel для рисования. Учебник Oracle Выполнение индивидуальной покраски проведет вас через основные шаги рисования на JPanel. Будьте готовы потратить много времени на разработку, создавая собственную диаграмму, поэтому JFreeChart - популярный вариант.
Я вижу, что вы используете
setBounds
иnull-layout
, что является ужасным подходом к созданию пользовательского интерфейса, почему см. пример.