Back To Normal

Subscribe To Our E-Mail Newsletter

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, September 26, 2012

Connecting Excel as Database in java

User can connect excel as database by using excel drivers and perform insert , read operations.
import java.io.*;
import java.sql.*;

public class ExcelReadTest{

public static void main(String[] args){

Connection connection = null;

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection( "jdbc:odbc:exceltest" );

Statement st = con.createStatement();

ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );

ResultSetMetaData rsmd = rs.getMetaData();

int numberOfColumns = rsmd.getColumnCount();

while (rs.next()) {

for (int i = 1; i <= numberOfColumns; i++) {

if (i > 1) System.out.print(", ");

String columnValue = rs.getString(i);

System.out.print(columnValue);

}

System.out.println("");

}
st.close();

con.close();

} catch(Exception ex) {

System.err.print("Exception: ");

System.err.println(ex.getMessage());

}

}

}
To insert data :
"insert into [Sheet1$] (ColumnName1,ColumnName2) values ('"+sValue1+"','"sValue2"')"
Read More


Tuesday, September 25, 2012

How to read Excel data by using POI jars

Public List ReadExcel2010(String sFilename)


{

List SheetData = new ArrayList();

InputStream fs = new FileInputStream(filename);

XSSFWorkbook wb = new XSSFWorkbook(fs);

XSSFSheet sheet = wb.getSheetAt(0);

try

{

Header header = sheet.getHeader();
int rowsCount = sheet.getLastRowNum();

System.out.println("Total Number of Rows: " + (rowsCount + 1));

for (int i = 0; i <= rowsCount; i++) {

List data = new ArrayList();

Row row = sheet.getRow(i);

int colCounts = row.getLastCellNum();

System.out.println("Total Number of Cols: " + colCounts);

for (int j = 0; j < colCounts; j++) {

Cell cell = row.getCell(j);

data.add(cell );

System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());

}

Sheetdata.add(data);

}
} catch (Exception ex) {

java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);

} finally {

try {

fs.close();

} catch (IOException ex) {

java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);

}

}
return Sheetdata;

}
Read More


Tuesday, August 21, 2012

Export - Import in Internet Explorer using Selenium Web Driver


Handling Export and Import by using Keys.
WebElement link = driver.findElement(By.xpath("myxpath"));
ExportAndSaveFileInIE(link, Path);

public static void ExportSaveFileInIE(WebElement element,String sFilepath) throws

InterruptedException{
    try {

      Robot robot = new Robot();
       //Focus the Element
       element.sendKeys("");

      //Press Enter Key to populate Window          
      robot.keyPress(KeyEvent.VK_ENTER);
      robot.keyRelease(KeyEvent.VK_ENTER);
   
      //Wait For Dialog        
      Thread.sleep(2000);

     //press s key to save          
      robot.keyPress(KeyEvent.VK_S);
      robot.keyRelease(KeyEvent.VK_S);
      Thread.sleep(2000);
     //press enter to save the file with specified name and in location
     sFilepath = sFilepath.ToUpperCase()
     for (int i=0;i<=sFilepath .length();i++)
     {
      Char ch = sFilepath.ChartAt(i);
      int iascvalue = ch;
      switch(ch)
{
      Case ":"
       robot.keyPress(KeyEvent.VK_SHIFT);
       robot.keyPress(KeyEvent.VK_COLON);
       robot.keyRelease(KeyEvent.VK_COLON);
       robot.keyRelease(KeyEvent.VK_SHIFT);
      Case "\"
    ....
      default
        robot.keyPress(iascvalue );
      robot.keyRelease(iascvalue );
}

     }
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);

 } catch (AWTException e) {

            e.printStackTrace();
  }

Read More


Monday, August 13, 2012

Java : Test Automation Questions and Answers

1. String Immutable
The absolutely most important reason that String is immutable is that it is used by the
class loading mechanism, and thus have profound and fundamental security aspects.

Had String been mutable, a request to load "java.io.Writer" could have been changed to load
"vijay.co.DiskErasingWriter"
Details: http://javarevisited.blogspot.in/2010/10/why-string-is-immutable-in-java.html

2. Two main mehod in single class

Ans : Its possible with different retun type and parameters (method overloading )
3. String Memory allocation vs StringBuffer vs String Builders

String sname ='vijay';

sname ='ragavan';
Details: http://javarevisited.blogspot.in/2011/07/string-vs-stringbuffer-vs-stringbuilder.html

5. Usage of Interface
Often interfaces are touted as an alternative to multiple class inheritance. While
interfaces may solve similar problems, interface and multiple class inheritance are quite
different animals, in particular:
A class inherits only constants from an interface.
A class cannot inherit method implementations from an interface.
The interface hierarchy is independent of the class hierarchy. Classes that implement the
same interface may or may not be related through the class hierarchy. This is not true for
multiple inheritance.

Usage of Interfaces :
You use an interface to define a protocol of behavior that can be implemented by any class
anywhere in the class hierarchy. Interfaces are useful for the following:
Capturing similarities between unrelated classes without artificially forcing a class relationship
Declaring methods that one or more classes are expected to implement
Revealing an object's programming interface without revealing its class. (Objects such as these are called anonymous objects and can be useful when shipping a package of classes to other developers.)
6. Garbage collector in java and usage.
Java takes a different approach; it handles deallocation for you automatically. The technique that accomplishes this is called garbage collection. It works like this: when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
The finalize( ) Method
Sometimes an object will need to perform some action when it is destroyed. For example, if
an object is holding some non-Java resource such as a file handle or window character font,
then you might want to make sure these resources are freed before an object is destroyed

Keyword: System.gc();
Read More


560 Free Online Courses

Top 200 universities launched 500 free online courses.  Please find the list here .