Thursday 15 April 2010

java - Getting Fractions of a Cent -



java - Getting Fractions of a Cent -

hey guys got first java job if things go may never need code again. need connect database , apply involvement big number of transactions. having problem getting math work right on local machine. must right within fraction of cent. ideas? in advance!

public connection getconnection() throws sqlexception { connection conn = null; properties connectionprops = new properties(); connectionprops.put("user", "my_user"); connectionprops.put("password", "my_password"); if (this.dbms.equals("mysql")) { conn = drivermanager.getconnection( "jdbc:" + this.dbms + "://" + "yr1f4k3qas3rv3r" + ":" + this.portnumber + "/", connectionprops); } else if (this.dbms.equals("derby")) { conn = drivermanager.getconnection( "jdbc:" + this.dbms + ":" + this.dbname + ";create=true", connectionprops); } system.out.println("connected database"); homecoming conn; } public static void applyinteresttohighvolumeaccounts(connection con, string dbname, string interesttoapply) throws sqlexception { statement stmt = null; string query = "select * "from " + dbname + ".highvolumeaccounts"; seek { stmt = con.createstatement(); resultset rs = stmt.executequery(query); while (rs.next()) { string accountname = rs.getstring("accountname"); int accountnumber = rs.getint("accountnumber"); int balance = rs.getint("balance"); int involvement = interesttoapply int newbalance = balance + (balance * interest) - (balance * 0.00000001%) int addtoretirement = balance * 0.000001% string getrich = "update tbl_accounts set balance=balance" + addtoretirement + " accountname=privateaccountinthecaymens"; resultset rs = stmt.executequery(getrich); string adjustbalance = "update tbl_accounts set balance=balance" + newbalance + " accountname=accountname"; resultset rs = stmt.executequery(adjustbalance); } } grab (sqlexception e ) { jdbctutorialutilities.printsqlexception(e); } { if (stmt != null) { stmt.close(); } } }

i think close. bigdecimals 1 way go. utilize next code verbatum , should fine:

import java.sql.*; import java.math.bigdecimal; import java.math.mathcontext; public class adjustaccounts{ static final string jdbc_driver = "com.mysql.jdbc.driver"; static final string db_url = "jdbc:mysql://yr1f4k3qas3rv3r"; static final string user = "my_user"; static final string pass = "my_password"; public static void main(string[] args) { connection conn = null; statement stmt = null; try{ class.forname("com.mysql.jdbc.driver"); system.out.println("connecting database..."); conn = drivermanager.getconnection(db_url,user,pass); stmt = conn.createstatement(); string sql; sql = "select * highvolumeaccounts"; resultset rs = stmt.executequery(sql); while(rs.next()){ //retrieve column name int accountnumber = rs.getint("accountnumber"); bigdecimal balance = rs.getbigdecimal("balance"); string accountname = rs.getstring("accountname"); double pennyshave = 0.000000001; bigdecimal difference = balance.multiply(new bigdecimal(pennyshave)); //pad business relationship sql = "update tbl_accounts set balance=balance +" + difference + " accountnumber=00098793302999"; //don't worry number, java thing stmt.executequery(sql); //adjust other one. sql = "update tbl_accounts set balance=balance -" + difference + " accountname="+ accountname; stmt.executequery(sql); } rs.close(); stmt.close(); conn.close(); }catch(sqlexception se){ se.printstacktrace(); }catch(exception e){ e.printstacktrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(sqlexception se2){ } try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); } } }

}

java math

No comments:

Post a Comment