Wednesday, March 14, 2012

How to encrypt using MD5 with a Java code example

Using the code bellow it is possible to generate a MD5 Hash related to strings:


    public static String encrypt(String sign) {   
    
        try {   
           java.security.MessageDigest md =   
              java.security.MessageDigest.getInstance("MD5");   
           md.update(sign.getBytes());   
           byte[] hash = md.digest();   
           StringBuffer hexString = new StringBuffer();   
           for (int i = 0; i < hash.length; i++) {   
              if ((0xff & hash[i]) < 0x10)   
                 hexString.append(   
                    "0" + Integer.toHexString((0xFF & hash[i])));   
              else   
                 hexString.append(Integer.toHexString(0xFF & hash[i]));   
           }   
           sign = hexString.toString();   
        }   
        catch (Exception nsae) {   
           nsae.printStackTrace();   
        }   
        return sign;   
     }  


Here you have a MD5 Hash Generator online:
http://www.miraclesalad.com/webtools/md5.php

Wednesday, January 18, 2012

How to show only one header in sqlplus

When you have to use sqlplus and want to avoid showing table header each n rows returned, you can use the command bellow to show only one header:


set pagesize 0   -- only one header row

Another useful commands:
set colsep ,     -- separate columns with a comma
set trimspool on -- remove trailing blanks
set headsep off  -- this may or may not be useful...depends on your headings.
set linesize X   -- X should be the sum of the column widths

sqlplus reference: http://psoug.org/reference/sqlplus.html
How to create a csv file from sqlplus output: http://stackoverflow.com/questions/643137/how-do-i-spool-to-a-csv-formatted-file-using-sqlplus


Wednesday, January 4, 2012

How to generate Java classes for Hibernate from MySql database tables using Eclipse Indigo


  1. Install JBoss Tools on Eclipse using update site
    In Eclipse Indigo, on menu bar, click Help / Install new Software
    Paste this address in Work with text field and click Add... http://download.jboss.org/jbosstools/updates/development/indigo/
    The mandatory plugin is JBoss Data Services / Hibernate tools, but it is good to install all of them
  2. Create a Hibernate Configuration
    Open Hibernate Perspective
    In Hibernate Configurations panel, right click in a empty space and go to Add Configuration
    In Project field, select the project where Java classes will be created.
    In Database Connection combo box select an existent MySql connection or create a new one clicking on New button.
    In Property file field, click in Setup / Create new
    In Configuration file field, click in Setup / Create new
    Click OK
  3. Run Hibernate Code Generation
    Edit hibernate.cfg.xml file. Remove name property on session-factory tag. Let it with no attributes.
    Add hibernate-tools.jar to the project classpath. It can be found at <eclipse installation folder>\plugins\org.jboss.tools.hibernateextension4_0_3.4.0.v20111213-1944-H40-M5\lib\tools
    On Hibernate perspective, on menu bar, click Run / Hibernate Code Generation /  Hibernate Code Generation Configurations
    In Console configuration, select the just created configuration
    In Output directory, select the source root folder
    In Package, type desired package
    In revenge.xml, click Setup / Create new
    In reveng. strategy, Browse to the class org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy
    Click Run button
  4. Check generated classes out
    Open Java EE perspective
    Refresh project files
    See that entity and DAO classes were generated

Tuesday, December 20, 2011

Auto generated ID's for MySql and Oracle using Hibernate

Here you have auto generated ID's examples using Hibernate with Oracle and MySql databases.

Oracle

...

@Entity
@Table(name = "grupo")
public class Grupo implements GenericModel {


@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GRUPO_ID_SEQ")
@SequenceGenerator(name = "GRUPO_ID_SEQ", sequenceName = "GRUPO_ID_SEQ", allocationSize = 1)
@Column(nullable=false)
private Long id_grupo;

...

Sequence GRUPO_ID_SEQ must exist in Oracle database.


Considering that MySql does not have sequences, if you try to use the code above with MySql you will get a runtime exception:


Caused by: org.hibernate.MappingException: Dialect does not support sequences
at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:619)
at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:88)
at org.hibernate.id.SequenceHiLoGenerator.configure(SequenceHiLoGenerator.java:66)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:127)

