Water Supply Management System In Java

by | Jan 21, 2023 | Coding, Java

Home » Coding » Water Supply Management System In Java

Introduction of the Project

In this java project tutorial, we will create a water supply management system in Java. We will use Java programming language to create software that will manage various aspects of water distribution. We have used an SQL database to store information regarding the name of the person placing the order, location, quantity & number of liters of water. To make the project more interesting, we will be implementing some GUI using the Swing module.

 

Objectives

The objectives for creating a water supply management system in Java are as follows:

  • Automating water distribution: The system will automate the distribution of water to different areas to ensure that everyone has access to a reliable water supply.
  • Managing water resources: The system should be able to manage water resources efficiently by monitoring water levels and predicting future water needs.
  • Reducing water loss: The system should be able to detect and fix leaks in the water distribution system to reduce water loss and improve efficiency.
  • Enhancing customer service: The system should be able to provide customers with accurate bills and efficient maintenance services to improve customer satisfaction.
  • Providing real-time data: The system should provide real-time data to help the operators and decision-makers to take quick actions.

Requirements

The requirements for creating a water supply management system in Java will include the following:

  • Knowledge of Concepts of Java Programming Language.
  • SQL Database: To implement the tables containing the water supply information and attach the functionality of taking orders & maintaining prices in the database.
  • Swing Module: To implement the program’s Graphical User Interface (GUI). The system would need a user interface that allows operators to accept the order and know the quantity of water to be supplied and the pricing related to these services.

Source Code

package com.company;
public class Main {
public static void main(String[] args) {
new Water();
}
}
package com.company;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.Vector;
public class Water {
private JTextField nameData;
private JTextField locationData;
private JTable table1;
private JButton ADDRECORDButton;
private JButton UPDATERECORDButton;
private JPanel waterPanel;
private JComboBox quantity;
private JComboBox litres;
JFrame waterF = new JFrame();
public Water(){
waterF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
waterF.setContentPane(waterPanel);
waterF.pack();
waterF.setLocationRelativeTo(null);
waterF.setVisible(true);
tableData();
ADDRECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(nameData.getText().equals("")|| locationData.getText().equals("")|| litres.getSelectedItem()==null|| quantity.getSelectedItem()==null){
JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record.");
}else{
try {
String sql = "insert into water"+"(NAME,LOCATION,LITRES,QUANTITY,PRICE)"+"values (?,?,?,?,?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
int price = Integer.parseInt(""+litres.getSelectedItem())*Integer.parseInt(""+quantity.getSelectedItem())*10;
statement.setString(1,nameData.getText());
statement.setString(2, locationData.getText());
statement.setString(3, ""+litres.getSelectedItem());
statement.setString(4,""+quantity.getSelectedItem());
statement.setInt(5,price);
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"ITEM ADDED SUCCESSFULLY");
nameData.setText("");
locationData.setText("");
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
tableData();
}
}
});
UPDATERECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
int price = Integer.parseInt(""+litres.getSelectedItem())*Integer.parseInt(""+quantity.getSelectedItem())*10;
String sql = "UPDATE water " +
"SET LOCATION = '"+ locationData.getText()+"',LITRES='"+ litres.getSelectedItem()+
"',QUANTITY='"+quantity.getSelectedItem()+"'" +",PRICE= "+price+
" WHERE NAME= '"+nameData.getText()+"'";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"Updated successfully");
}catch (Exception e2){
System.out.println(e2);
}
tableData();
}
});
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
DefaultTableModel dm = (DefaultTableModel)table1.getModel();
int selectedRow = table1.getSelectedRow();
nameData.setText(dm.getValueAt(selectedRow,0).toString());
locationData.setText(dm.getValueAt(selectedRow,1).toString());
}
});
}
public void tableData() {
try{
String a= "Select* from water";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
table1.setModel(buildTableModel(rs));
}catch (Exception ex1){
JOptionPane.showMessageDialog(null,ex1.getMessage());
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}

Explanation of the Code

The code can be broken down into two sections. One involves creating the GUI, and the other is the retrieval of information from the SQL database & updating it according to the user’s input.

Let us look at the GUI first:

1. The main screen consists of 2 buttons adding & updating in database.

2. It has 2 text fields & 2 combo Box that requires information on customer requiring water supply.

3. It consists of a Jtable that displays the stored information in the database.

Moving to the retrieval, we will apply the following:

1. Build a connection first with the database using the Connection object.

2. Inject the query that stores table data in ResultSet.

3. Finally, send data to Jtable.

4. Adding & Updating functionalities are added so that it gets updated in the database as well as in the interface.

Output

The Main Interface of our water supply management system will look like this.

Water Supply Management System In Java

Conclusion

This tutorial was a Java program that helps to manage customer information & help to place a water supply order with Swing used for GUI. This java program is a very efficient & easy way to maintain the data regarding the water supply. Additionally, we can also include smart sensors and IoT technologies to monitor and control water usage and machine learning algorithms to predict and optimize water distribution.

 

More Java-Related Projects>>>

You May Also Like To Create…