ianneAB
ianneAB

Reputation: 29

How to retrieve and/or add database table values and display in the textfield

computeHandler.java

public class computeHandler extends DBCollection
{
    private int totalcapitalPrice, totalprice;
    
    public void setTotalCapitalPrice(int temp)
    {
        this.totalcapitalPrice = temp;
    }
    public void setTotalPrice(int temp)
    {
        this.totalprice = temp;
    }
    
    public int getTotalCapitalPrice()
    {
        return this.totalcapitalPrice;
    }
    public int getTotalPrice()
    {
        return this.totalprice;
    }
}

where do I put the

SELECT SUM(Capital_Price) FROM item_list

and

SELECT SUM(Price) FROM item_list

I have a class named DBCollection which extends DBConnection and connects my codes to my database...

DBCollection.java

public class DBCollection extends DBConnection{
private Connection connect = null;
private ResultSet rs = null;
private Statement stmt = null;

public DBCollection()
{
    try
    {
        connect = super.getConnection();
        stmt = connect.createStatement();
        //JOptionPane.showMessageDialog(null,"Connected!");
        
    }
    catch(Exception ex)
    {
        //JOptionPane.showMessageDialog(null,"Not Connected!");
    }
}

private void closeConnection()
{
    super.closeConnection(connect);
    super.closeRecordSet(rs);
    super.closeStatement(stmt);
}

I want to sum up all of the saved Capital_Price and Price values in my database.. and retrieve it to capitalPriceTF (textfield) and priceTF (textfield also).. how do I do that?

I also want to retrieve the First_name of the saved user... and display it to the Welcome(first name of user)..

Upvotes: 2

Views: 1280

Answers (1)

Salah
Salah

Reputation: 8657

You need to execute your query using Statement object:

So from statement object :

stmt = connect.createStatement();

Execute Your queries like this :

rs = stmt.executeQuery("SELECT SUM(Capital_Price) sum FROM item_list");

ResultSet rs object holds the retrieved data from by that query, so you can get the data like:

int sum = rs.getInt("sum");

Then what all you have to do, just organize your code based on the above and you will get every thing works just fine.

Upvotes: 1

Related Questions