MySQL


...
@Entity
@Table(name = "grupo")
public class Grupo implements GenericModel {


@Id
@GeneratedValue
@Column(nullable=false)
private Long id_grupo;
...

The field id_grupo must be marked as auto increment in MySql.




Monday, December 12, 2011

I PASSED: 1Z0-894 Oracle Certified Expert Java Platform, Enterprise Edition 6 JavaServer Pages and Servlet Developer


I did the test on 09/12 and got through!

I was taking 70% in Mocks and hoped to pass with a score about 75% or 80%, but the test was harder than I expected and I passed with 68%.

As I had posted October, 1st, I based my studies on EPractize Labs Study Guide and Mock Exam, because I had already read Katy Sierra's book Head First J2EE1.4 sometimes on other occasions. Also I read in a Servlet 3.0 spec.

I really do not recommend EPractize Labs. The Study Guide is very superficial and Mock has many errors. In addition, the Mock is not consistent with the real exam. The real exam does not cover almost anything about custom tags and it covers much of the Servlet 3.0 spec, but Mock is upside down.

I believe Piotr's web page (http://piotrnowicki.com/2011/03/jee-6-scwcd-mock-questions/) saved me. It has 67 questions focused on the Servlet 3.0 specification. Enthuware Mock also helped a lot (http://enthuware.com/index.php/mock-exams/oracle-certified-expert/oce-jsp-servlet-mock-questions).

This Mock is cheaper and the quality of it is infinitely greater. The proof is that in addition to being 100% Test Pass Garantee, they return the money if you find three errors. Many exam questions were very similar to those on Mock.

My tip for anyone to prepare for this test is:

* Study the book Head First Servlets & JSP, Second Edition (JEE5): http://shop.oreilly.com/product/9780596516680.do?green=25526262438&cmp=af-mybuy-9780596516680.IP 
* Study the JSR-315 Spec Servlets 3.0: http://jcp.org/aboutJava/communityprocess/mrel/jsr315/index.html 
* Answering the questions of Piotr
* Make Enthuware Mock Exams

Following these steps with time, for sure you will do better than me :-)

Good studies!

Thursday, November 10, 2011

Telnet on Windows Vista


On Start menu, click on "Run..." option, then write "optionalfeatures" (without quotes) and click OK button.


"Windows Features" window will open so you can enable Telnet Client feature. Click OK and wait some minutes (MANY MINUTES in my case) while the features are been enabled.


When it is finished you have to restart the system before having fun with Telnet Client.

Tuesday, November 1, 2011

How to find UtcTimeOffsetCode table in TAP3 files

One of the information in Moc (Mobile Originated Calls) and Mtc (Mobile Terminated Calls) is Call Event Start Time Stamp.

It is formed by Local Time Stamp (yyyyMMddhhmmss) plus Utc Time Offset Code.

Utc Time Offset Code (5F-81-68) can be any integer from 0 to 99 and with this information you will be able to discover the Utc Time Offset (5F-81-67).



The problem is this information is not standardized for all TAP files, it is an information self-contained in TAP3 file, in another section. In other words, each file has its own Utc Time Offset Code list, mapping codes to real offsets.

This table is named Utc Time Offset Info and is located in Network Information section. Utc Time Offset Info can have one or more Utc Time Offset Definition and each Definition has the mapping between Utc Time Offset Code to Utc Time Offset.


In the example shown in pictures above, the offset code 65 (0x41) was linked to Utc Time Offset -05:00.

TAP3 protocol was designed this way to minimize the amount of data transferred and to avoid the repetition of
frequently identical information at the call/event level.

Tip:
Nice programs to work with TAP3 files:
TAP3 Editor (http://www.combil.com/tap3ed2.htm): Used to see structured information
ASN1VE (http://www.obj-sys.com/asn1-viewer.php): Used to see binary content of ASN.1 data