2015年9月24日 星期四

How to make a self extracting archive that launches your installer after extraction


source : http://ntsblog.homedev.com.au/index.php/2015/05/14/self-extracting-archive-runs-setup-exe-7zip-sfx-switch/



It is actually quite simple to do if you follow these easy steps.

Step 1 – Setup your installation folder

To make this easy create a folder c:\Install. This is where we will copy all the required files.

Step 2 – 7Zip your installers

  1. Go to the folder that has your msi and your setup.exe
  2. Select both the .msi and the setup.exe
  3. Right-Click and choose 7Zip –> “Add to Archive”
  4. Name your archive “Installer.7z” (or a name of your choice)
  5. Click Ok
  6. You should now have “Installer.7z”.
  7. Copy this .7z file to your c:\Install directory

Step 3 – Get the 7z-Extra sfx extension module

You need to download the 7z-Extra.
  1. Follow this link to go to 7zip download.
  2. You need to download the 9.20 version (as @ May-2015) as the beta does not contain the correct files.
  3. direct download link.
  4. Extract the 7zip extra files
  5. Copy the file “7zS.sfx” to c:\Install

Step 4 – Setup your config.txt

I would recommend using NotePad++ to edit this text file as you will need to encode in UTF-8, the following instructions are using notepad++.
  1. Using windows explorer go to c:\Install
  2. right-click and choose “New Text File” and name it config.txt
  3. right-click and choose “Edit with NotePad++
  4. Click the “Encoding Menu” and choose “Encode in UTF-8”
  5. Enter something like this:
    1
    2
    3
    4
    5
    ;!@Install@!UTF-8!
    Title="SOFTWARE v1.0.0.0"
    BeginPrompt="Do you want to install SOFTWARE v1.0.0.0?"
    RunProgram="setup.exe"
    ;!@InstallEnd@!
Edit this replacing [SOFTWARE v1.0.0.0] with your product name.
Notes on the parameters and options for the setup file are here

CheckPoint

You should now have a folder “c:\Install” with the following 3 files:
  1. Installer.7z
  2. 7zS.sfx
  3. config.txt

Step 5 – Create the archive

These instructions I found on the web but nowhere did it explain any of the 4 steps above.
  1. Open a cmd window, Window + R –> cmd –> press enter
  2. In the command window type the following
    1
    2
    3
    cd \
    cd Install
    copy /b 7zS.sfx + config.txt + Installer.7z MyInstaller.exe
  3. Look in c:\Install and you will now see you have a MyInstaller.exe
  4. You are finished

    Run the installer

    Double click on MyInstaller.exe and it will prompt with your message. Click OK and the setup.exe will run.
    Everything is easy… once you know how. :-)

    P.S. Note on Automation

    Now that you have this working in your c:\Install directory I would create an “Install.bat” file and put the copy script in it.
    1
    copy /b 7zS.sfx + config.txt + Installer.7z MyInstaller.exe
    Now you can just edit and run the Install.bat every time you need to rebuild a new version of you deployment package.

2015年9月17日 星期四

drag and drop html


http://hmkcode.com/java-servlet-jquery-file-upload/

http://www.java2s.com/Code/Jar/j/Downloadjavaxservlet30jar.htm

http://html5demos.com/dnd-upload


http://jsfiddle.net/danielzen/utp7j/


https://www.youtube.com/watch?v=hqSlVvKvvjQ


/******************************************************************/

<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Drag & Drop Uploading </title>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div id='uploads'><div>
<div class='dropzone' id='dropzone'>Drop files here to upload</div>
<script>
(function (){var dropzone=document.getElementById('dropzone');
var upload=function(files){
//console.log(files);
var formData=new FormData();
xhr=new XMLHttpRequest(),x;
for (x=0;x<files.length;x=x+1){
formData.append('file[]',files[x]);
}

xhr.onload=function(){
var data=this.responseText;
console.log(data);
}
xhr.open('post','upload.php')
xhr.send(formData);

}
dropzone.ondrop=function(e){
e.preventDefault();
this.className='dropzone';
console.log(e.dataTransfer.files);
var files = e.target.files || e.dataTransfer.files;
for (var i = 0, file; file = files[i]; i++) {
alert(file.webkitRelativePath+' '+file.name);
}
//alert(e.dataTransfer.files);
//upload(e.dataTransfer.files);
}

dropzone.ondragover=function(){
this.className='dropzone dragover'; 
return false;
}
dropzone.ondragleave=function(){
this.className='dropzone'; 
return false;
}
}())
</script>
</body>
</html>

