Monday, April 26, 2010

How to get distinct values in java from a column of ResultSet?


If you want get all the distinct values from any column of your database table using java. You have to take all the columns in the ResultSet object and then pass that ResultSet object to these methods.You have to take data into Resultset Object like this.
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("Select * from Table_Name");
You must be knowing the rest code. You have to encapsulate the following code into a class. After that, from Main Class you have to call these methods passing the Resultset Object rs.


int counter=0;
int count1=0;
int deptno[],A[],B[];
float Avg[];
////This method will find the number of rows in the resultset
void countRow(ResultSet rs) throws Exception
{
while(rs.next()){
counter++;
}
}
////This will put distinct value inside an array deptno[count]
void distinctValue(ResultSet rs) throws Exception
{
deptno=new int[counter];
A=new int[counter];
B=new int[counter];
int count=0,i=-1,x=0,y;
boolean bool=true;
rs.beforeFirst();
while(rs.next())
{
i++;
deptno[i]=rs.getInt(8);//--Here you have to give the column number
A[i]=deptno[i];
}
while(bool)
{
y=-1;
deptno[count]=A[x];
for(int j=0;j
{
if(deptno[count] != A[j])
{
y++;
B[y]=A[j];
}
}
counter=y+1;
if(y==-1)
{
count1=count;
break;
}
for(int j=0;j<=y;j++)
{
A[j]=B[j];
if(j==0)
{
count++;
deptno[count]=A[j];
}
}
}
}


The above code will give you distinct values of any numbered column into a array deptno[count]. And the size of array will be Zero-count1. Also you have to define the ResultSet as TYPE_SCROLL_SENSITIVE.
deptno[i]=rs.getInt(8); Here you have to give your desired column number. In my case its 8'th column.




Delicious add to del.icio.us saved by 0 users

How to remove olhrwef.exe?


Are you worried about olhrwef.exe? Olhrwef.exe is Trojan/Backdoor. This virus spreads through pendrives. Here is the procedure to remove it.

Step 1: Start->Run->msconfig.

Go to startup and unmark the olhrwef.exe from the Startup Items. After that Click Apply and Ok. But don’t restart your computer.

Step 2: Start->Run->regedit

Click on Computer(in Registry Editor),after that click ctrl+f and type “cdoosoft”.

A folder of this name will be visible. Delete that folder.

Step 3:Go into System32 folder(C:\Windows\System32). Check for two files named olhrwef.exe and nmdfgds0.dll. Delete these two files by changing the attribute.

Like C:\>attrib –r –s –h nmdfgds0.dll

C:\>del nmdfgds0.dll

You have to delete all file like this.

Step 4: Delete autorun.inf from each parent directory (like C,D,E,F...)

Step 5: Delete ur0.com file from each parent directory (like C,D,E,F...)

Step 6: Go to search(Start->search) and type ur0.com and search.Wait. In advance search tick mark for hidden files/folder search also.

You will get 3-4 files named as ur0%.pf. Delete all those files also.

If you have completed all these steps, then you are safe now. But it’s not as easy as I described in 6 steps.Because when you will attempt to delete these file, you will get some message like 'file is being used by some process and can not be deleted'.I did all these things from some other operating system. I am having windows as well as Ubuntu. So I performed first two steps through Windows and rest through Ubuntu.

Now it’s your turn to experience.





Delicious add to del.icio.us saved by 0 users

Sunday, April 25, 2010

Tomcat Server Configuration to run Servlet


When we install Tomcat Server for the first time then we have to configure it for its proper functioning. You must have written a servlet code and compiled it(may be). Now you want to run it, and see some wonder in your browser page and pat your back. Isn’t it? Ok.

Now you must have installed your tomcat with its default setting. In my case it is installed in

C:\Program Files\Apache Software Foundation\Tomcat 6.0….

Ok. Now you have to set the path for javax.servlet.*; and javax.servlet.http.*; package. Isn’t it? Because when ever you will be compiling you Java code. Your Java compiler will be giving some error that javax.servlet package not available.

So you have to search a directory named lib inside your installed Tomcat folder. In my case it’s

C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib

Now in that lib folder there will be a jar file named servlet-api.jar. You have to set the classpath for that.Open command prompt (holding windows key, press R and type CMD). Then type

classpath=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;

(make appropriate changes according to your tomcat directory and lib directory path).

But this not permanent. You have to set this path every time you start your computer. If you want to make it permanent, you have to set environment variable. The steps are as follows…

RightClick on my computer->Properties->Advance->Environment Variables--

->System Variables->New

Variable name=Classpath

Variable Value= C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;

(make appropriate changes according to your Tomcat directory and lib directory path).

Now you are done with the class path. Now go to the bin directory of your Tomcat Server. In my case it’s

C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin

And double click on Tomcat.exe(service runner). Your tomcat will start and a command prompt will be visible in which some characters and strings will be flowing. The last string will be some milliseconds.

Now just recompile your .java code. If everything went well. You will successfully compile it and a .class file will be generated. Now you have to follow some directory hierarchy to place this .class file. The hierarchy must be like this.

You should search for a directory named webapps (in my case C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps).

In that folder you have to create a subfolder of any name(like xyz). Inside xyz there must be a directory named in the same casing as WEB-INF, inside WEB-INF create a file(names web.xml) and a folder(named classes), inside classes you will place your .class file. You will write class name and url-pattern in the web.xml file.

In some cases some other program have occupied port 8080, and for this reason also your tomcat will not work properly, you will get this message in the command prompt which came in vision when you double clicked Tomcat.exe service runner. Here is the process to change the port number. Search for the conf folder and edit server.xml(change the port number “Connector port=8080”). You can change/see your admin password in the same folder in a file named tomcat-users.xml.

Now you are done, say all is well and open a browser and type

http://127.0.0.1/xyz/YourServletUrl or http://localhost/xyz/YourServletUrl

where xyz in the folder and YourServletUrl is the written in web.xml

I think if you will go through all these steps according to your requirement, your tomcat will run properly and your servlet will give the expected output.





Delicious add to del.icio.us saved by 0 users

 

Never feel bad if u loose some thing or do not get expected because "There is always a better option"