-
Notifications
You must be signed in to change notification settings - Fork 1
/
BioPelletPlantLocator.java
199 lines (169 loc) · 8.64 KB
/
BioPelletPlantLocator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package Backend;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class BioPelletPlantLocator extends JFrame {
// Connection details
private static final String DB_URL = "jdbc:mysql://localhost:3306/BioPelletLocatorDB";
private static final String DB_USER = "root"; // change if you use a different username
private static final String DB_PASS = "HTITN213x01!"; // replace with your MySQL password
// Method to connect to the database
private Connection connectToDatabase() {
Connection connection = null;
try {
// Establish the connection
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
System.out.println("Connection to database established successfully!");
} catch (SQLException e) {
e.printStackTrace(); // Print any SQL exceptions
}
return connection; // Return the connection object
}
private static boolean checkDatabaseConnection() {
try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
if (connection != null) {
System.out.println("Connected to the database!");
return true;
}
} catch (SQLException e) {
System.out.println("Failed to connect to the database.");
e.printStackTrace();
}
return false;}
private ArrayList<BioPelletPlant> plants;
private JTable table;
private JTextField searchField;
private DefaultTableModel tableModel;
public BioPelletPlantLocator() {
plants = new ArrayList<>();
initializePlants(); // Initialize with pre-added plants
setTitle("Bio Pellet Plant Locator");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Menu bar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
// Tool bar
JToolBar toolBar = new JToolBar();
JButton searchButton = new JButton("Search by Pincode");
toolBar.add(searchButton);
add(toolBar, BorderLayout.NORTH);
// Plant List Panel
JPanel plantListPanel = new JPanel(new BorderLayout());
String[] columns = {"Plant Name", "Location", "Capacity", "Contact", "Pincode"};
tableModel = new DefaultTableModel(columns, 0);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
plantListPanel.add(scrollPane, BorderLayout.CENTER);
JPanel searchPanel = new JPanel();
searchField = new JTextField(20);
searchPanel.add(new JLabel("Enter Pincode:"));
searchPanel.add(searchField);
searchPanel.add(searchButton);
plantListPanel.add(searchPanel, BorderLayout.NORTH);
add(plantListPanel, BorderLayout.CENTER);
// Footer label
JLabel footerLabel = new JLabel("- Bio Pellet Plant Locator");
footerLabel.setFont(new Font("Arial", Font.ITALIC, 12));
footerLabel.setHorizontalAlignment(SwingConstants.RIGHT);
add(footerLabel, BorderLayout.SOUTH);
// Event handling
searchButton.addActionListener(e -> searchPlantByPincode());
setVisible(true);
}
private void initializePlants() {
// Pre-added plants (example data)
plants.add(new BioPelletPlant("Chennai Biofuel Plant", "Chennai, Tamil Nadu", "120", "044-1234-5678", "600001"));
plants.add(new BioPelletPlant("Eco Friendly Biomass Plant", "Chennai, Tamil Nadu", "110", "044-2345-6789", "600001"));
plants.add(new BioPelletPlant("Eco Green Energy", "Chennai, Tamil Nadu", "150", "044-3456-7890", "600018"));
plants.add(new BioPelletPlant("Renewable Bioenergy Corp", "Chennai, Tamil Nadu", "220", "044-4567-8901", "600018"));
plants.add(new BioPelletPlant("Bioenergy Solutions", "Chennai, Tamil Nadu", "190", "044-5678-9012", "600018"));
plants.add(new BioPelletPlant("Sustainable Energy Works", "Chennai, Tamil Nadu", "130", "044-6789-0123", "600028"));
plants.add(new BioPelletPlant("Chennai Biomass Power", "Chennai, Tamil Nadu", "140", "044-8901-2345", "600028"));
plants.add(new BioPelletPlant("Renewable Energy Solutions", "Chennai, Tamil Nadu", "100", "044-9012-3456", "600042"));
plants.add(new BioPelletPlant("Chennai EcoPellet Plant", "Chennai, Tamil Nadu", "125", "044-0123-4567", "600042"));
plants.add(new BioPelletPlant("Green Fuel Industries", "Chennai, Tamil Nadu", "250", "044-1234-5678", "600060"));
plants.add(new BioPelletPlant("Biofuels India", "Chennai, Tamil Nadu", "160", "044-2345-6789", "600060"));
plants.add(new BioPelletPlant("Green Biomass Technologies", "Chennai, Tamil Nadu", "170", "044-3456-7890", "600060"));
plants.add(new BioPelletPlant("Tamil Nadu Bio Pellets", "Chennai, Tamil Nadu", "200", "044-4567-8901", "600100"));
plants.add(new BioPelletPlant("Chennai Green Energy Solutions", "Chennai, Tamil Nadu", "180", "044-5678-9012", "600100"));
plants.add(new BioPelletPlant("Renewable Resources Chennai", "Chennai, Tamil Nadu", "155", "044-6789-0123", "600100"));
plants.add(new BioPelletPlant("Renewable Energy Systems", "Chennai, Tamil Nadu", "135", "044-7890-1234", "600036"));
plants.add(new BioPelletPlant("Chennai Sustainable Fuels", "Chennai, Tamil Nadu", "145", "044-8901-2345", "600036"));
plants.add(new BioPelletPlant("Green Energy Solutions", "Chennai, Tamil Nadu", "175", "044-9012-3456", "600049"));
plants.add(new BioPelletPlant("Chennai Bioenergy Plant", "Chennai, Tamil Nadu", "165", "044-0123-4567", "600049"));
plants.add(new BioPelletPlant("Renewable Resources Chennai", "Chennai, Tamil Nadu", "155", "044-5678-9012", "600055"));
plants.add(new BioPelletPlant("Sustainable Biomass Energy", "Chennai, Tamil Nadu", "140", "044-9876-5432", "603203"));
}
private void searchPlantByPincode() {
String searchPincode = searchField.getText().trim();
tableModel.setRowCount(0); // Clear the table
boolean found = false; // Track if any plants are found
for (BioPelletPlant plant : plants) {
if (plant.getPincode().equals(searchPincode)) {
tableModel.addRow(new Object[]{
plant.getName(),
plant.getLocation(),
plant.getCapacity(),
plant.getContact(),
plant.getPincode()
});
found = true; // A match was found
}
}
if (!found) {
showErrorDialog("No plants found for the given pincode.");
}
}
private void showErrorDialog(String message) {
JDialog dialog = new JDialog(this, "Error", true);
dialog.setLayout(new FlowLayout());
dialog.add(new JLabel(message));
JButton okButton = new JButton("OK");
okButton.addActionListener(e -> dialog.dispose());
dialog.add(okButton);
dialog.setSize(300, 150);
dialog.setLocationRelativeTo(this); // Center the dialog
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(BioPelletPlantLocator::new);
BioPelletPlantLocator locator = new BioPelletPlantLocator();
Connection connection = locator.connectToDatabase(); // Connect to the database
// Use the connection for database operations...
}
class BioPelletPlant {
private String name, location, capacity, contact, pincode;
public BioPelletPlant(String name, String location, String capacity, String contact, String pincode) {
this.name = name;
this.location = location;
this.capacity = capacity;
this.contact = contact;
this.pincode = pincode;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public String getCapacity() {
return capacity;
}
public String getContact() {
return contact;
}
public String getPincode() {
return pincode;
}
}
}