2015年8月26日 星期三

mysqldump data structure

mysqldump -upassword -ppassword --add-drop-table --no-data BR201312111529 > tables.sql


 mysqldump -upassword -ppwassword BR201312111529 SETTINGS  >SETTINGS.sql

INSERT INTO COMPANY_POLICY SELECT * FROM  BR201312111529.COMPANY_POLICY;


2015年8月20日 星期四

mysql-procedure update all po SALES_ORDER_INV_DESC row_no

delimiter $$
drop procedure updateSALES_ORDER_INV_DESCRowNo $$
CREATE PROCEDURE updateSALES_ORDER_INV_DESCRowNo ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE mrow INT ;
declare mRECORDID varchar(20);
declare mPARENT_RECORDID varchar(20) DEFAULT '';
declare mLAST_PARENT_RECORDID varchar(20) DEFAULT '';
DEClARE SALES_ORDER_INV_DESC_cursor CURSOR FOR SELECT PARENT_RECORDID,RECORDID FROM SALES_ORDER_INV_DESC ORDER BY PARENT_RECORDID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN SALES_ORDER_INV_DESC_cursor;
get_recordid: LOOP
FETCH SALES_ORDER_INV_DESC_cursor INTO mPARENT_RECORDID, mRECORDID;
IF done THEN
LEAVE get_recordid;
END IF;
/**********/
IF mPARENT_RECORDID<>mLAST_PARENT_RECORDID THEN
SET mLAST_PARENT_RECORDID=mPARENT_RECORDID;
SET MROW=1;
END IF;
UPDATE SALES_ORDER_INV_DESC SET ROW_NO=MROW,TIMESTAMP=TIMESTAMP WHERE RECORDID=MRECORDID AND ROW_NO=0;
SET MROW=MROW+1;
/**********/
END LOOP get_recordid;
END;
$$
DELIMITER ;
call updateSALES_ORDER_INV_DESCRowNo ();



/**************************************************************/

delimiter $$
drop procedure updatePoRowNo $$
CREATE PROCEDURE updatePoRowNo ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE mrow INT ;
declare mRECORDID varchar(20);
declare mPARENT_RECORDID varchar(20) DEFAULT '';
declare mLAST_PARENT_RECORDID varchar(20) DEFAULT '';
DEClARE po_cursor CURSOR FOR SELECT PARENT_RECORDID,RECORDID FROM PO ORDER BY PARENT_RECORDID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN po_cursor;
get_recordid: LOOP
FETCH po_cursor INTO mPARENT_RECORDID, mRECORDID;
IF done THEN
LEAVE get_recordid;
END IF;
/**********/
IF mPARENT_RECORDID<>mLAST_PARENT_RECORDID THEN
SET mLAST_PARENT_RECORDID=mPARENT_RECORDID;
SET MROW=1;
END IF;
UPDATE PO SET ROW_NO=MROW WHERE RECORDID=MRECORDID;
SET MROW=MROW+1;
/**********/
END LOOP get_recordid;
END;
$$
DELIMITER ;
call updatePoRowNo ();



/*********************************************************/

