Pages

Monday, July 26, 2010

How to change any date format to DATE datatype in MySQL (Insert or Update)

 ----

Use this function:  STR_TO_DATE('date', 'format')

This accepts two parameters: date and format where date is the input raw date you want to change and format is the corresponding format relative to date that you want to change.

For example:
INSERT INTO table(date_field) VALUES(STR_TO_DATE('December 8, 2010','%M %d,%Y'));

Note: table is the name of the table in database and date_field is the name of field in DATE datatype.

The statement above shows 2 parameters date as 'December 8, 2010' and format '%M %d, %Y'. The format is the exact format of the date, so that the mysql knows from what format will it convert the date  to DATE datatype format which is '%y-%m-%d'.

Other valid examples are:
INSERT INTO table(date_field) VALUES(STR_TO_DATE('12-31-2004', '%m-%d-%Y'));
INSERT INTO table(date_field) VALUES(STR_TO_DATE('12/31/2004', '%m/%d/%Y'));
UPDATE table SET date_field =  STR_TO_DATE('Dec 31, 2004', '%b %d, %Y');

----

Thursday, July 15, 2010

How to disable edit cell in JTable (especially in NetBeans)

Put this in your code:

table = new javax.swing.JTable(){
    public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   //Disallow the editing of any cell
    }
};

where table is the name of your JTable.

For NetBeans users:
1. select the table GUI, right click, select Customize Code
2. Your code should look like this:

How to Select All textfield/ password field Java

--start--
Sample code:

     int i = textfield.getText().length();
     textfield.setSelectionStart(0);
     textfield.setSelectionEnd(i);


Put this in the action or wherever you want it. Replace textfield variable to the name of your textfield. I'm not sure but this might work for JTextArea and JTextPane too.



--end---