Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Friday, May 31, 2013

Using java read Excel(.xls) file

In this example demonstrate how to read Excel(.xls) file in java using apache poi jar file is need for dependency.


  // Import class   
  import java.io.File;   
  import java.io.IOException;  
  import jxl.Cell; //class from apache POI  
  import jxl.CellType;  
  import jxl.Sheet;  
  import jxl.Workbook;  
  import jxl.read.biff.BiffException;  
  public class Test {  
  private String inputFile;  
  public void setInputFile(String inputFile) {  
   this.inputFile = inputFile;  
  }  
  public void read() throws IOException {  
   File inputWorkbook = new File(inputFile);  
   Workbook w;  
   try {  
   w = Workbook.getWorkbook(inputWorkbook);  
   // Get the first sheet  
   Sheet sheet = w.getSheet(0);  
   // Loop over first 10 column and lines  
   for (int j = 0; j < sheet.getColumns(); j++) {  
    for (int i = 0; i < sheet.getRows(); i++) {  
    Cell cell = sheet.getCell(j, i);  
    CellType type = cell.getType();  
    // It return cell if type is lable   
       if (type == CellType.LABEL) {  
     System.out.println("I got a label "  
      + cell.getContents());  
    }  
    // It return cell if type is number   
    if (type == CellType.NUMBER) {  
     System.out.println("I got a number "  
      + cell.getContents());  
    }  
    }  
   }  
   } catch (BiffException e) {  
   e.printStackTrace();  
   }  
  }  
  public static void main(String[] args) throws IOException {  
   Test test = new Test();  
   test.setInputFile("C:\\Users\\cuboidology8\\Desktop\\New folder\\temp.xls");  
   //Set file location for reading   
   test.read();  
  }  
  }   

Tuesday, October 16, 2012

my sql connectivity

Connection  to demonstrate My sql connectivity to student with name and id filled 

 import java.sql.*;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 public class mysql1 {  
      public static void main(String[] args)   
      {  
     try {  
       System.out.println("My sql Connect Example.");  
       Connection conn = null;  
         Class.forName("com.mysql.jdbc.Driver").newInstance();  
         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root","");  
         System.out.println("Connected to the database");  
         Statement statement = conn.createStatement();   
         //create table statement  
         String createStudentTable = "CREATE TABLE student(NAME VARCHAR(64),ID number(10))";                                      
         statement.executeUpdate(createStudentTable);   
         statement.executeUpdate("INSERT INTO student VALUES ( 'UML User Guide','1909')");  
         statement.executeUpdate("INSERT INTO student VALUES ( 'UML User Guide2','2909')");  
         statement.execute("UPDATE student SET id=27889 WHERE id=28889");  
 statement.executeQuery("delete * from student");  
  conn.close();  
         System.out.println("Disconnected from database");  
     } catch (InstantiationException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (IllegalAccessException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (SQLException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (ClassNotFoundException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     }   
      }    
 }