Affichage des articles dont le libellé est Calculate Percentage. Afficher tous les articles
Affichage des articles dont le libellé est Calculate Percentage. Afficher tous les articles

VB.NET - Calculate Percentage With MySQL

How To Calculate Percentage From MySQL Database In Visual Basic.Net 

Calculate Percentage With MySQL Using Vbnet

In This VB.Net and MySQL Tutorial We Will See How To Calculate and Display the Percentage Values From a MySQL Database Table Using Visual Basic.Net Programming Language And Visual Studio Editor.

- To Learn More Watch The Video Tutorial Below. 


Project Source Code:

Imports MySql.Data.MySqlClient

Public Class calc_percent_form

    ' create the connection with mysql database
    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=vbnet_student_db")

    Private Sub calc_percent_form_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim totalStd As Integer = Convert.ToInt32(totalStudents())
        Dim totalMaleStd As Integer = Convert.ToInt32(totalMaleStudents())
        Dim totalFemaleStd As Integer = Convert.ToInt32(totalFemaleStudents())

        ' the % formula
        ' (male students * 100) / totale students
        ' (female students * 100) / totale students

        Dim maleStd_p As Double = (totalMaleStd * 100) / totalStd
        Dim femaleStd_p As Double = (totalFemaleStd * 100) / totalStd

        lbl_total_students.Text = totalStd.ToString()
        lbl_total_Male_Students.Text = maleStd_p.ToString("0.00") & "%"
        lbl_total_Female_Students.Text = femaleStd_p.ToString("0.00") & "%"


    End Sub


    'create a function to execute count queries
    Function execQuery(ByVal query As String) As String

        Dim command As New MySqlCommand(query, connection)

        ' open connection
        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If

        Return command.ExecuteScalar.ToString()

        ' close connection
        If connection.State = ConnectionState.Open Then
            connection.Close()
        End If


    End Function


    ' create a function to return total students
    Function totalStudents() As String

        Return execQuery("SELECT COUNT(*) FROM `student`")

    End Function

    ' create a function to return total MALE students
    Function totalMaleStudents() As String

        Return execQuery("SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'")

    End Function

    ' create a function to return total FEMALE students
    Function totalFemaleStudents() As String

        Return execQuery("SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'")

    End Function


End Class  
   


///////////////OUTPUT:



How To Calculate Percentage From MySQL Using Vbnet



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





C# - Calculate Percentage With MySQL

Calculating the Percentage of Male and Female Students from a MySQL Database Using C#

Calculate Percentage In C#


In this C# tutorial, we will learn how to calculate and display the percentage values from a MySQL database table in C# using Visual Studio editor.

We will create two classes:
1- "MY_DB": This class will handle the connection with the MySQL database.
2- "MY_CALC": This class will be responsible for counting the values we need.

Additionally, we will have a form named "calculate_percentage" to display the percentage values.


WATCH THIS C# TUTORIAL


Project Source Code:


//************** Start MY_DB Class **************//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;

namespace csharp__calculate_percentage
{
    class My_DB
    {

        // the connection
        MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=student_db");

        // get the connection
        public MySqlConnection getConnection
        {
            get
            {
                return con;
            }
        }


        // open the connection
        public void openConnection()
        {
            if ((con.State == ConnectionState.Closed))
            {
                con.Open();
            }

        }


        // close the connection
        public void closeConnection()
        {
            if ((con.State == ConnectionState.Open))
            {
                con.Close();
            }

        }
    }
}


//************** End MY_DB Class **************// 


//************** Start MY_CALC Class **************// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace csharp__calculate_percentage
{
    class My_CALC
    {
        My_DB db = new My_DB();

        // function to execute count query
        string execCount(string query)
        {
            MySqlCommand command = new MySqlCommand(query, db.getConnection);
            db.openConnection();

            string count = command.ExecuteScalar().ToString();
            db.closeConnection();

            return count;
        }

        // function to return the total students count in the database
        public string totalStudents()
        {
            return execCount("SELECT COUNT(*) FROM `student`");
        }

        // function to return the total MALE students count in the database
        public string totalMaleStudents()
        {
            return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'");
        }

        // function to return the total FEMALE students count in the database
        public string totalFemaleStudents()
        {
            return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'");
        }
    }
}


//************** End MY_CALC Class **************// 


//************** Start calculate_percentage Form **************// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace csharp__calculate_percentage
{
    class My_CALC
    {
        My_DB db = new My_DB();

        // function to execute count query
        string execCount(string query)
        {
            MySqlCommand command = new MySqlCommand(query, db.getConnection);
            db.openConnection();

            string count = command.ExecuteScalar().ToString();
            db.closeConnection();

            return count;
        }

        // function to return the total students count in the database
        public string totalStudents()
        {
            return execCount("SELECT COUNT(*) FROM `student`");
        }

        // function to return the total MALE students count in the database
        public string totalMaleStudents()
        {
            return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'");
        }

        // function to return the total FEMALE students count in the database
        public string totalFemaleStudents()
        {
            return execCount("SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'");
        }
    }
}

//************** End calculate_percentage Form **************// 


OUTPUT:


How To Calculate Percentage From MySQL In C#