Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, June 16, 2009

Convert an array to collection

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;

public class ArraysExample
{
public static void main(String[] args)
{
String[] array = {"Happy", "New", "Year", "2006"};
List list = Arrays.asList(array);

Iterator iterator = list.iterator();
while (iterator.hasNext())
{
System.out.println((String) iterator.next());
}
}
}

The result of our code is:

Happy
New
Year
2006

ref: http://www.kodejava.org/examples/25.html

Tuesday, June 9, 2009

Get number of rows in resultset

You can extract the data from the resultset from the top and rs.last() will move you to the end of the resultset.

Then, the method rs.getRow() get the row number of the last row and also shows you the number of rows in the table.

For example:

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
String query = "select [table_field] from [table_name]";
stmt = conn.createStatement();

rs = stmt.executeQuery(query);
while (rs.next()) {
String id = rs.getString(1);
}
rs.last();
int rowCount = rs.getRow();
System.out.println("Number of Rows=" + rowCount);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {}
}


ref: http://www.roseindia.net/java/java-get-example/number-rows-resul.shtml

Thursday, June 4, 2009

java BigDecimal problem

BigDecimal v = new BigDecimal(0.12d);
System.out.println(v);

result: 0.11999999999999999555910790149937383830547332763671875

Because ::: In Java API

public BigDecimal(double val)

Translates a double into a BigDecimal. The scale of the BigDecimal is the smallest value such that (10scale * val) is an integer.

Note: the results of this constructor can be somewhat unpredictable.

---

But ...


BigDecimal v = new BigDecimal("0.12");
System.out.println(v);

result: 0.12

Because:

The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=15

java decimal using BigDecimal

BigDecimal v = new BigDecimal(0.0d);
BigDecimal av = new BigDecimal(0.1d);
av = av.setScale(2, RoundingMode.HALF_EVEN);
for (int i = 0; i < 10; i++) {
v = v.add(av);
System.out.println(v);
}

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0

java decimal format

java.text.DecimalFormat dfm = new java.text.DecimalFormat("0.00");

double d = 0.123d;
System.out.println(dfm.format(d)); // 0.12

d = 0.129d;
System.out.println(dfm.format(d)); // 0.13

d = new Double(dfm.format(d)).doubleValue();
System.out.println(d); // 0.13


OR ...

DecimalFormat changeFormat = new DecimalFormat("#,##0.00");

double a = 20213243;

BigDecimal aa = new BigDecimal(a);

BigDecimal divideA = aa.divide(new BigDecimal(3),4,4);

System.out.println("divideA = "+divideA);
System.out.println("Result = "+changeFormat.format(divideA));


result:
divideA = 6737747.6667
Result = 6,737,747.67

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0

Wednesday, June 3, 2009

java double problem

public class HOWCOME
{
public static void main(String[] args) {
double v = 0.0d;
for (int i = 0; i < 10; i++) {
v += 0.1d;
}
System.out.println(v);
}
}

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0