Wednesday, August 31, 2016

Show error on grails domain class

When saving the a domain class and the data is not saved to the database, we can trace with this code :


if (!myDomainObj.save()) {
   log.warn myDomainObj.errors.allErrors.join(' \n') //each error is an instance of  org.springframework.validation.FieldError    
}

reference : http://stackoverflow.com/questions/4765037/why-doesnt-grails-notify-me-of-error-at-domain-object-saving

Tuesday, March 15, 2016

Java SQL : Query with paramater

I use this class to run sql query with parameter


/* @author adam_crume
*/
public class NamedParameterStatement {
    /** The statement this object is wrapping. */
    private final PreparedStatement statement;

    /** Maps parameter names to arrays of ints which are the parameter indices. 
*/
    private final Map indexMap;


    /**
     * Creates a NamedParameterStatement.  Wraps a call to
     * c.{@link Connection#prepareStatement(java.lang.String) 
prepareStatement}.
     * @param connection the database connection
     * @param query      the parameterized query
     * @throws SQLException if the statement could not be created
     */
    public NamedParameterStatement(Connection connection, String query) throws 
SQLException {
        indexMap=new HashMap();
        String parsedQuery=parse(query, indexMap);
        statement=connection.prepareStatement(parsedQuery);
    }


    /**
     * Parses a query with named parameters.  The parameter-index mappings are 
put into the map, and the
     * parsed query is returned.  DO NOT CALL FROM CLIENT CODE.  This 
method is non-private so JUnit code can
     * test it.
     * @param query    query to parse
     * @param paramMap map to hold parameter-index mappings
     * @return the parsed query
     */
    static final String parse(String query, Map paramMap) {
        // I was originally using regular expressions, but they didn't work well for ignoring
        // parameter-like strings inside quotes.
        int length=query.length();
        StringBuffer parsedQuery=new StringBuffer(length);
        boolean inSingleQuote=false;
        boolean inDoubleQuote=false;
        int index=1;

        for(int i=0;i





example to use this class : 





...
                NamedParameterStatement p = new NamedParameterStatement(tempConn, "SELECT username, password FROM username WHERE username = :paramUsername");
                p.setString("paramUsername", txtUsername.getText());
                ResultSet tempRs = p.executeQuery();
...

Reference : http://www.javaworld.com/article/2077706/core-java/named-parameters-for-preparedstatement.html

Wednesday, January 27, 2016

VB6 Compatibility Issue on Windows 7

My project it using VB6 and ADO. When I compile the project on Windows 7 machine, the generated application (.exe file) is working well on other Windows 7 machine. But then error when its running on Windows XP machine.

Solution: download Windows6.1-KB2640696-v3-x86.msu from microsoft website. Then install this update package on developer machine (on my Windows 7 machine) and recompile the project

reference: https://support.microsoft.com/en-us/kb/2640696