Monday, March 28, 2011

Get the top 5th salary of all employees

SELECT SALARY FROM


(SELECT VAL1, RANK() OVER (ORDER BY VAL1 DESC) R from TEMPX)


WHERE R=5;


OR


SELECT DISTINCT (a.sal) FROM EMP A WHERE 5= (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);


Wednesday, January 19, 2011

Unable to install breakpoint due to missing line number attributes

It would stop you add a breakpoint to your code. The detailed error description that comes as a message box says

Unable to install breakpoint in xyz class due to missing line number attributes. modify compiler options to generate line number attributes.

Reason: Absent line number attributes in generated class file.

Solution:

1. Verify the class file generation options in eclipse

Go to windows > preferences > Java > compiler screen.

Make sure that add line number attributes to generated files (used by debugger) check box is checked.

2. In the build.xml, set debug attribute to true in the javac task.


Wednesday, January 12, 2011

How to add struts capability to a existing core java project

With my earlier projects where we had the struts plug in added to the eclipse. all we used to do is right click on the project and -> Add Struts capabilities. Here it doesn't work since the plug in isn't there. So here is the alternate approach.

1. Go to your Eclipse project folder and you should see a file named ".project" open the file.
2. Search for these lines org.eclipse.jdt.core.javanature
3. Change those lines to become
org.eclipse.jdt.core.javanature
org.eclipse.wst.common.project.facet.core.nature
org.eclipse.wst.common.modulecore.ModuleCoreNature
save the changes you have made.

4. Open one more file called “org.eclipse.wst.common.component” under your ".settings" folder inside your Eclipse project folder. If there is no ".settings" folder inside your project or empty one, you can use from other Eclipse Struts Project, just copy it over.

5. Change the some parameters
Pay attention to (you can ignore other, the application_name usually the Eclipse project name) :
the source-path is relative to the location of your .java files.
the deploy-path is relative to the location of your .class files.
the context-root is the application name, usually the war name or the war-extracted folder name on the server.

4. Close your Eclipse project and open it again.
5. Right click on your project and go to the Properties menu.
6. Choose the Project Facets, select Dynamic Web Module and Java by active the checkbox
respectively(for my case i choose version 2.5 for the Dynamic Web Module and 5 for Java). Click OK.

Hope it useful.

Wednesday, May 26, 2010

Split a file

Reqmt : The input file will have a some text, each text delimited by a '$' sign. We need to separate each text to a new file, each new file is appended by'-1', '-2' depending on teh no. of texts it has. If the file has only one text message the original name should remain intact. We need to push the new files into a new path and delete the original ones from the input folder.

public class SplitFile {
public static void main(String[] args) {
/*args = new String[2];
args[0] = "C:/HOMEWARE/reports/swift/"; // input file path
args[1] = "C:/HOMEWARE/reports/extract/"; // output file path*/
try {
File directory = new File(args[0]);
/*Get the list of files in the swift directory*/
String filename[] = directory.list();
String listFilenames=null;
String inputFileName;
String outputFileName;
StringBuffer contents;
/*Iterate the directory for each SWIFT file*/
for (int i = 0; i < filename.length; i++) {
listFilenames = filename[i];
inputFileName=args[0]+listFilenames;
outputFileName=args[1]+listFilenames;
String line = null;
contents=new StringBuffer();
/*Return if the file is not available*/
File is = new File(inputFileName);
if(!is.exists()){
System.err.println("File " + inputFileName+ " not present to begin with!");
break;
}
BufferedReader input = new BufferedReader(new FileReader(inputFileName));
/*Get the contents of the file*/
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
input.close();
/*Separate each SWIFT message based on the delimiter '$'*/
int count=0;
if(null != contents && contents.length() >0) {
FileOutputStream fop = null;
String str= null;
StringTokenizer token = new StringTokenizer(contents.toString(),"$");
while(token.hasMoreTokens()){
str= token.nextToken().toString();
if(!("\r\n".equals(str))){
count++;
fop = new FileOutputStream(outputFileName + "-"+ count);
fop.write(str.getBytes());
}
}
fop.flush();
fop.close();
}
/*If the file contains only one swift messasge, rename the file to its original name*/
if(count == 1){
File renameFile = new File(outputFileName + "-"+ count);
renameFile.renameTo(new File(outputFileName));
}
/*Delete the original file*/
System.out.println("inputFileName :"+inputFileName);
if(is.delete()){
System.out.println("** File " + inputFileName+ " deleted **");
}else{
System.err.println("Failed to delete " + inputFileName);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not present to begin with!");
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Request not completed due to technical issues. Please contact IT team");
}
}
}

Monday, March 22, 2010

java.lang.UnsupportedClassVersionError (Unsupported major.minor version 49.0)

It is caused when the java compiler is not compatible with the code.
Goto Project Proprties -> java Compiler and check your jdk version.
Should solve the problem

Tuesday, January 26, 2010

javax.faces.FacesException: java.lang.ClassNotFoundException: [Ljava.lang.String

I got this error soon after I changed my jdk version to 1.6.
Turned out - since I'm using jdk1.6 unlike 1.5 it will not attempt to load class (java.lang.String) by name by default, hence the exception.
is easily solvable by locating JVM runtime in Eclipse's preferences.
(Window->Preferences->Installed JREs)

now add -Dsun.lang.ClassLoader.allowArraySyntax=true to your VM Arguments.The error shouldn't persist anymore.

Monday, August 17, 2009

Eclipse build error : Cannot find the class file for java.lang.Object.

The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project

Seems the default JRE is not being set properly to the project that is showing this error.
All you need to do is check the JRE System Library.

Go to the project ->properties->java build path->libraries

Add Library- JRE SYSTEM LIBRARY ->Next
and now choose from the installed jre's

Alternatively you could also add the following to your classpath file :

path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.5.0_15"/>

That should solve the problem.

REFACTORING

 What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...