November 24, 2014

Beans Validation java.lang.ClassNotFoundException: com.sun.el.ExpressionFactoryImpl

If you are using the Beans Validation framework (JSR 349), you may encounter this obscure exception when doing your validation.

java.lang.ExceptionInInitializerError
Caused by: javax.el.ELException: Provider com.sun.el.ExpressionFactoryImpl not found
Caused by: java.lang.ClassNotFoundException: com.sun.el.ExpressionFactoryImpl

This exception is caused by a missing key lookup in the ValidationMessages.properties file.  Make sure you have the the key in the properties file.

Enjoy!

November 17, 2014

DOS equivalent of Unix/Linux alias

The Unix/Linux alias command is great. But now you are stuck in a DOS environment. What do you do?  Use doskey!

The doskey command can be used in a DOS environment to simulate the kinds of things which alias does.  Here is an example from my alias.bat file:

doskey cdhome=cd c:\Users\michael

doskey tw=cd C:\Users\michael\workspace\ferris-tweial\ferris-tweial\ferris-tweial-app\target\unziped\ferris-tweial-app-1.0.0.0-SNAPSHOT

doskey twr=c:\Applications\java\jdk1.7.0_11\bin\java.exe -jar ferris-tweial-app-1.0.0.0-SNAPSHOT.jar 

As you can see, doskey is very similar to alias.  When on a DOS prompt, all you need to do is type tw or twr and that command will be executed. Of course make your own doskey values.

All of my doskey commands are in an alias.bat file.  This means anytime you open a new DOS prompt you need to execute alias.bat to load all the doskey command. Unacceptable! Instead, create a shortcut for either your desktop or toolbar and you edit the shortcut to execute the alias.bat file for you.  The shortcut will look something like this.

cmd.exe /K "c:\Users\Michael\alias.bat"

That's it.

Enjoy!


November 13, 2014

Remember PreparedStatement?

Simple example using PreparedStatement

Miss the good ole' days when interacting with the database was simple, easy to understand SQL statements and a few lines of code?

Select
public static void main(String[] args) throws Exception {
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    
    String dbLocation 
        = String.format("./db/Sample");
    
    String connectionUrl =
        String.format("jdbc:derby:%s;create=true;user=sample;password=elpmas",dbLocation);
    
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(connectionUrl);
    StringBuilder sp = new StringBuilder();
    sp.append("select \n");
    sp.append("    integer_value \n");
    sp.append("  , decimal_value \n");
    sp.append("  , varchar_value \n");
    sp.append("  , timestamp_value \n");
    sp.append("from example \n");
    PreparedStatement stmt = conn.prepareStatement(sp.toString());
    
    ResultSet rs = stmt.executeQuery();
    System.out.printf("ResultSet\n");
    while (rs.next()) {
     int i = rs.getInt(1);
     BigDecimal bd = rs.getBigDecimal(2);
     String s = rs.getString(3);
     Timestamp ts = rs.getTimestamp(4);
     
     System.out.printf("%d\t%f\t%s\t%s\n",i,bd,s, ts.toString());
    }

    conn.commit();
    conn.close();        
    System.out.printf("Goodbye\n");
}

Insert
public static void main(String[] args) throws Exception {
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    
    String dbLocation 
        = String.format("./db/Sample");
    
    String connectionUrl =
        String.format("jdbc:derby:%s;create=true;user=sample;password=elpmas",dbLocation);
    
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(connectionUrl);
    StringBuilder sp = new StringBuilder();
    sp.append("insert into example ( \n");
    sp.append("    integer_value \n");
    sp.append("  , decimal_value \n");
    sp.append("  , varchar_value \n");
    sp.append("  , timestamp_value \n");
    sp.append(" ) values ( \n");
    sp.append("    ? \n");
    sp.append("  , ? \n");
    sp.append("  , ? \n");
    sp.append("  , ? \n");
    sp.append(" ) \n");
    PreparedStatement stmt = conn.prepareStatement(sp.toString());
    stmt.setInt(1, 19);
    stmt.setBigDecimal(2, new BigDecimal("100.45"));
    stmt.setString(3, "Hello");        
    stmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
    
    int cnt = stmt.executeUpdate();
    
    System.out.printf("count = %d\n", cnt);

    conn.commit();
    conn.close();        
    System.out.printf("Goodbye\n");
}

Enjoy!