JAVA - Calculate Percentage In MySQL Using Java

How To Calculate Percentage ( % ) From MySQL Database Using Java NetBeans

Calculate Percentage In MySQL Using Java



In this Java Tutorial we will see How To Calculate Percentage In a MySQL Database Table Using Java NetBeans and  .
and to do that we need two values:
1 - the total value.
2 - the value of the specific element we want the percentage. 



WATCH THIS JAVA TUTORIAL


Project Source Code:


-> First We Have To Create a DB Class To Create a Connection With MySQL  Database

/*********** Start DB Class *************/

package javatutorials;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;

public class DB {
    
    // create a connection with mysql database using datasource
    public static Connection createConnection(String serverName, Integer portNumber, String userName, String password, String dbName){
        
        Connection mycon = null;
        
        try{
            
            MysqlDataSource dataSource = new MysqlDataSource();
            
            dataSource.setServerName(serverName);
            dataSource.setPortNumber(portNumber);
            dataSource.setUser(userName);
            dataSource.setPassword(password);
            dataSource.setDatabaseName(dbName);
            
            mycon = dataSource.getConnection();
            
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        
        return mycon;
        
    }
    
}

/*********** End DB Class *************/
                                                
Now We Will Create a JFrame and Name It "Calc_Percentage"


public Calc_Percentage() {
        initComponents();
        
       // call our function here to display the percentage values
        showPurcentage();
    }


// get the connection from our db class
    public Connection getConnection(){
        return DB.createConnection("localhost", 3306, "root", "", "student_db");
    }
    
    // create a function to execute count queries
    public int execCount(String query){
        
        int total = 0;
        Connection con = getConnection();
        
        try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            
            if(rs.next()){
                total = rs.getInt(1);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(Calc_Percentage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return total;
    }
    
    
    // calculate total students
    public int totalStudents(){
        String query = "SELECT COUNT(*) FROM `student`";
        return execCount(query);
    }
    
    // calculate total students
    public int totalMaleStudents(){
        String query = "SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'";
        return execCount(query);
    }
    
    // calculate total students
    public int totalFemaleStudents(){
        String query = "SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'";
        return execCount(query);
    }
    
    // display data
    public void showPurcentage()
    {
        jLabel_total.setText(Integer.toString(totalStudents()));
        
        double maleStd = totalMaleStudents();
        double femaleStd = totalFemaleStudents();
        
        double male_p = (maleStd * 100) / totalStudents();
        double female_p = (femaleStd * 100) / totalStudents();
        
        jLabel_Male.setText(String.format("%.2f", male_p) + "%");
        jLabel_female.setText(String.format("%.2f", female_p) + "%");
    }
    

OutPut:

calculating percentage in java and mysql





Share this

Related Posts

Previous
Next Post »