![]() |
| GG Bio Metric cleaned data - 1-15-july - AM - 22-July-2014.xls |
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Excel {
public static void main(String[] args) {
List resultDataList = new ArrayList();
try
{
String fileName = "D://GG Bio Metric cleaned data - 1-15-july - AM - 22-July-2014.xls";
FileInputStream myInput = new FileInputStream(fileName);
HSSFWorkbook myWorkBook = new HSSFWorkbook(myInput);
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
int maxNumOfCells = mySheet.getRow(0).getLastCellNum();
while(rowIter.hasNext())
{
HSSFRow row = (HSSFRow) rowIter.next();
Iterator cells = row.cellIterator();
List dataList = new ArrayList();
for( int cellCounter = 0 ; cellCounter < maxNumOfCells ; cellCounter ++){ // Loop through cells
HSSFCell cell;
if( row.getCell(cellCounter ) == null )
{
cell = row.createCell(cellCounter);
}
else {
cell = row.getCell(cellCounter);
}
dataList.add(cell);
}
resultDataList.add(dataList);
}
}catch (Exception e)
{
System.out.println("ERROR IN READ EXCEL: "+e.getMessage());
}
showExcelData(resultDataList);
}
public static void showExcelData(List sheetData)
{
String columns = "";
String values = "";
Connection connectionobj = null;
Statement insertStmt = null;
long count = 0;
try {
connectionobj = SqlConnectionData.getconnection();
insertStmt = connectionobj.createStatement();
List listColumns = (List) sheetData.get(0);
columns = listColumns.toString();
columns = columns.replace("[", "(");
columns = columns.replace("]", ")");
//System.out.println("COLUMNS : "+columns);
for (int loop = 1; loop < sheetData.size();loop++)
{
values = "";
List list = (List) sheetData.get(loop);
for (int innerLoop = 0; innerLoop < list.size(); innerLoop++)
{
HSSFCell cell = (HSSFCell) list.get(innerLoop);
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
{
if(HSSFDateUtil.isCellDateFormatted(cell))
{
SimpleDateFormat dateForMatNeeded = new SimpleDateFormat("yyyy-MM-dd");
String frmDateStr = dateForMatNeeded.format(cell.getDateCellValue());
//System.out.print(frmDateStr);
values += "'"+frmDateStr+"'";
}
else
{
//System.out.print(cell.getNumericCellValue());
values += "'"+cell.getNumericCellValue()+"'";
}
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
//System.out.print(cell.getRichStringCellValue());
values += "'"+cell.getRichStringCellValue()+"'";
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK)
{
//System.out.print(" ");
values += "' '";
}
if (innerLoop < list.size() - 1) {
// System.out.print(",");
values += " ,";
}
}//end of inner Loop
count++;
System.out.println("");
String sqlStament = "INSERT INTO t036_swipe_biometric " + columns + " VALUES ("+ values +")";
//System.out.println("SQLSTATEMENT "+count+":"+sqlStament);
insertStmt.addBatch(sqlStament);
}//end of for loop
int[] rowsInsert = insertStmt.executeBatch();
connectionobj.commit();
insertStmt.clearBatch();
System.out.println("No. of Rows Inserted :"+rowsInsert.length);
} catch (Exception e) {
System.out.println("ERROR IN showExcelData : "+e.getMessage());
}
finally{
try {
connectionobj.close();
insertStmt.close();
count = 0;
} catch (SQLException e) {
System.out.println("ERROR IN Finally : "+e.getMessage());
}
}
}
}
//SqlConnectionData .java
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class SqlConnectionData {
public static Connection getconnection()
{
Properties properties = new Properties();
Connection con = null;
try {
String URL = "jdbc:mysql://20.1.1.4:3306/test";
String username = "team";
String password = "Esales123";
Properties info = new Properties();
info.put("user", username);
info.put("password", password);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(URL, info);
con.setAutoCommit(false);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("MySQL Connection Error " + e.toString());
e.printStackTrace();
}
return con;
}
}

No comments:
Post a Comment