Monday, November 28, 2011

Inserting CVS Data to DB Using Db command


Command:
  db2 import from C:\test.csv of del insert into address    (addressId,status,nickname,firstname,lastname,email1)

Imp Points:
  • According to csv file the corresponding coloumns have to be specified for the table.
  • Have to be login to DB with Administrative Privilages.

Thursday, November 3, 2011

Creating Datasource in WAS6.0 for MySql -- RAD7.0

This Post about Creating Mtsql Datasource in RAD 7.0 for WAS 6.0 / WAS 6.1
By default WAS 6.0 does not provide datasource for MySql. We have to create User defined JDBC provider for the My Sql.

The detailed Steps for the Creation.

Step1:
In Admin console select Data Source under Resources.




 


















Step : 3
On the new screen, select User-Defined in Database type field (because WAS doesn’t have MySQL pre-defined), com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource in Implementation Class Name field and MySQL JDBC Provider in Name field.



Step 4: Specify the Jar file path for MySql JDBC connection.


Step 5: Click Finish button.



Step 7: After finishing Again click on the "Test"(what we created just now) Datasource on list of data sources, and Click on the right side JASS-J2C Authentication.


Step 8: Fill the database authentication details like username and password.

Step 9: Just Last Step. We have to fill in some custom properties. like databaseName, URL (of the Database).



Step 10 : in Custom properties look for the URL and enter the Database Access url.







Step 11: now select the authentication we creatted now.

Step 13; now test the connection. its successful.


Retriving Image From Database


// column is column index in ResultSet containing the image,
// we assume that the type is LONGVARBINARY
Image myImage = null;
InputStream stream = rset.getBinaryStream(column);
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
int a1 = stream.read();
while (a1 >= 0) {
output.write((char)a1);
a1 = stream.read();
}
myImage =
Toolkit.getDefaultToolkit().createImage(output.toByteArray());
output.close();
}
catch(Exception e){}

Wednesday, November 2, 2011

Map Sorting Using Comparator

This Post about sorting TreeMap.

TreeMap Actually sorts values by key. Inorder to sort Map by values we have to use comparator. The below program illustrates this.


public class ComparatorClassDemo {
 
    public static void main(String[] args) {
     
        HashMap<Integer,String> map = new HashMap<Integer,String>();
        ValueComparator bvc =  new ValueComparator(map);
        TreeMap<Integer,String> sorted_map = new TreeMap(bvc);
     
     
        map.put(1,"Arogya");
        map.put(2,"wer");
        map.put(3,"dfg");
        map.put(6,"io");
        map.put(5,"lkaldfk");
        map.put(4,"bkjaskj");
       
System.out.println("Before usage of comparator"+map);
     
     
        sorted_map.putAll(map);
     
        System.out.println("results");
        for (int key : sorted_map.keySet()) {
            System.out.println("key/value: " + key + "/"+sorted_map.get(key));
        }
    }
 
}
class ValueComparator implements Comparator {
    Map base;
  public ValueComparator(Map base) {
      this.base = base;
  }

  public int compare(Object a, Object b) {
    String name=(String)base.get(a);
    String name1=(String)base.get(b);
    return name.compareTo(name1);
  }
}



OutPut:





{2=wer, 4=bkjaskj, 6=io, 1=Arogya, 3=dfg, 5=lkaldfk}
results
key/value: 1/Arogya
key/value: 4/bkjaskj
key/value: 3/dfg
key/value: 6/io
key/value: 5/lkaldfk
key/value: 2/wer