delimiter $$
drop procedure updatePAYMENT_LINERowNo $$
CREATE PROCEDURE updatePAYMENT_LINERowNo ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE mrow INT ;
declare mRECORDID varchar(20);
declare mPARENT_RECORDID varchar(20) DEFAULT '';
declare mLAST_PARENT_RECORDID varchar(20) DEFAULT '';
DEClARE PAYMENT_LINE_cursor CURSOR FOR SELECT PARENT_RECORDID,RECORDID FROM PAYMENT_LINE ORDER BY PARENT_RECORDID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN PAYMENT_LINE_cursor;
get_recordid: LOOP
FETCH PAYMENT_LINE_cursor INTO mPARENT_RECORDID, mRECORDID;
IF done THEN
LEAVE get_recordid;
END IF;
/**********/
IF mPARENT_RECORDID<>mLAST_PARENT_RECORDID THEN
SET mLAST_PARENT_RECORDID=mPARENT_RECORDID;
SET MROW=1;
END IF;
UPDATE PAYMENT_LINE SET ROW_NO=MROW WHERE RECORDID=MRECORDID;
SET MROW=MROW+1;
/**********/
END LOOP get_recordid;
END;
$$
DELIMITER ;
call updatePAYMENT_LINERowNo ();

















2015年8月16日 星期日

java Pass by-value or Pass by-reference


static void doubleNumber (int input1, intClass input2)
{input1=input1*2; input2.number=input2.number*2;}


In general, there are two ways that a computer language can pass an argument to a subroutine. The first way is pass-by-value. This method copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument.

The second way an argument can be passed is pass-by-reference. In this method, a reference to an argument (not the value of the argument) is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine.

As you will see, Java uses both approaches, depending upon what is passed.

In Java, when you pass a primitive / simple type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method.

When you pass an object to a method, the situation changes dramatically. because objects are passed by reference. Keep in mind that when you create a variable of a class type, you are only creating a reference to an object. Thus , when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects are passed to methods by use of call-by--reference. Changes to the object inside the method do affect the object used as an argument.




2015年8月1日 星期六

java local variable vs class variable


source : http://stackoverflow.com/questions/5539652/using-a-class-variable-vs-sending-local-variable-to-functions-methods

local variable to a function/method as a parameter, rather than using a class variable in place of the function/method variable.

The only answer out of the blue, for a any generic case is: it depends on your specific case. Data members, static members and function arguments all serve different purposes. Of course, there are some key tips we can give for what types of signs you should look for choosing one or the other.

Typical cases:

Data member: the value is part of the object's (as in instance of a class) state. You want other calls to methods to reflect this particular state.
Static member: the value has simultaneous, identical meaning to all instances of the class. This is typically used only for constants (even when initialized at runtime, like a singleton) but in some cases, there is need for mutable class state.
Function argument: the value has meaning only for a specific execution of the function/method. This value is subject to change from one invocation to the next.
There are some common symptoms of bad choice.

Consider the following questions:

Do you always pass the same value to a method, no matter where you call it from? Consider making the argument a constant and hiding the argument away. Consider defining overloads: one without argument for the common case and one with argument for flexibility.
Do you need to set a data member (via a setter) every single time you invoke the function? Consider making the value an argument to the function. There's no need to save on the function signature if you need to replace each call with two lines to set the value before hand.
I'm under the impression that you and your co-worker are in a simple misunderstanding of the nature of this parameter. Make sure you clearly understand your co-worker's arguments and make yourself clear. Try to rephrase what it is that you're trying to say.

java-servlet instance variables , method variable

source : http://stackoverflow.com/questions/2183974/difference-between-each-instance-of-servlet-and-each-thread-of-servlet-in-servle

When the Servlet container starts, it:

reads web.xml;
finds the declared Servlets in the classpath; and
loads and instantiates each Servlet only once.
Roughly, like this:

String urlPattern = parseWebXmlAndRetrieveServletUrlPattern();
String servletClass = parseWebXmlAndRetrieveServletClass();
HttpServlet servlet = (HttpServlet) Class.forName(servletClass).newInstance();
servlet.init();
servlets.put(urlPattern, servlet); // Similar to a map interface.
Those Servlets are stored in memory and reused every time the request URL matches the Servlet's associated url-pattern. The servlet container then executes code similar to:

