
Name: Pourya
Posts by DarthPourya:
Xubuntu – Time Synchronization
August 21st, 2010In xubuntu (XFCE) you can run the following command to synchronize your system clock with time servers:
sudo ntpdate pool.ntp.org
After running this command you would get a response similar to the following:
21 Aug 04:08:20 ntpdate[4824]: adjust time server 209.167.68.100 offset 0.017300 sec
Struts2 Iterating through a List of List
July 22nd, 2010This is a simple example demonstrating how to use the iterator tag in struts2 to iterate through a list of objects which contains another list of objects.
Assume that we have a class called City which contains a list of phone numbers:
public class City {
private String name;
private List<PhoneNumber> phoneNumbers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}
Plain-text encryption/decryption in Java
July 19th, 2010There could be cases in which you are planning to keep some sensitive data in your database but you do not wish to have this information available to anyone (not even the DBA should be able to see the content) except the specific user. To do this you can encrypt the sensitive plan-text provided by the user. Save the encrypted message in the database and decrypt it anytime the user would like to view/edit the data. Doing this is quite easy with java. You would just need to add a few jar files to your project and a few lines of code to encrypt and decrypt the data.
The libraries you need to download are available at: http://www.jasypt.org/
The following sample code encrypts and decrypts a String. Please not that I have created an object of BasicTextEncryptor twice the reason for that is to show that you would need to set the correct password used in the encryption process to decrypt the plain-text message.
import org.jasypt.util.text.BasicTextEncryptor;
public class Secret {
public static void main(String[] args) {
String userPassword="PassWord";
String message = "Some text which is to be encrypted by the user password. Later it is decrypted to the original plain-text using the same password.";
//Encrypt
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(userPassword);
String myEncryptedText = textEncryptor.encrypt(message);
System.out.println("Encrypted text:\n"+myEncryptedText);
//Decrypt
BasicTextEncryptor textDecryptor = new BasicTextEncryptor();
textDecryptor.setPassword(userPassword);
String plainText = textDecryptor.decrypt(myEncryptedText);
System.out.println("Decrypted text:\n"+plainText);
}
}
Inserting/Update a long String into a CLOB in Oracle database
May 26th, 2010Inserting a long String into a CLOB field in Oracle database using SQL queries is quite simple, but at the same time can be confusing. Mainly because personally couldn’t find good tutorials on the web when I was trying to do this. Here is a simple way to do it:
To insert a ClOB field you need to have a good query browser. The reason is that I will be using variables to do this, I was not able to do this using DbVisualizer but it works perfectly fine in TOAD.
Here is how to do it:
-- First: you need to declare a variable to hold the long CLOB value. declare clobVariable varchar2(32767) :='YOUR LONG CLOB VALUE GOES HERE'; begin -- ALL the queries you need to run as to be inside the begin tag. -- As you can see I'm using the variable 'clobVariable' in the insert statement to insert into the table. -- You can use this variable in any combination of SQL statements. insert into sometbale(id,clob_column) values(1,clobVariable); end; /
XFCE – XUBUNTU
May 26th, 2010First time I installed xfce on my laptop I was surprised with how light and stable it was. I have been using XFCE for more than 2 years now I would have to say that its great. I would strongly recommend it specially if you don’t have a really powerful machine like me. To customize it the way you like its obviously a little different from Gnome but once you can used to it you would see how easy it is. Here is a screen-shot of my desktop:

Ubuntu change computer name
April 17th, 2010If you would like to change your computer name in Ubuntu Linux you would need to modify two files:
First you would need to change hostname file:
sudo vi /etc/hostname
This file contains your existing computer name. You can change it to anything you like.
The next step is to modify the hosts file:
sudo vi /etc/hosts
Inside this file look for the entry starting with:
127.0.1.1
You would need to change the name in this line to whatever you change to in the first file name.
If you don’t do the second step your computer name would still be changed but you would get the error message “unable to resolve host” everything you login to your account.
SQL insert row if doesn’t exist
April 16th, 2010There are cases in which you like to insert rows into the database only if it doesn’t exist. There are databases that allow you to insert conditions into the SQL to test the number of rows in the table and insert the new row only of there are no rows currently in the table. But unfortunately you cant do that with many other databases.
Here is a simple way to use regular SQL notations to insert rows into the database table only if it doesn’t exit.
lets assume you have a table called users with the following schema:
| name(varchar) |
| family(varchar) |
| email(varchar) |
lets assume we are interested in inserting a row with the following values only if it doesn’t exist in the database table:
name: Pourya
family: Shahroudi
email: something@something.com
INSERT INTO USERS(name,family,email)
SELECT 'Pourya', 'Shahroudi','something@something.com' FROM USERS
WHERE NOT EXISTS(
SELECT name,family,emial FROM USERS
WHERE name='Pourya'
AND family='Shahroudi' and email='something@something.com'
) and ROWNUM = 1;
As you can see this query does a SELECT on the database table with the values we want to insert if this row doesn’t exist then they are inserted into the database table.
NOTE: “ROWNUM = 1” is extremely important in this query if you don’t have this included the query will try to insert as many records as you currently have in the table into the table.
Removing ^M character GVIM windows
April 14th, 2010‘^M’ is end of line character in Linux, when you edit files created in Linux in other environments you might see the ‘^M’ character.
To remove ^M character in GVIM windows you should do:
:%s/^M//g
‘^M’ is CONTROL-V. Once you enter it will search your document and it removes all instances of ^M character from the file.
VI tips
April 12th, 2010Here are some beginners tips to VI editor:
To change the color scheme in vi just do:
colorscheme torte
which changes your color shema to torte. To see the available color schemes you could use
To change the font in vi editor you would have to do:
set guifont=Lucida\ Console:h10
Please note that ‘\’ is being used before every space character in the font name.
setting the line numbers:
set number
To highlight the search result in the current doument:
set hlsearch
To have this change made every time you start vi you would need to add it anywhere in vimrc file. In Linux its under /etc/vim/ and in windows its inside the installation folder in “program files”
PySDM – Storage Device Manager
March 28th, 2010
To auto-mount storage devices in Linux normally you would need to edit /etc/fstab file, which sometimes can be quite difficult specially if you are a Linux newbie. To make things easier you can look into using a software called PySDM which does exactly what you need to do with a nice user interface. Using this tool you can easily manage your mount points.
to Install PySDM:
sudo apt-get install pysdm