state the create table name in jdbc without .xls - java
i made a program in java that it takes an excel file and store its data in a database. When i state where the file is its like this:
String filename = "test5.xls";
String path = "C:\\Users\\myfiles\\Documents\\";
But when i call the create table and i state the filename of the excel file because it it test.xls the mysql command shows error!
The create table function is:
try
{
String all = org.apache.commons.lang3.StringUtils.join(allFields, ",");
String createTableStr = "CREATE TABLE " + "table5" + " (" + all + ")";
System.out.println( "Create a new table in the database" );
stmt.executeUpdate( createTableStr );
Is there any way to rename the file before i create the table or to read only the "test" without the .xls?
My program is the below:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
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.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class readexcel {
public static void main (String[] args) throws Exception {
//String filename = "C:\\Users\\myfiles\\Documents\\test5.xls";
String filename = "test5.xls";
String path = "C:\\Users\\myfiles\\Documents\\";
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
fis = new FileInputStream(path + filename);
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
showExcelData(sheetData);
HashMap<String,Integer> tFields = new HashMap();
tFields = parseExcelData(sheetData);
String str = getCreateTable(filename, tFields);
}
private static void showExcelData(List sheetData) {
// HashMap<String, String> tableFields = new HashMap();
for (int i=0; i<sheetData.size();i++){
List list = (List) sheetData.get(i);
for (int j=0; j<list.size(); j++){
Cell cell = (Cell) list.get(j);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue());
}
else if(cell.getCellType()==Cell.CELL_TYPE_STRING)
{
System.out.print(cell.getRichStringCellValue());
}
else if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
}
if (j < list.size() - 1)
{
System.out.print(", ");
}
}
System.out.println("");
}
}
#SuppressWarnings({ "unchecked", "unused" })
private static HashMap parseExcelData (List sheetData){
HashMap<String,Integer> tableFields = new HashMap();
List list = (List) sheetData.get(0);
for (int j=0; j<list.size(); j++){
Cell cell=(Cell) list.get(j);
tableFields.put(cell.getStringCellValue(),cell.getCellType());
}
return tableFields;
}
private static String getCreateTable(String tablename, HashMap<String, Integer> tableFields){
Iterator iter = tableFields.keySet().iterator();
String str="";
String[] allFields = new String[tableFields.size()];
int i = 0;
while (iter.hasNext()){
String fieldName = (String) iter.next();
Integer fieldType=(Integer)tableFields.get(fieldName);
switch (fieldType){
case Cell.CELL_TYPE_NUMERIC:
str=fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_STRING:
str= fieldName + " VARCHAR(255)";
break;
case Cell.CELL_TYPE_BOOLEAN:
str=fieldName + " INTEGER";
break;
}
allFields[i++]= str;
}
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/kainourgia","root", "root");
Statement stmt = con.createStatement();
try
{
System.out.println( "Use the database..." );
stmt.executeUpdate( "USE kainourgia;" );
}
catch( SQLException e )
{
System.out.println( "SQLException: " + e.getMessage() );
System.out.println( "SQLState: " + e.getSQLState() );
System.out.println( "VendorError: " + e.getErrorCode() );
}
try
{
String all = org.apache.commons.lang3.StringUtils.join(allFields, ",");
String createTableStr = "CREATE TABLE " + "table5.xls" + " (" + all + ")";
System.out.println( "Create a new table in the database" );
stmt.executeUpdate( createTableStr );
}
catch( SQLException e )
{
System.out.println( "SQLException: " + e.getMessage() );
System.out.println( "SQLState: " + e.getSQLState() );
System.out.println( "VendorError: " + e.getErrorCode() );
}
}
catch( Exception e )
{
System.out.println( ((SQLException) e).getSQLState() );
System.out.println( e.getMessage() );
e.printStackTrace();
}
return str;
}
#SuppressWarnings({ "unused", "unused", "unused", "unused" })
private Statement createStatement() {
return null;
}
}
Thank you in advance!
Gives name of the file
String filename = "test5.xls";
String path = "C:\\Users\\myfiles\\Documents\\";
File f = new File (path + filename);
System.out.println(f.getName());// gives only file name with out extension
Related
How to get excel(xls) cell comments when i use poi with HSSFListener to process record?
I use HSSFListener to process record, but i don't know how to get cell comments. I use EventExample from poi source code, here is the code: public class EventExample implements HSSFListener { private SSTRecord sstrec; #Override public void processRecord(Record record) { //how to get comments here? } public static void main(String[] args) throws IOException { try (FileInputStream fin = new FileInputStream(args[0])) { try (POIFSFileSystem poifs = new POIFSFileSystem(fin)) { try (InputStream din = poifs.createDocumentInputStream("Workbook")) { HSSFRequest req = new HSSFRequest(); req.addListenerForAllRecords(new EventExample()); HSSFEventFactory factory = new HSSFEventFactory(); factory.processEvents(req, din); } } } System.out.println("done."); } }
This is a really good question which seems not answered anywhere yet. Even Apache Tika handles cell comments as extraTextCells outside the worksheet and without relation to the cell. If I have got i right, hen there are NoteRecords having cell relations. The NoteRecord.getShapeId points to an CommonObjectDataSubRecord of type CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT in an ObjRecord which is immediatelly followed by a TextObjectRecord. So to get the whole comment, we first need get the TextObjectRecords mapped to their shape Ids. Then we can get the NoteRecords and using their getShapeId methods we can get the related TextObjectRecords. Example: import org.apache.poi.hssf.eventusermodel.*; import org.apache.poi.hssf.record.*; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.InputStream; import java.io.FileInputStream; import java.util.Map; import java.util.HashMap; public class EventExample implements HSSFListener { private SSTRecord sstrec; private Map<Integer, TextObjectRecord> cellCommentRecs = new HashMap<Integer, TextObjectRecord>(); private int commentORecId = -1; public void processRecord(Record record) { switch (record.getSid()) { case SSTRecord.sid: // shared strings table sstrec = (SSTRecord) record; break; case BoundSheetRecord.sid: BoundSheetRecord bsr = (BoundSheetRecord) record; System.out.println("Sheet found named " + bsr.getSheetname()); break; case NumberRecord.sid: // numeric cell NumberRecord numrec = (NumberRecord) record; System.out.println("Cell found with value " + numrec.getValue() + " at row " + numrec.getRow() + " and column " + numrec.getColumn()); break; case LabelSSTRecord.sid: // string cell LabelSSTRecord lrec = (LabelSSTRecord) record; System.out.println("String cell found with value " + sstrec.getString(lrec.getSSTIndex()) + " at row " + lrec.getRow() + " and column " + lrec.getColumn()); break; case ObjRecord.sid: // object record (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/dd34df60-8250-40a9-83a3-911476a31ea7) ObjRecord orec = (ObjRecord) record; for (SubRecord subrec : orec.getSubRecords()) { if (subrec instanceof CommonObjectDataSubRecord) { // kind of shape (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/29161566-5018-4356-8d25-50e6674c66fa) CommonObjectDataSubRecord codsrec = (CommonObjectDataSubRecord) subrec; if (codsrec.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT) { // comment shape //System.out.println(codsrec); //FtCmo commentORecId = codsrec.getObjectId(); // we have a comment object record, so get it's Id } } } break; case TextObjectRecord.sid: // text object (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/638c08e6-2942-4783-b71b-144ccf758fc7) TextObjectRecord trec = (TextObjectRecord) record; //System.out.println(trec); //TxO if (commentORecId > -1) { // if we have a a comment object record Id already, so this is a comment text object cellCommentRecs.put(commentORecId, trec); // map that Id to the text object record commentORecId = -1; } break; case NoteRecord.sid: // note record (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/3a610bb3-9d35-435f-92ef-cdbc42974404) NoteRecord nrec = (NoteRecord) record; // shapeId points to the comment shape which is immediately followed by the comment text object System.out.println("Cell comment found at row " + nrec.getRow() + " and column " + nrec.getColumn() + ", author: " + nrec.getAuthor() + ", shape-id: " + nrec.getShapeId() + ", comment content: " + cellCommentRecs.get(nrec.getShapeId()).getStr().getString()); break; default: //System.out.println(record); break; } } public static void main(String[] args) throws Exception { //FileInputStream fin = new FileInputStream(args[0]); FileInputStream fin = new FileInputStream("./Excel.xls"); POIFSFileSystem poifs = new POIFSFileSystem(fin); InputStream din = poifs.createDocumentInputStream("Workbook"); HSSFRequest req = new HSSFRequest(); req.addListenerForAllRecords(new EventExample()); HSSFEventFactory factory = new HSSFEventFactory(); factory.processEvents(req, din); fin.close(); din.close(); System.out.println("done."); } } Microsoft documentation: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/7d9326d6-691a-4fa1-8dce-42082f38e943: Note (section 2.4.179) Obj (section 2.4.181) -> FtCmo: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/29161566-5018-4356-8d25-50e6674c66fa TxO (section 2.4.329)
Write, append DQL query results to excel in java using apache poi and DFC
I have to write the dql query results to excel in java using apache poi and DFC. I have the following code written, this needs to be organized to handle efficiently. I tried writing to excel utility in other method, but this is not working correctly. So I stopped calling the method and written the code in the main method only. This is inefficient code. In the first dql, I will fetch some attributes along with i_chronicle_id, this i_chronicle_id needs to be passed to the second dql for r_child_id. I need to add these attribute values to excel. It is doing if excel file doesn't exist create it, if exists write/append data. But after writing more amount of data this is getting slow. And when I used HSSFWorkbook the max row it can go is 1370. I didn't check for XSSFWorkbook. I tried searching all the excel writing posts, but not able to implement properly. So asking here. Please help me to organize the code efficiently and it should go for the next sheet if the sheet is full. Let me know for any information. Thanks in advance! import com.documentum.com.DfClientX; import com.documentum.com.IDfClientX; import com.documentum.fc.client.*; import com.documentum.fc.common.DfException; import com.documentum.fc.common.IDfLoginInfo; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.*; import java.util.stream.Collectors; public class MedicalDevicesReport { private static int j = 0; public static void main(String[] args) throws DfException { String chronicleId; String documentId, documentName, title, domain, primaryGroup, subGroup, artifactName, versionLabel, status, creationDate, versionNum = null, is_current; ArrayList<String> author = new ArrayList<>(); ArrayList<String> reviewer = new ArrayList<>(); ArrayList<String> formatReviewer = new ArrayList<>(); ArrayList<String> approver = new ArrayList<>(); ArrayList<String> approvalCompletionTime = new ArrayList<>(); int wfAbortCount = 0; String authorsF, reviewersF, formatReviewersF, approversF; String approvalCompletionTimeStamps; IDfClientX clientX = new DfClientX(); IDfClient dfClient = clientX.getLocalClient(); IDfSessionManager sessionManager = dfClient.newSessionManager(); IDfLoginInfo loginInfo = clientX.getLoginInfo(); loginInfo.setUser("user"); loginInfo.setPassword("password"); sessionManager.setIdentity("docbase", loginInfo); IDfSession dfSession = sessionManager.getSession("docbase"); System.out.println(dfSession); IDfQuery idfquery = new DfQuery(); IDfCollection collection1 = null; IDfCollection collection2 = null; try { String dql1 = "select distinct r_object_id, object_name, title, authors, domain, primary_group, subgroup, artifact_name, r_version_label," + "a_status, r_creation_date, i_chronicle_id from cd_quality_gmp_approved (all) where r_creation_date between " + "DATE('07/04/2018 00:00:00','mm/dd/yyyy hh:mi:ss') and DATE('07/05/2018 23:59:59','mm/dd/yyyy hh:mi:ss') order by r_creation_date"; idfquery.setDQL(dql1); collection1 = idfquery.execute(dfSession, IDfQuery.DF_READ_QUERY); int i = 1; while(collection1 != null && collection1.next()) { chronicleId = collection1.getString("i_chronicle_id"); author.add(collection1.getString("authors")); String dql2 = "select a.r_object_id, a.audited_obj_id, a.event_name as event_name, a.object_name as workflow_name, " + "doc.object_name as document_name, ra.child_label as document_version, a.owner_name as supervisor_name, " + "w.tracker_state as task_state, w.start_date as date_sent, a.user_name as task_performer, a.time_stamp as " + "task_completion_time, a.string_2 as outcome, a.event_source as event_source, a.string_3 as delegation_from, " + "a.string_4 as delegation_to from dm_audittrail a, d2c_workflow_tracker w, dm_relation ra, dm_sysobject doc " + "where a.audited_obj_id in (select w.r_object_id from d2c_workflow_tracker w where r_object_id in (select " + "distinct w.r_object_id from dm_relation r, d2c_workflow_tracker w where r.relation_name = 'D2_WF_TRACKER_DOCUMENT' " + "and r.child_id = '" + chronicleId + "' and r.parent_id=w.r_object_id)) and a.audited_obj_id=w.r_object_id and " + "ra.parent_id=w.r_object_id and a.audited_obj_id=ra.parent_id and ((a.event_name='d2_workflow_sent_task' and " + "a.user_name not in (select user_name from dm_audittrail b where b.event_name in ('d2_workflow_rejected_task', " + "'d2_workflow_forwarded_task', 'd2_delegation_delegated_task', 'd2_workflow_delegated_task', 'd2_workflow_added_note', " + "'d2_workflow_aborted') and b.audited_obj_id=a.audited_obj_id)) or (a.event_name in ('d2_workflow_rejected_task', " + "'d2_workflow_forwarded_task', 'd2_workflow_added_note', 'd2_workflow_aborted') and a.string_2 is not nullstring) or " + "(a.event_name in ('d2_delegation_delegated_task','d2_workflow_delegated_task', 'd2_workflow_added_note', " + "'d2_workflow_aborted'))) and doc.i_chronicle_id=ra.child_id and ra.child_label not In ('CURRENT',' ') order by 1 desc;"; idfquery.setDQL(dql2); collection2 = idfquery.execute(dfSession, IDfQuery.DF_READ_QUERY); while(collection2 != null && collection2.next()) { String supervisorName = collection2.getString("supervisor_name"); author.add(supervisorName); if(collection2.getString("event_name").equals("d2_workflow_aborted")) { wfAbortCount++; } if(collection2.getString("event_source").equals("Review")) { reviewer.add(collection2.getString("task_performer")); continue; } if(collection2.getString("event_source").equals("Format Review")) { if(collection2.getString("task_performer").contains("grp_wf_")) { continue; } else { formatReviewer.add(collection2.getString("task_performer")); continue; } } if((collection2.getString("event_source").equals("First Approval-no Sig")) || (collection2.getString("event_source").equals("First Approval")) || (collection2.getString("event_source").equals("Second Approval-no Sig")) || (collection2.getString("event_source").equals("Second Approval")) || (collection2.getString("event_source").contains("Approval"))) { approver.add(collection2.getString("task_performer")); approvalCompletionTime.add(collection2.getString("task_completion_time")); } } documentId = collection1.getString("r_object_id"); documentName = collection1.getString("object_name"); title = collection1.getString("title"); domain = collection1.getString("domain"); primaryGroup = collection1.getString("primary_group"); subGroup = collection1.getString("subgroup"); artifactName = collection1.getString("artifact_name"); versionLabel = collection1.getString("r_version_label"); status = collection1.getString("a_status"); creationDate = collection1.getString("r_creation_date"); String temp = versionLabel; String[] parts = temp.split("(?<=\\D)(?=\\d\\.?\\d)"); if(parts.length > 1) { versionNum = parts[1]; is_current = parts[0]; } else { is_current = parts[0]; } String versionLabelF = versionNum + " " + is_current; List<String> authors = author.stream().distinct().collect(Collectors.toList()); List<String> reviewers = reviewer.stream().distinct().collect(Collectors.toList()); List<String> formatReviewers = formatReviewer.stream().distinct().collect(Collectors.toList()); List<String> approvers = approver.stream().distinct().collect(Collectors.toList()); List<String> approvalCompletionTimeStamp = approvalCompletionTime.stream().distinct().collect(Collectors.toList()); authorsF = authors.toString().substring(1, authors.toString().length() - 1); reviewersF = reviewers.toString().substring(1, reviewers.toString().length() - 1); formatReviewersF = formatReviewers.toString().substring(1, formatReviewers.toString().length() - 1); approversF = approvers.toString().substring(1, approvers.toString().length() - 1); approvalCompletionTimeStamps = approvalCompletionTimeStamp.toString().substring(1, approvalCompletionTimeStamp.toString().length() - 1); author.clear(); reviewer.clear(); formatReviewer.clear(); approver.clear(); approvalCompletionTime.clear(); Workbook workbook = null; File file = new File("C:\\SubWay TRC\\fetched_reports\\mdreport.xlsx"); try { if (!file.exists()) { if (!file.toString().endsWith(".xls")) { workbook = new XSSFWorkbook(); workbook.createSheet(); } } else { workbook = WorkbookFactory.create(new FileInputStream(file)); workbook.createSheet(); } } catch(IOException ioe) { ioe.printStackTrace(); } Row row; try { Sheet sheet = workbook.getSheetAt(j); int last_row = sheet.getLastRowNum(); System.out.println(last_row); row = sheet.createRow(++last_row); Map<Integer, Object[]> data = new HashMap<>(); data.put(i, new Object[] {documentId, documentName, title, domain, primaryGroup, subGroup, artifactName, versionLabelF, status, creationDate, authorsF, reviewersF, formatReviewersF, approversF, approvalCompletionTimeStamps, wfAbortCount}); Set<Integer> key_set = data.keySet(); for(Integer key: key_set) { Object[] obj_arr = data.get(key); int cell_num = 0; for(Object obj: obj_arr) { Cell cell = row.createCell(cell_num++); if(obj instanceof String) { cell.setCellValue((String)obj); } } } FileOutputStream out = new FileOutputStream("C:\\SubWay TRC\\fetched_reports\\mdreport.xlsx", false); workbook.write(out); out.close(); System.out.println("Data added successfully"); } catch (IOException e) { e.printStackTrace(); } } } finally { if(collection1 != null) { collection1.close(); } if(collection2 != null) { collection2.close(); } if(dfSession != null) { sessionManager.release(dfSession); } } } private static void executeWorkflowAudit(IDfQuery idfquery, IDfSession dfSession, IDfCollection attributeCollection, String chronicleId, int i) throws DfException { IDfCollection collection; String documentId, documentName, title, domain, primaryGroup, subGroup, artifactName, versionLabel, status, creationDate, versionNum = null, is_current; ArrayList<String> author = new ArrayList<>(); ArrayList<String> reviewer = new ArrayList<>(); ArrayList<String> formatReviewer = new ArrayList<>(); ArrayList<String> approver = new ArrayList<>(); ArrayList<String> approvalCompletionTime = new ArrayList<>(); int wfAbortCount = 0; String authorsF, reviewersF, formatReviewersF, approversF; String approvalCompletionTimeStamps; String dql = "select a.r_object_id, a.audited_obj_id, a.event_name as event_name, a.object_name as workflow_name, " + "doc.object_name as document_name, ra.child_label as document_version, a.owner_name as supervisor_name, " + "w.tracker_state as task_state, w.start_date as date_sent, a.user_name as task_performer, a.time_stamp as " + "task_completion_time, a.string_2 as outcome, a.event_source as event_source, a.string_3 as delegation_from, " + "a.string_4 as delegation_to from dm_audittrail a, d2c_workflow_tracker w, dm_relation ra, dm_sysobject doc " + "where a.audited_obj_id in (select w.r_object_id from d2c_workflow_tracker w where r_object_id in (select " + "distinct w.r_object_id from dm_relation r, d2c_workflow_tracker w where r.relation_name = 'D2_WF_TRACKER_DOCUMENT' " + "and r.child_id = '" + chronicleId + "' and r.parent_id=w.r_object_id)) and a.audited_obj_id=w.r_object_id and " + "ra.parent_id=w.r_object_id and a.audited_obj_id=ra.parent_id and ((a.event_name='d2_workflow_sent_task' and " + "a.user_name not in (select user_name from dm_audittrail b where b.event_name in ('d2_workflow_rejected_task', " + "'d2_workflow_forwarded_task', 'd2_delegation_delegated_task', 'd2_workflow_delegated_task', 'd2_workflow_added_note', " + "'d2_workflow_aborted') and b.audited_obj_id=a.audited_obj_id)) or (a.event_name in ('d2_workflow_rejected_task', " + "'d2_workflow_forwarded_task', 'd2_workflow_added_note', 'd2_workflow_aborted') and a.string_2 is not nullstring) or " + "(a.event_name in ('d2_delegation_delegated_task','d2_workflow_delegated_task', 'd2_workflow_added_note', " + "'d2_workflow_aborted'))) and doc.i_chronicle_id=ra.child_id and ra.child_label not In ('CURRENT',' ') order by 1 desc;"; idfquery.setDQL(dql); collection = idfquery.execute(dfSession, IDfQuery.READ_QUERY); while(collection != null && collection.next()) { String supervisorName = collection.getString("supervisor_name"); author.add(supervisorName); if(collection.getString("event_name").equals("d2_workflow_aborted")) { wfAbortCount++; } if(collection.getString("event_source").equals("Review")) { reviewer.add(collection.getString("task_performer")); continue; } if(collection.getString("event_source").equals("Format Review")) { if(collection.getString("task_performer").contains("grp_wf_")) { continue; } else { formatReviewer.add(collection.getString("task_performer")); continue; } } if((collection.getString("event_source").equals("First Approval-no Sig")) || (collection.getString("event_source").equals("First Approval")) || (collection.getString("event_source").equals("Second Approval-no Sig")) || (collection.getString("event_source").equals("Second Approval"))) { approver.add(collection.getString("task_performer")); approvalCompletionTime.add(collection.getString("task_completion_time")); } documentId = attributeCollection.getString("r_object_id"); documentName = attributeCollection.getString("object_name"); title = attributeCollection.getString("title"); domain = attributeCollection.getString("domain"); primaryGroup = attributeCollection.getString("primary_group"); subGroup = attributeCollection.getString("subgroup"); artifactName = attributeCollection.getString("artifact_name"); versionLabel = attributeCollection.getString("r_version_label"); status = attributeCollection.getString("a_status"); creationDate = attributeCollection.getString("r_creation_date"); String temp = versionLabel; String[] parts = temp.split("(?<=\\D)(?=\\d\\.?\\d)"); if(parts.length > 1) { versionNum = parts[1]; is_current = parts[0]; } else { is_current = parts[0]; } String versionLabelF = versionNum + " " + is_current; List<String> authors = author.stream().distinct().collect(Collectors.toList()); List<String> reviewers = reviewer.stream().distinct().collect(Collectors.toList()); List<String> formatReviewers = formatReviewer.stream().distinct().collect(Collectors.toList()); List<String> approvers = approver.stream().distinct().collect(Collectors.toList()); List<String> approvalCompletionTimeStamp = approvalCompletionTime.stream().distinct().collect(Collectors.toList()); authorsF = authors.toString().substring(1, authors.toString().length() - 1); reviewersF = reviewers.toString().substring(1, reviewers.toString().length() - 1); formatReviewersF = formatReviewers.toString().substring(1, formatReviewers.toString().length() - 1); approversF = approvers.toString().substring(1, approvers.toString().length() - 1); approvalCompletionTimeStamps = approvalCompletionTimeStamp.toString().substring(1, approvalCompletionTimeStamp.toString().length() - 1); author.clear(); reviewer.clear(); formatReviewer.clear(); approver.clear(); approvalCompletionTime.clear(); Workbook workbook = null; File file = new File("C:\\SubWay TRC\\fetched_reports\\wfperf.xls"); try { if (!file.exists()) { if (!file.toString().endsWith(".xlsx")) { workbook = new HSSFWorkbook(); workbook.createSheet(); } } else { workbook = WorkbookFactory.create(new FileInputStream(file)); workbook.createSheet(); } } catch(IOException ioe) { ioe.printStackTrace(); } Row row; try { Sheet sheet = workbook.getSheetAt(j); int last_row = sheet.getLastRowNum(); System.out.println(last_row); if(last_row == 1370) { ++j; sheet = workbook.getSheetAt(j); int last_row_new = sheet.getLastRowNum(); row = sheet.createRow(++last_row_new); } else { row = sheet.createRow(++last_row); } Map<Integer, Object[]> data = new HashMap<>(); data.put(i, new Object[] {documentId, documentName, title, domain, primaryGroup, subGroup, artifactName, versionLabelF, status, creationDate, authorsF, reviewersF, formatReviewersF, approversF, approvalCompletionTimeStamps, wfAbortCount}); Set<Integer> key_set = data.keySet(); for(Integer key: key_set) { Object[] obj_arr = data.get(key); int cell_num = 0; for(Object obj: obj_arr) { Cell cell = row.createCell(cell_num++); if(obj instanceof String) { cell.setCellValue((String)obj); } } } FileOutputStream out = new FileOutputStream("C:\\SubWay TRC\\fetched_reports\\wfperf.xls", false); workbook.write(out); out.close(); System.out.println("Data added successfully"); } catch (IOException e) { e.printStackTrace(); } } } }
Try below code for organizing, in your method executeWorkflowAudit(), you are collecting all the attribute data in while loop only, what if the collection has no results, this would skip the data what you want to add apart from workflow data. Place the attribute data outside of while loop, so this won't skip adding initial collection data. I have separated the session manager and getting session parts also. Similarly, you can keep DQL queries in separate class like QueryConstants and access here. This should work, please have a try. I'm not sure about the maximum row count. Will update if I could find any. Hope, this helps you! Anyway you can refer this for writing huge data to excel. import com.documentum.com.DfClientX; import com.documentum.com.IDfClientX; import com.documentum.fc.client.*; import com.documentum.fc.common.DfException; import com.documentum.fc.common.IDfLoginInfo; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; public class MedicalDevicesReport { private static int j = 0; public static void main(String[] args) throws DfException { String chronicleId; ArrayList<String> author = new ArrayList<>(); IDfSessionManager sessionManager = getSessionManager("docbase", "user", "password"); IDfSession dfSession = sessionManager.getSession("docbase"); System.out.println(dfSession); IDfQuery idfquery = new DfQuery(); IDfCollection collection; try { String dql = "select distinct r_object_id, object_name, title, authors, domain, primary_group, subgroup, artifact_name, r_version_label," + "a_status, r_creation_date, i_chronicle_id from cd_quality_gmp_approved (all) where r_creation_date between " + "DATE('07/04/2018 00:00:00','mm/dd/yyyy hh:mi:ss') and DATE('07/05/2018 23:59:59','mm/dd/yyyy hh:mi:ss') order by r_creation_date"; idfquery.setDQL(dql); collection = idfquery.execute(dfSession, IDfQuery.DF_READ_QUERY); int i = 1; File file = new File("C:\\SubWay TRC\\fetched_reports\\mdreport.xlsx"); while(collection != null && collection.next()) { chronicleId = collection.getString("i_chronicle_id"); author.add(collection.getString("authors")); executeWorkflowAudit(dfSession, collection, idfquery, chronicleId, author, i, file); i++; } } finally { cleanup(sessionManager, dfSession); } } private static void executeWorkflowAudit(IDfSession dfSession, IDfCollection attributeCollection, IDfQuery idfquery, String chronicleId, ArrayList<String> author, int i, File file) throws DfException { IDfCollection collection; String documentId, documentName, title, domain, primaryGroup, subGroup, artifactName, versionLabel, status, creationDate, versionNum = null, is_current; ArrayList<String> reviewer = new ArrayList<>(); ArrayList<String> formatReviewer = new ArrayList<>(); ArrayList<String> approver = new ArrayList<>(); ArrayList<String> approvalCompletionTime = new ArrayList<>(); String authorsF, reviewersF, formatReviewersF, approversF; String approvalCompletionTimeStamps; int wfAbortCount = 0; String dql = "select a.r_object_id, a.audited_obj_id, a.event_name as event_name, a.object_name as workflow_name, " + "doc.object_name as document_name, ra.child_label as document_version, a.owner_name as supervisor_name, " + "w.tracker_state as task_state, w.start_date as date_sent, a.user_name as task_performer, a.time_stamp as " + "task_completion_time, a.string_2 as outcome, a.event_source as event_source, a.string_3 as delegation_from, " + "a.string_4 as delegation_to from dm_audittrail a, d2c_workflow_tracker w, dm_relation ra, dm_sysobject doc " + "where a.audited_obj_id in (select w.r_object_id from d2c_workflow_tracker w where r_object_id in (select " + "distinct w.r_object_id from dm_relation r, d2c_workflow_tracker w where r.relation_name = 'D2_WF_TRACKER_DOCUMENT' " + "and r.child_id = '" + chronicleId + "' and r.parent_id=w.r_object_id)) and a.audited_obj_id=w.r_object_id and " + "ra.parent_id=w.r_object_id and a.audited_obj_id=ra.parent_id and ((a.event_name='d2_workflow_sent_task' and " + "a.user_name not in (select user_name from dm_audittrail b where b.event_name in ('d2_workflow_rejected_task', " + "'d2_workflow_forwarded_task', 'd2_delegation_delegated_task', 'd2_workflow_delegated_task', 'd2_workflow_added_note', " + "'d2_workflow_aborted') and b.audited_obj_id=a.audited_obj_id)) or (a.event_name in ('d2_workflow_rejected_task', " + "'d2_workflow_forwarded_task', 'd2_workflow_added_note', 'd2_workflow_aborted') and a.string_2 is not nullstring) or " + "(a.event_name in ('d2_delegation_delegated_task','d2_workflow_delegated_task', 'd2_workflow_added_note', " + "'d2_workflow_aborted'))) and doc.i_chronicle_id=ra.child_id and ra.child_label not In ('CURRENT',' ') order by 1 desc"; idfquery.setDQL(dql); collection = idfquery.execute(dfSession, IDfQuery.READ_QUERY); while(collection != null && collection.next()) { String supervisorName = collection.getString("supervisor_name"); author.add(supervisorName); if(collection.getString("event_name").equals("d2_workflow_aborted")) { wfAbortCount++; } if(collection.getString("event_source").equals("Review")) { reviewer.add(collection.getString("task_performer")); continue; } if(collection.getString("event_source").equals("Format Review")) { if(collection.getString("task_performer").contains("grp_wf_")) { continue; } else { formatReviewer.add(collection.getString("task_performer")); continue; } } if((collection.getString("event_source").equals("First Approval-no Sig")) || (collection.getString("event_source").equals("First Approval")) || (collection.getString("event_source").equals("Second Approval-no Sig")) || (collection.getString("event_source").equals("Second Approval")) || (collection.getString("event_source").contains("Approval"))) { approver.add(collection.getString("task_performer")); approvalCompletionTime.add(collection.getString("task_completion_time")); } } documentId = attributeCollection.getString("r_object_id"); documentName = attributeCollection.getString("object_name"); title = attributeCollection.getString("title"); domain = attributeCollection.getString("domain"); primaryGroup = attributeCollection.getString("primary_group"); subGroup = attributeCollection.getString("subgroup"); artifactName = attributeCollection.getString("artifact_name"); versionLabel = attributeCollection.getString("r_version_label"); status = attributeCollection.getString("a_status"); creationDate = attributeCollection.getString("r_creation_date"); String temp = versionLabel; String[] parts = temp.split("(?<=\\D)(?=\\d\\.?\\d)"); if(parts.length > 1) { versionNum = parts[1]; is_current = parts[0]; } else { is_current = parts[0]; } String versionLabelF = versionNum + " " + is_current; List<String> authors = author.stream().distinct().collect(Collectors.toList()); List<String> reviewers = reviewer.stream().distinct().collect(Collectors.toList()); List<String> formatReviewers = formatReviewer.stream().distinct().collect(Collectors.toList()); List<String> approvers = approver.stream().distinct().collect(Collectors.toList()); List<String> approvalCompletionTimeStamp = approvalCompletionTime.stream().distinct().collect(Collectors.toList()); authorsF = authors.toString().substring(1, authors.toString().length() - 1); reviewersF = reviewers.toString().substring(1, reviewers.toString().length() - 1); formatReviewersF = formatReviewers.toString().substring(1, formatReviewers.toString().length() - 1); approversF = approvers.toString().substring(1, approvers.toString().length() - 1); approvalCompletionTimeStamps = approvalCompletionTimeStamp.toString().substring(1, approvalCompletionTimeStamp.toString().length() - 1); author.clear(); reviewer.clear(); formatReviewer.clear(); approver.clear(); approvalCompletionTime.clear(); Workbook workbook = null; try { if (!file.exists()) { if (!file.toString().endsWith(".xls")) { workbook = new XSSFWorkbook(); workbook.createSheet(); } } else { workbook = WorkbookFactory.create(new FileInputStream(file)); workbook.createSheet(); } } catch(IOException ioe) { ioe.printStackTrace(); } Row row; try { Sheet sheet = workbook.getSheetAt(j); int last_row = sheet.getLastRowNum(); System.out.println(last_row); row = sheet.createRow(++last_row); Map<Integer, Object[]> data = new HashMap<>(); data.put(i, new Object[] {documentId, documentName, title, domain, primaryGroup, subGroup, artifactName, versionLabelF, status, creationDate, authorsF, reviewersF, formatReviewersF, approversF, approvalCompletionTimeStamps, wfAbortCount}); Set<Integer> key_set = data.keySet(); for(Integer key: key_set) { Object[] obj_arr = data.get(key); int cell_num = 0; for(Object obj: obj_arr) { Cell cell = row.createCell(cell_num++); if(obj instanceof String) { cell.setCellValue((String)obj); } } } FileOutputStream out = new FileOutputStream("C:\\SubWay TRC\\fetched_reports\\mdreport.xlsx", false); workbook.write(out); out.close(); System.out.println("Data added successfully"); } catch (IOException e) { e.printStackTrace(); } finally { if(collection != null) { collection.close(); } } } private static IDfSessionManager getSessionManager(String docbase, String userName, String password) throws DfException { IDfClientX clientX = new DfClientX(); IDfClient client = clientX.getLocalClient(); IDfSessionManager sessionManager = client.newSessionManager(); IDfLoginInfo loginInfo = clientX.getLoginInfo(); loginInfo.setUser(userName); loginInfo.setPassword(password); sessionManager.setIdentity(docbase, loginInfo); return sessionManager; } public static void cleanup(IDfSessionManager sessionManager, IDfSession session) { if(sessionManager != null && session != null) { sessionManager.release(session); } } }
STRING cannot be resolved or is not a field
ERROR : Exception in thread "main" java.lang.Error: Unresolved compilation problems: STRING cannot be resolved or is not a field,NUMERIC cannot be resolved or is not a field,BOOLEAN cannot be resolved or is not a field,BLANK cannot be resolved or is not a field. I am using below code. import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; 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.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelReader { public static void main(String[] args) throws Exception { String filename = "test.xlsx"; try (FileInputStream fis = new FileInputStream(filename)) { HSSFWorkbook workbook = new HSSFWorkbook(fis); HSSFSheet sheet = workbook.getSheetAt(0); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); Iterator<Cell> cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); org.apache.poi.ss.usermodel.CellType type = cell.getCellTypeEnum(); if (type == CellType.STRING) { System.out.println("[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = STRING; Value = " + cell.getRichStringCellValue().toString()); } else if (type == CellType.NUMERIC) { System.out.println("[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = NUMERIC; Value =" + cell.getNumericCellValue()); } else if (type == CellType.BOOLEAN) { System.out.println("[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = BOOLEAN; Value =" + cell.getBooleanCellValue()); } else if (type == CellType.BLANK) { System.out.println("[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = BLANK CELL"); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Try to import org.apache.poi.ss.usermodel.CellType. This org.apache.poi.ss.usermodel.CellType type = cell.getCellTypeEnum(); is working but you don't call CellType the same way on next lines.
If you are using the latest version poi-4.0.1 Change all your if condition should simply be : if (type == STRING) { // .............. // .............. } else if (type == NUMERIC) { // .............. // .............. } else if (type == BOOLEAN) { // .............. // .............. } else if (type == BLANK) { // .............. // .............. } }
How can I crawl a website with multiple pages using Java?
Following code works fine but it only fetch data from one page. How can I handle pagination so that the code can fetch data from the first page then second and continue until the last page number? import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; 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.ss.usermodel.Row; import com.webscrap4j.WebScrap; import com.webscrap4j.WebScrapException; public class okayapower_battery { #SuppressWarnings("resource") public static void main(String[] args) throws IOException { ArrayList<String> al = new ArrayList<String>(); ArrayList<String> bl = new ArrayList<String>(); ArrayList<String> cl = new ArrayList<String>(); WebScrap ws = new WebScrap(); ws.setUrl("https://www.okayapower.com/product-category/inverter/"); try { ws.startWebScrap(); //al = ws.getImageTagData("img", "title"); al = ws.getSingleHTMLScriptData("<h3>", "</h3>"); bl = ws.getSingleHTMLScriptData("<del>", "</del>"); cl = ws.getSingleHTMLScriptData("<ins>", "</ins>"); HSSFWorkbook workBook = new HSSFWorkbook(); FileOutputStream fos = new FileOutputStream("/Users/parthpatil/Documents/Abm Technologies/Crawl/okaya_battery.xls"); { // Create the Sheet HSSFSheet Sheet = workBook.createSheet("products"); // Create the first row corresponding to the header Row header = Sheet.createRow(0); header.createCell(0).setCellValue("Product Name"); header.createCell(1).setCellValue("Product Price"); header.createCell(2).setCellValue("Product MRP"); // Ensure that all the List have the same size otherwise throw an exception if (al.size() != bl.size() || al.size() != cl.size()) throw new IllegalStateException("Some data is missing"); // Iterate over all the list an create the rows of data for(int i = 0; i < al.size(); i++){ // Create the current starting from 1 to al.size() HSSFRow row = Sheet.createRow((short) i + 1); // Cell of the Product Name row.createCell(0).setCellValue(al.get(i)); // Cell of the Product Price row.createCell(1).setCellValue(cl.get(i)); // Cell of the Product MRP row.createCell(2).setCellValue(bl.get(i)); } // Write the result into the file workBook.write(fos); for (String adata : al) { System.out.println("the product are:- " + adata); } for (String bdata : bl) { System.out.println("the MRp are:- " + bdata); } for (String cdata : cl) { System.out.println("the selling price is:- " + cdata); } } }catch (WebScrapException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Just add simple for loop like for(int page=1; page<=10; page++) { (where 10 is the last page you want to parse) just before ws.setUrl(... which should be as well changed to ws.setUrl("https://www.okayapower.com/product-category/inverter/page/" + String.valueOf(page) + "/"); So the resultant code should look like: import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; 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.ss.usermodel.Row; import com.webscrap4j.WebScrap; import com.webscrap4j.WebScrapException; public class okayapower_battery { #SuppressWarnings("resource") public static void main(String[] args) throws IOException { ArrayList<String> al = new ArrayList<String>(); ArrayList<String> bl = new ArrayList<String>(); ArrayList<String> cl = new ArrayList<String>(); WebScrap ws = new WebScrap(); for(int page=1; page<=10; page++) { ws.setUrl("https://www.okayapower.com/product-category/inverter/page/" + String.valueOf(page) + "/"); try { ws.startWebScrap(); //al = ws.getImageTagData("img", "title"); al = ws.getSingleHTMLScriptData("<h3>", "</h3>"); bl = ws.getSingleHTMLScriptData("<del>", "</del>"); cl = ws.getSingleHTMLScriptData("<ins>", "</ins>"); HSSFWorkbook workBook = new HSSFWorkbook(); FileOutputStream fos = new FileOutputStream("/Users/parthpatil/Documents/Abm Technologies/Crawl/okaya_battery.xls"); { // Create the Sheet HSSFSheet Sheet = workBook.createSheet("products"); // Create the first row corresponding to the header Row header = Sheet.createRow(0); header.createCell(0).setCellValue("Product Name"); header.createCell(1).setCellValue("Product Price"); header.createCell(2).setCellValue("Product MRP"); // Ensure that all the List have the same size otherwise throw an exception if (al.size() != bl.size() || al.size() != cl.size()) throw new IllegalStateException("Some data is missing"); // Iterate over all the list an create the rows of data for(int i = 0; i < al.size(); i++){ // Create the current starting from 1 to al.size() HSSFRow row = Sheet.createRow((short) i + 1); // Cell of the Product Name row.createCell(0).setCellValue(al.get(i)); // Cell of the Product Price row.createCell(1).setCellValue(cl.get(i)); // Cell of the Product MRP row.createCell(2).setCellValue(bl.get(i)); } // Write the result into the file workBook.write(fos); for (String adata : al) { System.out.println("the product are:- " + adata); } for (String bdata : bl) { System.out.println("the MRp are:- " + bdata); } for (String cdata : cl) { System.out.println("the selling price is:- " + cdata); } } } catch (WebScrapException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Importing Excel sheet data and inserting it in to mysql database in java
I have a the code which takes input as an excel file and creates table in mysql database with the same name but i am unable to insert the values in to the database. Period A B Overall 2008 70 60 65 2009 50 60 55 2010 80 70 75 2011 90 80 85 THESE are the values but i am only getting table headers in the database but not the values. here is the code. #SuppressWarnings({ "unchecked", "unused" }) private static HashMap parseExcelData (List sheetData) { HashMap<String,Integer> tableFields = new HashMap(); List list = (List) sheetData.get(0); for (int j=0; j<list.size(); j++){ Cell cell=(Cell) list.get(j); // System.out.println("Values"+cell); tableFields.put(cell.getStringCellValue(),cell.getCellType()); // System.out.println("Values"+cell.getStringCellValue()); } return tableFields; } private static String getCreateTable(String tablename, HashMap<String, Integer> tableFields){ Iterator iter = tableFields.keySet().iterator(); String str=""; String[] allFields = new String[tableFields.size()]; //System.out.println("all fields"+allFields); int i = 0; while (iter.hasNext()){ String fieldName = (String) iter.next(); Integer fieldType=(Integer)tableFields.get(fieldName); switch (fieldType){ case Cell.CELL_TYPE_NUMERIC: str=fieldName + " INTEGER"; break; case Cell.CELL_TYPE_STRING: str= fieldName + " VARCHAR(255)"; break; case Cell.CELL_TYPE_BOOLEAN: str=fieldName + " INTEGER"; break; } allFields[i++]= str; //System.out.println("all fields"+allFields); } try { Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/tutorial","root", "ranjini123"); Statement stmt = con.createStatement(); try { System.out.println( "Use the database..." ); stmt.executeUpdate( "USE tutorial;" ); } catch( SQLException e ) { System.out.println( "SQLException: " + e.getMessage() ); System.out.println( "SQLState: " + e.getSQLState() ); System.out.println( "VendorError: " + e.getErrorCode() ); } try { String all = org.apache.commons.lang3.StringUtils.join(allFields, ","); String createTableStr = "CREATE TABLE " + "Book1" + " (" + all + ")"; System.out.println( "Create a new table in the database" ); stmt.executeUpdate( createTableStr ); } catch( SQLException e ) { System.out.println( "SQLException: " + e.getMessage() ); System.out.println( "SQLState: " + e.getSQLState() ); System.out.println( "VendorError: " + e.getErrorCode() ); } } catch( Exception e ) { System.out.println( ((SQLException) e).getSQLState() ); System.out.println( e.getMessage() ); e.printStackTrace(); } return str; } #SuppressWarnings({ "unused", "unused", "unused", "unused" }) private Statement createStatement() { return null; } } Thanks in advance