for (Entry<String, HttpServlet> entry : servlets.entrySet()) {
    String urlPattern = entry.getKey();
    HttpServlet servlet = entry.getValue();
    if (request.getRequestURL().matches(urlPattern)) {
        servlet.service(request, response);
        break;
    }
}
The GenericServlet#service() on its turn decides which of the doGet(), doPost(), etc.. to invoke based on HttpServletRequest#getMethod().

You see, the servletcontainer reuses the same servlet instance for every request. In other words: the servlets are shared among every request. That's why it's extremely important to write servlet code the threadsafe manner --which is actually simple: just do not assign request or session scoped data as servlet instance variables, but just as method local variables. E.g.

public class MyServlet extends HttpServlet {

    private Object thisIsNOTThreadSafe;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object thisIsThreadSafe;

        thisIsNOTThreadSafe = request.getParameter("foo"); // BAD!! Shared among all requests!
        thisIsThreadSafe = request.getParameter("foo"); // OK, this is thread safe.
    }
}




2015年7月31日 星期五

10 Most Useful Java Best Practice Quotes for Java Developers

http://viralpatel.net/blogs/most-useful-java-best-practice-quotes-java-developers/

10 Most Useful Java Best Practice Quotes for Java Developers

star-trek-spock-java-best-practice-quotes

Quote 1: Avoid creating unnecessary objects and always prefer to do Lazy Initialization

Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code.
public class Countries {
 
    private List countries;
     
    public List getCountries() {
         
        //initialize only when required
        if(null == countries) {
            countries = new ArrayList();
        }
        return countries;
    }
}

Quote 2: Never make an instance fields of class public

Making a class field public can cause lot of issues in a program. For instance you may have a class called MyCalender. This class contains an array of String weekdays. You may have assume that this array will always contain 7 names of weekdays. But as this array is public, it may be accessed by anyone. Someone by mistake also may change the value and insert a bug!
public class MyCalender {
     
    public String[] weekdays =
        {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};
     
    //some code
     
}
Best approach as many of you already know is to always make the field private and add a getter method to access the elements.
private String[] weekdays =
    {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};
 
public String[] getWeekdays() {
    return weekdays;
}
But writing getter method does not exactly solve our problem. The array is still accessible. Best way to make it unmodifiable is to return a clone of array instead of array itself. Thus the getter method will be changed to.
public String[] getWeekdays() {
    return weekdays.clone();
}

Quote 3: Always try to minimize Mutability of a class

Making a class immutable is to make it unmodifiable. The information the class preserve will stay as it is through out the lifetime of the class. Immutable classes are simple, they are easy to manage. They are thread safe. They makes great building blocks for other objects.
However creating immutable objects can hit performance of an app. So always choose wisely if you want your class to be immutable or not. Always try to make a small class with less fields immutable.
To make a class immutable you can define its all constructors private and then create a public static method
to initialize and object and return it.
public class Employee {
 
    private String firstName;
    private String lastName;
     
