0

Hi i am new to this and i am locked in this problem, i am trying to figure out whether you can add a set of values (1,000 ....) to another value. Though i successfully linked an input that automatically changed into comma to the table in SQL, it seems that it cannot add two values that has that format, my client wants to see a comma in a table and when inputting values.

Can you guide me where to change my code, is there an automatic format of these in SQL? Or probably in the java code, I read some articles that you cannot do a comma format before adding as it is done after that.

Here is the code I added to automatically change the numbers into having a comma:

private void FaremoKeyReleased(java.awt.event.KeyEvent evt)
{
    String a = Faremo.getText();

    if (a.isEmpty())
    {
        a = "";
    }
    else
    {
        a = a.replace(",","");
        a =  NumberFormat.getNumberInstance(Locale.ENGLISH).format(Double.parseDouble(a));
        a = a.replace(".", ",");
    }
    Faremo.setText(a);
}

My problem is i cannot add values linked to my SQL with that format. [Sample data - https://i.stack.imgur.com/kVQII.png]

When i am getting the sum of it I am using these:

   private void SumActionPerformed(java.awt.event.ActionEvent evt) { 
  try{
    String sql = "Select sum(Faremo),sum(Eatery),sum(Admin),sum(Jisoo),sum(Cav1),sum(Cav2),sum(Reliance1),sum(Reliance2)from Dataa";
    pst = conn.prepareStatement(sql);
    rs=pst.executeQuery();
    if(rs.next()){
        String sum1 = rs.getString("sum(Faremo)");
        SFaremo.setText(sum1);

        String sum2 = rs.getString("sum(Eatery)");
        SEatery.setText(sum2);

        String sum3 = rs.getString("sum(Admin)");
        SAdmin.setText(sum3);

        String sum4 = rs.getString("sum(Jisoo)");
        SJisoo.setText(sum4);

        String sum5 = rs.getString("sum(Cav1)");
        SCav1.setText(sum5);

        String sum6 = rs.getString("sum(Cav2)");
        SCav2.setText(sum6);

        String sum7 = rs.getString("sum(Reliance1)");
        SReliance1.setText(sum7);

        String sum8 = rs.getString("sum(Reliance2)");
        SReliance2.setText(sum8);

    }
}
catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
}
Update_table();

}
John
  • 19
  • 3

2 Answers2

0

Are the table columns where you want to save the numbers defined as float?

Sundaze
  • 195
  • 1
  • 3
  • 11
  • I used TEXT and VARCHAR – John Nov 19 '15 at 08:25
  • So you can't add a formated number like 1,000 to your table as a new column? – Sundaze Nov 19 '15 at 08:30
  • no, i can add it just fine, the problem is when performing operation such as SUM of all values in it – John Nov 19 '15 at 08:34
  • Okey but why do you save it with a comma then in the database ? Wouldn't it be better to save it as normal number and format it when you select it ? – Sundaze Nov 19 '15 at 08:48
  • hmm i am just linking my database to display it in the table in the netbeans so i guess adding with comma is not possible, but also i want to display a comma in my table, i might look up for a solution to these.Thanks btw – John Nov 20 '15 at 09:27
0

If you are sure that a value will only be a number make the table columns int. Depending on the kind of numbers, you can choose a variety of Data Types: float, decimal,real. These could all work. It is all depending on what the numbers represent.

If you are going to do math IN the cells, a double gives rounding problems (link). For more info on Data Types check Data Types.

Community
  • 1
  • 1
Nina
  • 99
  • 12
  • hmm, i get it and will follow your guide but not an answer i was looking for – John Nov 20 '15 at 09:23
  • Also possible is when doing a `SUM` of all values, `parse` the text/varchar (if they are all numbers) to a `float`, then do the SUM, and then `parse` them to `String` again. Just make sure to add the right amount of decimals. – Nina Nov 20 '15 at 10:19