gudii9
asked on
Read excel Query DB and email missing records
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
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
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.HSSFCe ll;
import org.apache.poi.hssf.usermodel.HSSFRo w;
import org.apache.poi.hssf.usermodel.HSSFSh eet;
import org.apache.poi.hssf.usermodel.HSSFWo rkbook;
import org.apache.poi.poifs.filesystem.POIF SFileSyste m;
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(cellStor eVector);
}
}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.e lementAt(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 ").newInst ance();
Connection con = DriverManager.getConnection("jdbc:my sql://loca lhost:3306 /test","ro ot", "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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.