Thursday, January 20, 2005

Threads Learning

When we extend thread we can start the thread from outside where as if we implement runnable interface we need to start the thread inside the constructor of the implementing class.

Saturday, January 15, 2005

Working with java.io

This program displays all the subdirecotries and files in a given directory.



import java.io.*;

/*
*Program to list all the directories and files
under the given directory
*/


public class DirFiles{


public static void main(String[] args) {
File fil = new File(args[0]); //Input directory
from the command prompt

DirLevel dirlvl = new DirLevel(fil , 0);
checkIfDirectory(dirlvl );
}

//Recursive Method to get all the
files and directories under a given directory
static void checkIfDirectory(DirLevel fname){


for(int k=0;k<fname.level;k++){
System.out.print(" ");
}


if(fname.filename.isDirectory()) {
File filarray[] = fname.filename.listFiles();

System.out.println(fname.filename.getName());

for(int i=0;i<filarray.length;i++){


DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );

}
}
else {

System.out.println(fname.filename.getName());
}

}


}


/*
*This calss is defined to determine the level
from the parent directory
*/


class DirLevel {
File filename ;
int level;
DirLevel(File flname , int lvl){
filename = flname;
level = lvl;

}
}



************************************************************************************
************************************************************************************
************************************************************************************




import java.io.*;

/*
*Program to list all the directories and files under the given
directory. This also gives the number of lines present in each file.
*/


public class DirFilesCount {


public static void main(String[] args) {
File fil = new File(args[0]);
DirLevel dirlvl = new DirLevel(fil , 0);


checkIfDirectory(dirlvl );
}

//Recursive Method to get all
the directories and files and their line count, under a given directory

static void checkIfDirectory(DirLevel fname){

for(int k=0;k<fname.level;k++){

System.out.print(" ");
}


if(fname.filename.isDirectory()) {

File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());

for(int i=0;i<filarray.length;i++){

DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}

else {


BufferedReader buffreader=null ;

//Try catch block to count
the no number of line of all the files and print their names as well as thier count.
try{

buffreader = new BufferedReader(new FileReader(fname.filename));

int linecount=0;
String line;

do {
line = buffreader.readLine();

if(line!=null) {

linecount++;
}

}while(line!=null);

System.out.println(fname.filename.getName()+" ---------->" + linecount+" lines");


}catch (IOException e){
System.out.println("the follwing error occured"+e);
}
finally{
if(buffreader!= null){

try{
buffreader.close();
}catch (IOException e){
System.out.println("Error while closing the file "+e);
}
}

}
}
}
}



************************************************************************************
************************************************************************************
************************************************************************************



import java.io.*;

/*
*Program to list all the directories and files under the given
directory. This also gives the number of lines present in each file.
*/


public class DirFilesCount {


public static void main(String[] args) {
File fil = new File(args[0]);
DirLevel dirlvl = new DirLevel(fil , 0);


checkIfDirectory(dirlvl );
}

//Recursive Method to get all the directories and files and their line count,
under a given directory

static void checkIfDirectory(DirLevel fname){

for(int k=0;k<fname.level;k++){

System.out.print(" ");
}

if(fname.filename.isDirectory()) {

File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());

for(int i=0;i<filarray.length;i++){

DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}

else {


BufferedReader buffreader=null ;

//Try catch block to count
the no number of line of all the files and print their names as well as thier count.
try{

buffreader = new BufferedReader(new FileReader(fname.filename));

int linecount=0;
String line;

do {
line = buffreader.readLine();

if(line!=null) {

linecount++;
}

}while(line!=null);

System.out.println(fname.filename.getName()+" ---------->" + linecount+" lines");


}catch (IOException e){
System.out.println("the follwing error occured"+e);
}
finally{
if(buffreader!= null){

try{
buffreader.close();
}catch (IOException e){
System.out.println("Error while closing the file "+e);
}
}

}
}
}
}


Saturday, January 08, 2005

Query to find the day ina date

The following query can be used to get the day of any date

SELECT TO_CHAR(TO_DATE('01-JAN-05','DD-MON-RR'),'Dy') as day FROM dual;


Friday, January 07, 2005

INSTR function

The instr function can be used to find if the given string is present in a particular column or not.
Example for this is given below.

SQL> SELECT ename
FROM emp
WHERE instr(ename , 'LL')>0;

ENAME
----------
ALLEN
MILLER

Using INSTR will be much faster than using like in these kind of scenarios.


variable refcursor

Variabel can be declared as reference cursor in sql and record sets can be stored into it. A simle exapmple is shown below.


SQL> variable test_cur refcursor
SQL> declare
2 v_sql varchar2(3000);
3 begin
4 v_sql:='select * from client_pricing order by 2,1';
5 open :test_cur for v_sql;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL> print test_cur

FUNCTION_ID CLIENT_ID PRICING
----------- ---------- ----------
1 10 44
1 10 22
1 20 33
2 20 33
1 30 33
2 30 55
1 40 22
2 40 77
1 50 44
2 50 99

10 rows selected.

SQL> spool off

Tuesday, January 04, 2005

Restrictions on LONG Datatype

The following are the restrictions on the LONG datatype:
1. There can be only one column of datatype LONG in a table.
2. You can not use the LONG datatype column for GROUP BY, ORDER BY, WHERE or CONNECT BY clauses.
3. Character functions can not be applied to LONG columns.

Note:PL/SQL LONG varoabel is different from LONG datatype for columns.

Monday, January 03, 2005

Auto Trace

To set autotrace option run the file @D:\Oracle\Ora81\RDBMS\ADMIN\UTLXPLAN.SQL.

This sql creates the required table for setting up auto trace.

update scott.up_ml set cl3=null
where rowid in ( select rid
from ( select b.rowid rid,
row_number() over
(partition by cl1,cl2,cl3 ORDER BY cl1,cl2,cl3) rn
from scott.up_ml b
)
where rn <> 1 )


update scott.up_ml a set cl3 = null where rowid > (select min(rowid) from scott.up_ml b where a.cl1=b.cl1 and b.cl2=b.cl2);


Auto trace outputs

Execution Plan
----------------------------------------------------------
0 UPDATE STATEMENT Optimizer=CHOOSE
1 0 UPDATE OF 'UP_ML'
2 1 NESTED LOOPS
3 2 VIEW OF 'VW_NSO_1'
4 3 SORT (UNIQUE)
5 4 VIEW
6 5 WINDOW (SORT)
7 6 TABLE ACCESS (FULL) OF 'UP_ML'
8 2 TABLE ACCESS (BY USER ROWID) OF 'UP_ML'




Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
8 consistent gets
0 physical reads
860 redo size
619 bytes sent via SQL*Net to client
689 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
3 rows processed



*************************************************************************************

Execution Plan
----------------------------------------------------------
0 UPDATE STATEMENT Optimizer=CHOOSE
1 0 UPDATE OF 'UP_ML'
2 1 FILTER
3 2 TABLE ACCESS (FULL) OF 'UP_ML'
4 2 SORT (AGGREGATE)
5 4 TABLE ACCESS (FULL) OF 'UP_ML'




Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
12 consistent gets
0 physical reads
808 redo size
621 bytes sent via SQL*Net to client
618 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
3 rows processed