Wednesday, January 2, 2013


How to connect to SQL Database-JDBC connection

1. How to connect to SQL Database

Steps to connect SQL Database (using JDBC Driver)
1. Import the SQL package.
2. Create an Connection Object and initialize to NULL.
3. Create an String variable for URL and store the URL.
4. Create an String variable for DATABASE and store Database Name.
5. Create an String variable for DRIVER and store the JDBC driver.
6. Create an String variable for USERNAME and store UserName.
7. Create an String variable for PASSWORD and store Password.
8. Put an try catch Block, inside try add the following logic.
    1. Create an instance of DRIVER
    2. Pass the URL, DATABASE, USERNAME, PASSWORD to getConnection()
       method of DriverManager, and pass it to Connection Object.
   3. Create an STATEMENT Object which acts as a reference  to Create SQL   
       Statement for the Connection.
   4. Through the STATEMENT Object Execute any Query, and pass it to Object
       of RESULT SET.
   5. Loop through RESULT SET Object and can print the data.
   6. Close the Connection through Connection object.

-----------------------------------------------------------------------------------------------------------------------------
import java.sql.*
public class Connect_DB{

public static void main(String[] args){
      
     Connection conn = NULL;
     String url= "jdbc:mysql://localhost:3306";
     String dbName = "MidMac";
     String driver = "com.mysql.jdbc.Driver";
     String username = "root";
     String password = "tiger";
     
     try{
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbNameusername, password);
            Statement Smt = conn.createStatement();
            ResultSet Rs = Smt.executeQuery("SELECT * FROM EMPLOYEE");
            
            while(Rs .next()){
              System.out.println(Rs.getString(1));
            }//end while
      }catch(Exception e){
            conn.close();
      }
     
}

}

-----------------------------------------------------------------------------------------------------------------------------

Tuesday, January 1, 2013


How to ZIP files in SELENIUM with an Example

1. How to ZIP a file in SELENIUM

Steps to ZIP(Compare with Code and understand)
1. Create an File object for the SOURCE FOLDER, in which files are present.
2. Create another File object for the destination folder, for ZIP file.
3. Create a ZipOutputStream Object.
4. Create a BufferedInputStream Object.
5. Create a Byte Array Object of size 1000.
6. Create a String Array, and collect all files from SOURCE FOLDER as   
    LIST  using File Object for source folder.
7. Loop through String Array and for each element perform below action.
8. INSIDE FOR LOOP
   1. Get the SOURCE FOLDER path and append each file to the path
       with default size, and put it in BufferedInputStream Object.
   2. Through ZipOutputStream Object, make an ZIP Entry for each File.
   3. Through While Loop read the data bit by bit untill 1000 bytes from
       BufferedInputStream Object and write to ZipOutputStream Object.
      9. INSIDE WHILE LOOP
       1. Read the BufferedInputStream Object.
       2. Write to ZipOutputStream Object.

4. After writing make a Close Entry using ZipOutputStream Object.
5. Flush the data using ZipOutputStream Object.
6. Close the ZipOutputStream Object.
-----------------------------------------------------------------------
try
  {
    File inFolder=new File("D:\\sel\\framework\\tests");
    File outFolder=new File("D:\\sel\\framework\\results\\results.zip");

    ZipOutputStream out = new ZipOutputStream(new  BufferedOutputStream( 
                                                  new FileOutputStream(outFolder)));
   BufferedInputStream in = null;
   byte[] data  = new byte[1000];
   String files[] = inFolder.list();
  for (int i=0; i<files.length; i++)
  {
     in = new BufferedInputStream(new FileInputStream
                                               (inFolder.getPath() + "/" + files[i]), 1000);  
     out.putNextEntry(new ZipEntry(files[i])); 
     int count;
              while((count = in.read(data,0,1000)) != -1)
               {
                    out.write(data, 0, count);
               }
              out.closeEntry();
  }
 out.flush();
 out.close();
  }
 catch(Exception e)
 {
  e.printStackTrace();
  }

-----------------------------------------------------------------------

Total Pageviews