    //private default constructor
    private Employee(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
     
    public static Employee valueOf (String firstName, String lastName) {
        return new Employee(firstName, lastName);
    }
}

Quote 4: Try to prefer Interfaces instead of Abstract classes

First you can not inherit multiple classes in Java but you can definitely implements multiple interfaces. Its very easy to change the implementation of an existing class and add implementation of one more interface rather then changing full hierarchy of class.
Again if you are 100% sure what methods an interface will have, then only start coding that interface. As it is very difficult to add a new method in an existing interface without breaking the code that has already implemented it. On contrary a new method can be easily added in Abstract class without breaking existing functionality.

Quote 5: Always try to limit the scope of Local variable

Local variables are great. But sometimes we may insert some bugs due to copy paste of old code. Minimizing the scope of a local variable makes code more readable, less error prone and also improves the maintainability of the code.
Thus, declare a variable only when needed just before its use.
Always initialize a local variable upon its declaration. If not possible at least make the local instance assigned null value.

Quote 6: Try to use standard library instead of writing your own from scratch

Writing code is fun. But “do not reinvent the wheel”. It is very advisable to use an existing standard library which is already tested, debugged and used by others. This not only improves the efficiency of programmer but also reduces chances of adding new bugs in your code. Also using a standard library makes code readable and maintainable.
For instance Google has just released a new library Google Collections that can be used if you want to add advance collection functionality in your code.

Quote 7: Wherever possible try to use Primitive types instead of Wrapper classes

Wrapper classes are great. But at same time they are slow. Primitive types are just values, whereas Wrapper classes are stores information about complete class.
Sometimes a programmer may add bug in the code by using wrapper due to oversight. For example, in below example:
int x = 10;
int y = 10;
 
Integer x1 = new Integer(10);
Integer y1 = new Integer(10);
 
System.out.println(x == y);
System.out.println(x1 == y1);
The first sop will print true whereas the second one will print false. The problem is when comparing two wrapper class objects we cant use == operator. It will compare the reference of object and not its actual value.
Also if you are using a wrapper class object then never forget to initialize it to a default value. As by default all wrapper class objects are initialized to null.
Boolean flag;
 
if(flag == true) {
    System.out.println("Flag is set");
} else {
    System.out.println("Flag is not set");
}
The above code will give a NullPointerException as it tries to box the values before comparing with true and as its null.

Quote 8: Use Strings with utmost care.

Always carefully use Strings in your code. A simple concatenation of strings can reduce performance of program. For example if we concatenate strings using + operator in a for loop then everytime + is used, it creates a new String object. This will affect both memory usage and performance time.
Also whenever you want to instantiate a String object, never use its constructor but always instantiate it directly. For example:
//slow instantiation
String slow = new String("Yet another string object");
 
//fast instantiation
String fast = "Yet another string object";

Quote 9: Always return empty Collections and Arrays instead of null

Whenever your method is returning a collection element or an array, always make sure you return empty array/collection and not null. This will save a lot of if else testing for null elements. For instance in below example we have a getter method that returns employee name. If the name is null it simply return blank string “”.
public String getEmployeeName() {
    return (null==employeeName ? "": employeeName);
}

Quote 10: Defensive copies are savior

Defensive copies are the clone objects created to avoid mutation of an object. For example in below code we have defined a Student class which has a private field birth date that is initialized when the object is constructed.
public class Student {
    private Date birthDate;
     
    public Student(birthDate) {
        this.birthDate = birthDate;
    }
     
    public Date getBirthDate() {
        return this.birthDate;
    }
}
Now we may have some other code that uses the Student object.
public static void main(String []arg) {
 
    Date birthDate = new Date();
    Student student = new Student(birthDate);
 
    birthDate.setYear(2019);
     
    System.out.println(student.getBirthDate());
}
In above code we just created a Student object with some default birthdate. But then we changed the value of year of the birthdate. Thus when we print the birth date, its year was changed to 2019!
To avoid such cases, we can use Defensive copies mechanism. Change the constructor of Student class to following.
public Student(birthDate) {
    this.birthDate = new Date(birthDate);
}
This ensure we have another copy of birthdate that we use in Student class.

Two bonus quotes

Here are two bonus Java best practice quotes for you.

Quote 11: Never let exception come out of finally block

Finally blocks should never have code that throws exception. Always make sure finally clause does not throw exception. If you have some code in finally block that does throw exception, then log the exception properly and never let it come out :)

Quote 12: Never throw “Exception”

Never throw java.lang.Exception directly. It defeats the purpose of using checked Exceptions. Also there is no useful information getting conveyed in caller method.

More Quotes from Java Developers

Do you have a quote that is not included in above list? Well, feel free to add your Java best practice quote using comment below. Write your quote and explain it in 2-3 lines. I will add all those user generated quotes in this section.

Quote #13: Avoid floating point numbers

It is a bad idea to use floating point to try to represent exact quantities like monetary amounts. Using floating point for dollars-and-cents calculations is a recipe for disaster. Floating point numbers are best reserved for values such as measurements, whose values are fundamentally inexact to begin with. For calculations of monetary amounts it is better to use BigDecimal.