Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Read excel Query DB and email missing records

Posted on 2012-08-29
1
Medium Priority
?
653 Views
Last Modified: 2012-08-30
Hi,

I was following following link


http://www.roseindia.net/tutorial/java/poi/readExcelFile.html

I read attached excel whose content is like

username      password      
john      pass1      
eric      word1      


I got output on the console fine.



I want to capture the username and password to form one sql query to check the database



I changed the program like
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class POIRIRaw4EE {
      public static void main( String [] args ) {
            String fileName="C:\\File.xls";
            Vector dataHolder=read(fileName);
            checkDatabase(dataHolder);
      }
      public static Vector read(String fileName)    {
            Vector cellVectorHolder = new Vector();
            try{
                  FileInputStream myInput = new FileInputStream(fileName);
                  POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
                  HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
                  HSSFSheet mySheet = myWorkBook.getSheetAt(0);
                  Iterator rowIter = mySheet.rowIterator();
                  while(rowIter.hasNext()){
                        HSSFRow myRow = (HSSFRow) rowIter.next();
                        Iterator cellIter = myRow.cellIterator();
                        Vector cellStoreVector=new Vector();
                        while(cellIter.hasNext()){
                              HSSFCell myCell = (HSSFCell) cellIter.next();
                              cellStoreVector.addElement(myCell);
                        }
                        cellVectorHolder.addElement(cellStoreVector);
                  }
            }catch (Exception e){e.printStackTrace(); }
            return cellVectorHolder;
      }
      private static void checkDatabase(Vector dataHolder) {
            String username="";
            String password="";
            PreparedStatement pstmt = null;      
            for (int i=1;i<dataHolder.size(); i++){
                  Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
                  for (int j=1; j < cellStoreVector.size();j++){
                        HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
                        String stringCellValue = myCell.toString();
                        //String st = myCell.toString();
                        username=stringCellValue.substring(1);
                        password=stringCellValue.substring(0);
                        System.out.println("value username--->"+username);
                        //userkey1=st.substring(0);
                        System.out.println("value password-->"+password);
                  }
                  if(i!=0)      try{
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
                        Statement stmt = con.createStatement();
                        ResultSet rs1 = stmt.executeQuery("select * from login where username='username' and password='password'");
                        while (rs1.next()){
                              String rec1 = rs1.getString(1);
                              String rec2 = rs1.getString(2);
                              System.out.println("rec1 is"+rec1+"rec2 is"+rec2);
                        }


                        pstmt.close();
                        con.close();
                  }
                  catch(Exception e){}
            }
      }
}



Issue I faced was following system out is not showing correct values.

System.out.println("value userkey10--->"+userkey10);

System.out.println("value userkey1-->"+userkey1);

Console shows like this

value username--->ass1
value password-->pass1
value username--->ord1
value password-->word1

password is showing correct but username is not correct.So my query also failing.

I need to enhance this program to see database missing records compared to excel records and send email to user.



 

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
File.xls
0
Comment
Question by:gudii9
1 Comment
 
LVL 7

Accepted Solution

by:
gudii9 earned 0 total points
ID: 38350881
i eliminated one for loop and removed substring which resolved the issue
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.regex.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Vector;
public class POIRIInsert {
      public static void main( String [] args ) {
            String fileName="C:\\File.xls";
            Vector dataHolder=read(fileName);
            saveToDatabase(dataHolder);
      }
      public static Vector read(String fileName)    {
            Vector cellVectorHolder = new Vector();
            try{
                  FileInputStream myInput = new FileInputStream(fileName);
                  POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
                  HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
                  HSSFSheet mySheet = myWorkBook.getSheetAt(0);
                  Iterator rowIter = mySheet.rowIterator();
                  while(rowIter.hasNext()){
                        HSSFRow myRow = (HSSFRow) rowIter.next();
                        Iterator cellIter = myRow.cellIterator();
                        Vector cellStoreVector=new Vector();
                        while(cellIter.hasNext()){
                              HSSFCell myCell = (HSSFCell) cellIter.next();
                              cellStoreVector.addElement(myCell);
                        }
                        cellVectorHolder.addElement(cellStoreVector);
                  }
            }catch (Exception e){e.printStackTrace(); }
            return cellVectorHolder;
      }
      private static void saveToDatabase(Vector dataHolder) {
            String usernamefield="";
            String passwordfield="";
            List list = new ArrayList();


            for (int i=1;i<dataHolder.size(); i++){
                  System.out.println("Data is inserted--->"+dataHolder.size());
                  Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
                  //      for (int j=0; j < cellStoreVector.size();j++){
                  HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(0);
                  String stringCellValue = myCell.toString();
                  usernamefield=stringCellValue;


                  myCell = (HSSFCell)cellStoreVector.elementAt(1);
                  stringCellValue = myCell.toString();
                  passwordfield=stringCellValue;
            

            try{
                  Class.forName("com.mysql.jdbc.Driver").newInstance();
                  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
                  Statement stat=con.createStatement();
                  int k=stat.executeUpdate("insert into login(username,password) value('"+usernamefield+"','"+passwordfield+"')");
                  System.out.println("Data is inserted");
                  stat.close();
                  con.close();
            }
            catch(Exception e){}
            

            finally{

            }

            }


      }

}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question