Surfing With Code

Tricks for developers about Java, HTML5, CSS, JavaScript, XML, jSON, deployment, design …

[Quick trick] Remove or empty a column from a table in SQL — febrero 15, 2017
[Quick trick] How to View Which Postgres Version is Running — febrero 6, 2017
Groovy: the Elvis Operator ?: — agosto 25, 2016

Groovy: the Elvis Operator ?:

Groovy is good at shortening normal Java statements. The Elvis operator is to shorten the ternary operator. If we have a sensible default when the value is null or false (following Groovy truth rules) we can use the Elvis operator. And why is it called the Elvis operator? Turn your head to the left and you will know.

def sampleText

// Normal ternary operator.
def ternaryOutput = (sampleText != null) ? sampleText : 'Hello Groovy!'

// The Elvis operator in action. We must read: 'If sampleText is not null assign
// sampleText to elvisOuput, otherwise assign 'Viva Las Vegas!' to elvisOutput.
def elvisOutput = sampleText ?: 'Viva Las Vegas!'
<div class="line number0 index0 alt1 break"></div>

Via: MRHAKI blog

Keyboard locks in IntelliJ IDEA on Ubuntu 14.04 — julio 6, 2016

Keyboard locks in IntelliJ IDEA on Ubuntu 14.04

When you use Intellij IDEA 13 on Ubuntu 14.04 and from time to time the keyboard completely (yep, completely, it’s not about shortcuts) locks in IDEA. You still can do anything you want in IDEA with your mouse, also you can use the keyboard everywhere else except IDEA. And it can be fixed only by restarting IDEA so far… Howewer, you have a better solution:

ibus-daemon -rd


Why enclose JavaScript code in a functions? — febrero 2, 2016

Why enclose JavaScript code in a functions?

Lately, specially with the last JavaScript frameworks, it is common to see the JS code (or files) enclosed in a function as follows:

(function() {
... all javascript code for the file ...

Variables in Javascript have function scope. You’re wrapping your code in a function in order for it not to clobber the global namespace with tons of variables, which may lead to bugs later on when different code is added. E.g.:

// module 1
(function () {
var foo = 'bar';

// module 2
(function () {
var foo = 'baz';

By this way there is no problems, because both modules have their own variable scopes.

Integrate Spring and Webapp project — enero 26, 2016

Integrate Spring and Webapp project

Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web.xml and use a contextConfigLocation to set which context files to load.




Then, you can use the WebApplicationContext to get a handle on your beans or handle them using “@Autowired” annotation




Set different loglevel for different packages/classes —
What are the key differences between SQL Query and Hibernate Query Language (HQL)? — noviembre 19, 2015

What are the key differences between SQL Query and Hibernate Query Language (HQL)?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries which in turns perform action on database.

  • SQL is based on relational database model whereas HQL is a combination of OOPS with Relational database concept.
  • SQL manipulates data stored in tables and modifies its rows and columns while HQL is concerned about objects and its properties.
  • SQL is concerned about the relationship that exists between two tables while HQL is concerned about relation between objects.


How to rename file in Java — noviembre 16, 2015

How to rename file in Java

Java comes with renameTo() method to rename a file. However , this method is platform-dependent. So, the return value (true if the file rename successful, false if failed) should always be checked to make sure the file is rename successful.

package com.surfingwithcode.file;


public class RenameFileExample 
    public static void main(String[] args)
		File oldfile =new File("oldfile.txt");
		File newfile =new File("newfile.txt");
			System.out.println("Rename succesful");
			System.out.println("Rename failed");