Tuesday, February 18, 2014

Rounding Financial Numbers in Java with HALF_EVEN: what you should know

Dealing with financial application is always appealing and also sensitive. I will rather leave the appealing part to you and go to the sensitive part. I had a bad luck to develop an application for accounting. Every things seemed ok till the D-day came.  Hey Soulemane what have you done ?  Then what really happen ?
Trust me these accounts people worship numbers, they don't even tolerate a slight deviation of 0.0000001 cent. 

 Now coming back to HALF EVEN what does javadoc really say about it ?

Rounding mode to round towards the "nearest neighbor"  unless both neighbors are equidistant, in which case, round  towards the even neighbor. Behaves as for   RoundingMode.HALF_UP if the digit to the left of the   discarded fraction is odd; behaves as for  RoundingMode.HALF_DOWN if it's even. Note that this   is the rounding mode that statistically minimizes cumulative   error when applied repeatedly over a sequence of calculations.   It is sometimes known as "Banker's rounding," and is   chiefly used in the USA. This rounding mode is analogous to   the rounding policy used for float and double   arithmetic in Java. 
Example:

Input NumberInput rounded to one digit
with HALF_EVEN rounding *
5.5 6
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -6

In addition to that I would rather add the following explanation when you are in the middle i.e 5  :



Before the account take his/her calculator and start arguing with you get prepare as he might what 12. 455  to be 12. 46 instead of 12. 45. 

However this remain an opened issue feel free to share your experience by dropping a comment.

Sunday, February 16, 2014

MySQL Server Logs

MySQL Server has several logs that can help you find out what activity is taking place.

Log TypeInformation Written to Log
Error logProblems encountered starting, running, or stopping mysqld
ISAM logChanges to the ISAM tables (used only for debugging the ISAM code)
General query logEstablished client connections and statements received from clients
Update logStatements that change data (this log is deprecated)
Binary logStatements that change data (also used for replication)
Relay logData changes received from a replication master server
Slow query logQueries that took more than long_query_time seconds to execute

By default, all log files are created in the data directory: in windows C:\xampp\mysql\data

Saturday, February 15, 2014

Error: MySQL shutdown unexpectedly.

Just go to C:\xampp\mysql\data\ibdata1 delete it and restart your database. it should work now.

Tuesday, February 11, 2014

Location of php.ini configuration file

Even though this may seem silly at glance, often when time comes you will probably waste some precious minutes to look for this location. And maybe you have already done so while reading this post. Now where is the location of the php.ini file in your xampp folder ?
It is located just here  C:\xampp\php


Tuesday, February 4, 2014

Oracle JDBC connection in java under Netbeans

1. Create your project:

2. Project name: jdbcProject
3.Downoad the oracle jar ojdbc14.jar from here or here

4. Add the jar to your project:
   - by right clicking your oracle project
   -Click on Properties
   -Under Categories at the left Select Libraries
   -Click on Add Jar/Folder button at the right
   -Go to the jar location , select it and Open, OK, OK

5. Create a table in your oracle database:
create table student (name varchar(20), country varchar(20));
6. Write code
imports:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

 Your Code:

public class JdbcProject {

    
    public static void main(String[] args) {
       
        try {  
          String sid="orcl";  //your oracle sid
          String username="user"; 
          String password= "pass";  
          DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
          Connection connection = DriverManager.getConnection(  
                "jdbc:oracle:thin:@localhost:1521:"+sid, username, password);  

        //Statement statement = connection.createStatement();  
           
          String name="soulemane";
          String country="cameroon";
          String query = " insert into student(name,country)values(?,?)";
          PreparedStatement preparedStmt = connection.prepareStatement(query);
           //System.out.println("Data is inserted:");
           preparedStmt.setString(1, name);
           preparedStmt.setString(2,country);

            // execute the preparedstatement
           preparedStmt.execute();
           connection.close(); 
          } catch (Exception e) {  
            e.printStackTrace();  
          }  
           System.out.println("Data is inserted:");
    }
}
  
 7. Final RUN your project and have fun