Month: April 2011

  • SQL Rounding Error when calculating percentages

    For cross compatability between sql and mysql servers when we do a division on a aggregated variable like count
    we need to promote the INT to a DOUBLE type by multiplying it by 1.00 otherwise our results will loose precision

    Examples


    1/2=0 Incorrect Results
    1.00/2=.5 or 1.00/2.00=.5 Good

    For sake of consistency both variables have been promoted to DOUBLE


    SELECT ROUND((COUNT(FieldA) * 1.00) / (COUNT(FieldB) * 1.00) * 100, 0)
    FROM TESTTABLE

  • Detecting Wide Area Network(WAN) IP programmatically

    Detecting Wide Area Network(WAN) IP programmatically

    This is a reliable way to detect WAN IP address from Java, cpp or any other programming language. Motivation for this was my need to update my DNS server automatically, presented code will be in Java I have decided to use Java for cross-platform support and its my favorite language, in near feature I will include cpp code as well(moding Linksys router to Support xname.org Free DNS service)

    Intro

    There are couple ways of detecting an WAN IP address that I know of, first one is to use ping utility with hop count of 1 then parse the results

    Z:\>ping -r 1 www.google.com

    Pinging www.l.google.com [64.233.167.147] with 32 bytes of data:

    Request timed out.

    drawback of this approach is that it is dependent on ping utility and is not cross platform compatible. The second strategy is to use some external service that will

    echo back us our ip address like http://www.whatismyip.com/ problem with this approach is that if service is down we want be able to retrieve our IP.

    Strategy

    I decided to go with second option using external service, to overcome the possibility of service being down I have assembled list of number of free Ip detection services using Goggle. Next step was to parse the page and extract the IP address, here came up another problem, some of the services contained multiple IP’s. My solution was to query my services while the number of top IP count was less than a set threshold.

    Code

    I am using regular expressions to extract all the IP from retrieved page.

    Regex Here

  • Fixing JasperReports net.sf.jasperreports.engine.JRRuntimeException: Error creating SAX parser

    While upgrading machines I have upgraded to run jdk 1.6 which have screwed up compiling of my reports with following error.

    C:\Program Files (x86)\Java\jdk1.6.0_24\jre\lib\endorsed
    Source File:c:/Users/greg/report2.jrxml
    Exception in thread "main" net.sf.jasperreports.engine.JRRuntimeException: Error creating SAX parser
    	at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.createParser(JRReportSaxParserFactory.java:109)
    	at net.sf.jasperreports.engine.xml.JRXmlDigesterFactory.createParser(JRXmlDigesterFactory.java:1320)
    	at net.sf.jasperreports.engine.xml.JRXmlDigesterFactory.createDigester(JRXmlDigesterFactory.java:1295)
    	at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:199)
    	at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:164)
    	at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:148)
    	at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:85)
    	at com.americanbanksystems.complianceproreports.util.CompileReport.compileReport(CompileReport.java:133)
    	at com.americanbanksystems.complianceproreports.util.CompileReport.main(CompileReport.java:189)
    Caused by: org.xml.sax.SAXNotRecognizedException: http://java.sun.com/xml/jaxp/properties/schemaLanguage
    	at gnu.xml.aelfred2.XmlReader.getProperty(XmlReader.java:181)
    	at gnu.xml.aelfred2.XmlReader.setProperty(XmlReader.java:166)
    	at gnu.xml.aelfred2.JAXPFactory$JaxpParser.setProperty(JAXPFactory.java:147)
    	at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.configureParser(JRReportSaxParserFactory.java:140)
    	at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.createParser(JRReportSaxParserFactory.java:104)
    	... 8 more
    

    My first thought was to place the xalan.jar inside the ‘java.endorsed.dirs’ but that did not fix the problem.

     System.out.println(System.getProperty("java.endorsed.dirs"));

    So after a little investigation I have found the problem and simple solution, download following file or extract it from jasper reports libs folder

    xercesImpl-2.7.0.jar
    

    And place it inside your “java.endorsed.dirs” thats all.