Powered By Blogger

Sunday, December 4, 2011

JSON Notes

JSON is abbreviation of Javascript Object and Array Notation
=======================
create an array in Javascript:
var myArray = new Array("Taipei", "Taichung", "Tainan");
or
var myArray = ["Taipei", "Taichung", "Tainan"];
Javascript array can be a mixed array which contains any type of object, string , number or customized object.
create an object in Javascript:
var myDog = new Object( );
myDog.name = "Allen";
myDog.age = 5;
myDog.color = "blue";
=======================
or
var myCat = {
   "name":"Allen";
   "age":5;
   "color":"blue";
}
=======================
a mixed use of array and object
var myDogs = [ {
   "name":"Allen";
   "age":5;
   "color":"blue"; },{
   "name":"Ben";
   "age":6;
   "color":"red";} ];
alert(myDogs[0].name);
alert(myDogs[1].age);
=======================
Example of JSON
var json = '{"myCats": [ {"name": "Hero", "age": 5, "color": "silver" }, {"name": "Euro", "age": 2, "color": ["brown", "white", "black"] }]}';
var obj = eval ("(" + json + ")"); 
alert('I have ' + obj.myCats.length + ' cats.'); 
alert(obj.myCats[0].name); 
alert(obj.myCats[1].name);
But eval( ) is dangerous for XSS attack so we use parser instead.
var obj = JSON.parse(json); 
alert('I have ' + obj.myCats.length + ' cats.'); 
alert(obj.myCats[0].name);
alert(obj.myCats[1].name);
=======================
Here is the way transfer an object to JSON string.
<script type="text/javascript" src="http://www.json.org/json.js"></script> 
<script type="text/javascript">
 var obj = new Object(); 
obj.myCats = new Array(new Object(), new Object()); 
obj.myCats[0].name = "Hero"; 
obj.myCats[0].age = 5; 
obj.myCats[0].color = "silver"; 
obj.myCats[1].name = "Euro"; 
obj.myCats[1].age = 2; 
obj.myCats[1].color = ["black", "white", "brown"]; 
alert(JSON.stringify (obj)); 
</script>

























Thursday, December 1, 2011

Hibernate @OneToOne

============================================================================
@Entity
@Table(name="Master")
public class Master implements Serializable{
       private MasterId id;
       private String Sex; 


       private Detail detail;


       @EmbeddedId
       @AttributeOverrides({
             @AttributeOverride(name="pId", column= @Column( name="P_ID", nullable=false)),
             @AttributeOverride(name="pName", column= @Column( name="P_NAME", nullable=false)) })
       public MasterId getId( ){
           return this.id;
       }
       public void setId(MasterId id) {
           this.id=id;
       }


       @Column(name="SEX", nullable=false, length=4)
       public String getSex( ){
           return this.Sex;
       }
       public void setSex(String Sex){
           this.Sex=Sex;
       }


       @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
       @JoinColumn(name="P_ID", insertable=false , updatable=false)
       public Detail getDetail( ){
            return detail;
       }
       public void setDetail(Detail detail){
            this.detail=detail;
       }
}
============================================================================


@Embeddable
public MasterId implements Serializable{


        private String pId;
        private String pName;


        @Column(name = "P_ID", nullable = false, length=15)
public String getPId( ) {
return this.pId;
}
public void setPId(String pId) {
this.pId = pId;
}


        @Column(name = "P_NAME", nullable = false, length=15)
public String getPName( ) {
return this.pName;
}
public void setPName(String pName) {
this.pName = pName;
}


//public boolean equals(Object other) { }


//public int hashCode( ) { }
}
============================================================================
@Entity
@Table(name = "DETAIL")
public class Detail implements Serializable{
       private String pId;


       @Id
       @Column(name = "P_ID", unique = true, nullable=false)
       public String getPId( ) {
return this.pId;
}
public void setPId(String pId) {
this.pId = pId;
} 
}

Sunday, November 27, 2011

SQL Basic Syntax

SELECT:
1. SELECT col1, col2, col3 FROM tablename 
2. SELECT DISTINCT col FROM tablename
3. SELECT COUNT(col) FROM tablename
4. SELECT col FROM tablename WHERE col = value
5. SELECT col FROM tablename WHERE col1 = value1 AND col2 = value2
6. SELECT col FROM tablename WHERE IN (value1 AND value2)
7. SELECT col FROM tablename WHERE BETWEEN (value1 AND value2)
8. SELECT col FROM tablename WHERE col LIKE '%value%'
9. SELECT col FROM tablename ORDER BY col

UPDATE:
1. UPDATE tablename SET col value2 WHERE col value1

DELETE:
1. DELETE tablename WHERE col = value

Thursday, November 24, 2011

Get the Current Time

import java.util.Calendar;
import java.util.Date;




public class GetTheCurrentTime {
  public static void main( String[] args ) {
    // one way
    long currentTimeInMillis = System.currentTimeMillis();
    Date today = new Date( currentTimeInMillis );
    System.out.println( today );


    // another way
    Calendar cal = Calendar.getInstance();
    today = cal.getTime();
    System.out.println( today );
  }
}

Wednesday, November 23, 2011

Eclipse SVN Plug-in

If you're using Eclipse 3.6, just use the following link for installing new software.
http://subclipse.tigris.org/update


But If you're using 3.7 Indigo version, use the other one then.
http://subclipse.tigris.org/update_1.6.x

Tuesday, November 22, 2011

m2e Compiler J2SE-1.5 Warning Message

I got the warning message as below while developing m2e project:


"Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace that are strictly compatible with this environment."


Simple way to solve this problem is to add JRE System library in Java build path.
Preferences->Java->Installed JREs->Add


Or go  Preferences->Java->Compiler
to change JDK Compliance to 1.6
However, this will all back to 1.5 if execute maven -update project configuration. Therefore, the best way is to modify the file "maven-compiler-plugin.pom", that is located in
C:\Documents and Settings\xxx\.m2\repository\org\apache\maven\plugins\maven-compiler-plugin\2.3.2


  <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.1</version>
          <configuration>
            <source>1.6</source>
            <target>1.6</target>
          </configuration>
  </plugin>
Then, the warning message should be fixed!