Pages

Monday, August 13, 2012

Spring MVC file upload NullPointerException

Took me hours to figure out and in the end I realized I just had to add this small piece of resolver in my dispatcher-servlet.


    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="100000"/>
    </bean>

Monday, July 30, 2012

PostgreSQL create table if not exists


Try running a query on the table. If it throws an exception then catch the exception and create a new table.
try {
    int a =  db.queryForInt("SELECT COUNT(*) FROM USERS;");
}
catch (Exception e) {
    System.out.print(e.toString());
    db.update("CREATE TABLE USERS (" +
                "id SERIAL," +
                "PRIMARY KEY(id)," +
                "name varchar(30) NOT NULL," +
                "email varchar(30) NOT NULL," +
                "username varchar(30) NOT NULL," +
                "password varchar(30) NOT NULL" +
                ");");
}
return db;

Wednesday, July 25, 2012

Example of Log4J

Log4J Example:

@Controller
public class VanityController {
    private static Logger logger = Logger.getLogger(VanityController.class.toString());

    @RequestMapping(value= "/user/{vanity}", method = RequestMethod.GET)
    public ModelAndView viewProfile(@PathVariable String vanity) {
        ModelAndView mv = new ModelAndView("vanity");
        logger.info(vanity + "vanity called!");
        mv.addObject("name", vanity);
        return mv;
    }

}

when called localhost:8080/user/todo
this is what was displayed in the output: 
INFO: todovanity called!

Adding Log4J dependency to Maven


<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      <scope>runtime</scope>
   </dependency>

Tuesday, July 24, 2012

FATAL: password authentication failed for user postgres


If you face this error on trying to use psql, the following steps should fix it:

By default, postgres creates a user named 'postgres'. We log in as postgres, and give a password.

$ sudo -u postgres psql
\password
Enter password: ...
...

Now try again. The above steps work for a fresh install of postgres 9.1 and 8.4 on Ubuntu 12.04.