Monday, February 23, 2015

How to create a maven project

mvn -X  archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.hordine  -DartifactId=hordineCmdTwitter
 
 
mvn eclipse:eclipse
 
 
mvn package
 
mvn clean install  


Thursday, February 12, 2015

Parsing JSON string in Java

Here is the fully working and tested corrected code:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShowActivity {


  private final static String JSON_DATA =
     "{" 
   + "  \"geodata\": [" 
   + "    {" 
   + "      \"id\": \"1\"," 
   + "      \"name\": \"Julie Sherman\","                  
   + "      \"gender\" : \"female\"," 
   + "      \"latitude\" : \"37.33774833333334\"," 
   + "      \"longitude\" : \"-121.88670166666667\""
   + "    }," 
   + "    {" 
   + "      \"id\": \"2\"," 
   + "      \"name\": \"Johnny Depp\","          
   + "      \"gender\" : \"male\"," 
   + "      \"latitude\" : \"37.336453\"," 
   + "      \"longitude\" : \"-121.884985\""
   + "    }" 
   + "  ]" 
   + "}"; 

  public static void main(final String[] argv) throws JSONException {
    final JSONObject obj = new JSONObject(JSON_DATA);
    final JSONArray geodata = obj.getJSONArray("geodata");
    final int n = geodata.length();
    for (int i = 0; i < n; ++i) {
      final JSONObject person = geodata.getJSONObject(i);
      System.out.println(person.getInt("id"));
      System.out.println(person.getString("name"));
      System.out.println(person.getString("gender"));
      System.out.println(person.getDouble("latitude"));
      System.out.println(person.getDouble("longitude"));
    }
  }
}
 
 
 
 
Here's the output:
C:\dev\scrap>java -cp json.jar;. ShowActivity 1 Julie Sherman female 37.33774833333334 -121.88670166666667 2 Johnny Depp male 37.336453 -121.884985
 

Wednesday, February 11, 2015

Parse JavaScript with jsoup

Document doc = Jsoup.parse(html);

Element script = doc.select("script").first();

Sunday, February 8, 2015

how to extract zip file in linux

Use unzip command:


Code:
unzip file.zip
many time unzip is not installed by default so install it before using it, use rpm or apt-get/yum command to install unzip

How to open link in new tab on html?

Set the 'target' attribute of the link to _blank:

<a href="#" target="_blank">Link</a>
 
Edit: for other examples, see here: http://www.w3schools.com/tags/att_a_target.asp

(Note: I previously suggested blank instead of _blank because, if used, it'll open a new tab and then use the same tab if the link is clicked again. However, this is only because, as GolezTrol pointed out, it refers to the name a of a frame/window, which would be set and used when the link is pressed again to open it in the same tab).

First Login: HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException

It appears that MySQL or a firewall is killing off your inactive connections that are hanging around in your jdbc connection pool for long periods of time:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
The last packet successfully received from the server was 4,665,488 milliseconds ago.

Check the value of wait_timeout on MySQL.

You can play around with DBCP settings e.g. validationQuery, testOnBorrow and testWhileIdle.

A a confuguration that is 'belt and braces', and will probably solve your problem at the expense of performance is:
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  <property name="validationQuery" value="SELECT 1"/>
  <property name="testOnBorrow" value="true"/>
</bean>
The above will test connections every time you borrow from the pool.

Thursday, February 5, 2015

Solving the mySQL “The security settings could not be applied” error

Yesterday I got this error message during the last step of the MySQL Windows installer: “The security settings could not be applied to the database because the connection has failed with the following error”.

The issue can be solved issue this procedure from the MySQL website.

First, cancel the wizard and make sure that the MySQL service is stopped.

Then, create a text file using a SQL query that will reset the root password.

1
2
UPDATE mysql.user SET Password=PASSWORD('admin') WHERE User='root';
FLUSH PRIVILEGES;

Then, launch mySQL using the following command line:

1
mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" --init-file="C:\mysql-init.txt" --console

Note: it’s better to use the ”–console” option so that error messages are correctly displayed.

Finally, open a new DOS shell and execute the following command line to shutdown mySQL:

1
mysqladmin -u root -p shutdown

You can now restart the mySQL installer and choose the “Repair” option: