I am trying to read an excel sheet
Added required Jars
Added poi jars within .classpath are below
<classpathentry kind="lib" path="External_Jars/poi/commons-codec-1.10.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/curvesapi-1.03.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/jsqlparser-0.8.0.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/poi-3.14-20160307.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/poi-excelant-3.14-20160307.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/poi-ooxml-3.14-20160307.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/poi-ooxml-schemas-3.14-20160307.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/poi-scratchpad-3.14-20160307.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/sqlsheet-6.5.jar"/>
<classpathentry kind="lib" path="External_Jars/poi/xmlbeans-2.6.0.jar"/>
Exception is -
java.lang.IllegalArgumentException: Cell index must be >= 0
at org.apache.poi.xssf.usermodel.XSSFRow.getCell(XSSFRow.java:237)
at org.apache.poi.xssf.usermodel.XSSFRow.getCell(XSSFRow.java:224)
at org.apache.poi.xssf.usermodel.XSSFRow.getCell(XSSFRow.java:44)
at com.hp.commercial.framework.common.JDBCExcel.updateTestCaseDoc(JDBCExcel.java:546)
at com.hp.commercial.framework.common.WebDriverListener1.afterInvocation(WebDriverListener1.java:262)
at org.testng.internal.invokers.InvokedMethodListenerInvoker$InvokeAfterInvocationWithoutContextStrategy.callMethod(InvokedMethodListenerInvoker.java:100)
at org.testng.internal.invokers.InvokedMethodListenerInvoker.invokeListener(InvokedMethodListenerInvoker.java:62)
at org.testng.internal.Invoker.runInvokedMethodListeners(Invoker.java:566)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:713)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Read excel sheet code
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
import org.openqa.selenium.remote.RemoteWebDriver;
public class JDBCExcel{
private Connection con = null;
private ResultSet resultSet = null;
private Statement st = null;
private Workbook workBook = null;
private FileInputStream fileInputStream = null;
private String filePath = null;
/**
* WritingExcel method will be used with inline calling from method where
* needed to update excel sheet after updating by POI.
* #throws SQLException
*/
private void WritingExcel() throws SQLException {
try {
// closing connectiona and fileinput Stream for already oepened excel file
this.connectionClose();
// creating fileoutput Stream and writing
FileOutputStream fileOutput = new FileOutputStream(new File(filePath));
workBook.write(fileOutput);
// closing fileoutput Steram
fileOutput.close();
// Reloading excel file using fileinput Stream
this.loadExcel(filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* load excel file from path. Based on its extension .xls or .xlsx, workbook
* using POI As well, formal declaration to get connection for driver
* manager using SqlSheet API
*
* #param path
*
*/
public void loadExcel(String path) {
try {
filePath = path;
fileInputStream = new FileInputStream(new File(filePath));
// check extension of file to determine workbook type
String fileExt = path.substring(path.indexOf(".")).toLowerCase();
// create workbook class based on file extension
if (fileExt.equals(".xls"))
workBook = new HSSFWorkbook(fileInputStream);
else if (fileExt.equals(".xlsx"))
workBook = new XSSFWorkbook(fileInputStream)
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
* getRowId method to get particular row idneified by id value in ID column
* in Last-to-First manner. This will be handled by SqlSheet API
*
* #param sheet
* #param id
* #return
* #throws SQLException
*/
public Map<String, String> getRowbyID(String sheet, String id)
throws SQLException {
Map<String, String> map = new HashMap<>();
// TO DO.. how to execute Where in SQL Query in JAVA 8
String strQuery = "SELECT * FROM " + this.getSheetFormat(sheet); // + " WHERE ID= '" + id +
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
ResultSetMetaData rmd = resultSet.getMetaData();
while (resultSet.next()) {
//drawback to read cell again..so this is used.
String idCell = resultSet.getString("ID");
// Temporary solution until Where clause working in SQL Query
if (id.equalsIgnoreCase(idCell)) {
int columntCount = rmd.getColumnCount();
map.put(rmd.getColumnName(1), idCell);
for (int i = 2; i <= columntCount; i++) {
map.put(rmd.getColumnName(i), resultSet.getString(i));
}
break;
}
}
resultSet.close();
return map;
}
public String getValuebyParamName(String sheet, String paramName)
throws SQLException {
String strParamValue = "";
// TO DO.. how to execute Where in SQL Query in JAVA 8
String strQuery = "SELECT * FROM " + this.getSheetFormat(sheet); // + " where ParamName = " + paramName;
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
while (resultSet.next()) {
// Temporary solution until Where clause working in SQL Query
if (paramName.equalsIgnoreCase(resultSet.getString("ParamName"))) {
strParamValue = resultSet.getString("ParamValue");
strParamValue = (strParamValue == null) ? "" : strParamValue;
break;
}
}
resultSet.close();
return strParamValue;
}
public String getValueByAnyColumn(String sheet, String refColumn, String refKey, String targetCol)
throws SQLException {
String strValue = "";
String strQuery = "SELECT * FROM " + this.getSheetFormat(sheet);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
while (resultSet.next()) {
if (refKey.equalsIgnoreCase(resultSet.getString(refColumn))) {
strValue = resultSet.getString(targetCol);
strValue = (strValue.equalsIgnoreCase("$Null")) ? "" : strValue;
break;
}
}
resultSet.close();
return strValue;
}
public String getNextValueOfColumn(String sheet, String column, String refKey) throws SQLException {
String strValue = "";
String strQuery = "SELECT * FROM " + this.getSheetFormat(sheet);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
resultSet.last();
int resultSize = resultSet.getRow();
resultSet.beforeFirst();
while (resultSet.next()) {
if (refKey.equals(resultSet.getString(column))) {
if ((resultSize) == resultSet.getRow()) {
resultSet.first();
strValue = resultSet.getString(column);
strValue = (strValue.equalsIgnoreCase("$Null")) ? "" : strValue;
} else {
resultSet.next();
strValue = resultSet.getString(column);
strValue = (strValue.equalsIgnoreCase("$Null")) ? "" : strValue;
}
break;
}
}
resultSet.close();
return strValue;
}
public void updateParticularColumnValue(String sheetNm, String columnNm, String targetValue, String replaceValue, String... ParamName) throws Exception{
// Declaring Variables
String paramName = ParamName.length > 0 ? ParamName[0]: "";
Iterator<Row> rows = null;
ArrayList<String> columnNames = new ArrayList<String>();
// Opening spreadsheet
Sheet sheetName = workBook.getSheet(sheetNm);
// Collecting row Iterator
rows = sheetName.rowIterator();
// Reading first row of excel as column Titles
if (rows.hasNext()) { columnNames = this.getAllColumnTitles(rows.next().cellIterator()); }
boolean isForSingleRow = !paramName.isEmpty();
// Reading rest of rows as column values
while (rows.hasNext()) {
// getting next row
Row row = rows.next();
if(isForSingleRow) {
// not updating single row
break;
}
else {
if (row.getCell(columnNames.indexOf(columnNm.toLowerCase())).getStringCellValue().equalsIgnoreCase(targetValue)) {
//Else update each row turn by turn in loop
row.getCell(columnNames.indexOf(columnNm.toLowerCase())).setCellValue(replaceValue);
if (rows.hasNext()) {
row = rows.next();
row.getCell(columnNames.indexOf(columnNm.toLowerCase())).setCellValue(targetValue);
} else {
if (DriverScript.executionMode.equalsIgnoreCase("ITG")) {
CommonUtils.getExcel(Excel.Files.User).updateParticularColumnValue(Excel.User.Password.name(), "ITGStatus", "Old", "New");
} else {
CommonUtils.getExcel(Excel.Files.User).updateParticularColumnValue(Excel.User.Password.name(), "Status", "Old", "New");
}
}
break;
}
}
}
WritingExcel();
}
public List<ExecutionConfig> getModules() throws SQLException {
List<ExecutionConfig> listReturn = new ArrayList<ExecutionConfig>();
String Product = DriverScript.product;
String ProductExecutionconfig = Product + "ExecutionConfig";
// TO DO.. how to execute Where in SQL Query in JAVA 8
String strQuery = "SELECT * FROM " + this.getSheetFormat(ProductExecutionconfig); // +
// " WHERE SelectedToExecute='Yes' and Executed='No'";
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
Map<ExecutionConfig, Integer>unsortedExecutionConfigMap = new LinkedHashMap<>();
while (resultSet.next()) {
// Temporary solution until Where clause working in SQL Query
if ("Yes".equalsIgnoreCase(resultSet.getString("SelectedToExecute"))
&& "No".equalsIgnoreCase(resultSet.getString("Executed"))) {
ExecutionConfig obj = new ExecutionConfig(
resultSet.getString("ModuleName"),
resultSet.getString("TestCategory"),
resultSet.getString("RiskLevel"),
0); //No priority as it is decided in next block
unsortedExecutionConfigMap.put(obj, Integer.valueOf(resultSet.getString("ExecutionPriority").isEmpty() ? "0" : resultSet.getString("ExecutionPriority")));
}
}
resultSet.close();
Integer[] uniquePriorityNumbers = unsortedExecutionConfigMap.values().stream().sorted().distinct().toArray(Integer[]::new);
for(int currentPriority : uniquePriorityNumbers){
unsortedExecutionConfigMap.entrySet().forEach(e ->
{
if(e.getValue() == currentPriority){
ExecutionConfig eObj = e.getKey();
eObj.setModulePriority(listReturn.size());
listReturn.add(eObj);
}
});
}
return listReturn;
}
public List<TestCases> getActionList(ExecutionConfig selectedConfig)
throws SQLException {
String strModule = selectedConfig.strModuleName;
String strTestCategory = selectedConfig.strTestCat;
String strRiskLevel = selectedConfig.strRiskLevel;
List<TestCases> listReturn = new ArrayList<TestCases>();
// TO DO.. how to execute Where in SQL Query in JAVA 8
String strQuery = "SELECT * FROM " + this.getSheetFormat(strModule);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
while (resultSet.next()) {
String testCategory = resultSet.getString("TestCategory");
String riskLevel = resultSet.getString("RiskLevel");
if ("1".equalsIgnoreCase(String.valueOf(resultSet.getInt("Enabled"))) && "Automated".equalsIgnoreCase(resultSet.getString("TestExecution")) && strTestCategory.equalsIgnoreCase(testCategory) && strRiskLevel.equalsIgnoreCase(riskLevel)) {
TestCases obj = new TestCases(resultSet.getString("TCID"),
resultSet.getString("Action"),
testCategory,
riskLevel,
resultSet.getString("TestDescription"));
listReturn.add(obj);
}
}
resultSet.close();
return listReturn;
}
public void updateExecutionConfig(String strModuleName,
String strTestCategory) throws SQLException, IOException {
// Declaring Variables
String Product = getValuebyParamName(DriverScript.prodConfigSheetName,
"Product");
String ProductExecutionconfig = Product + "ExecutionConfig";
Iterator<Row> rows = null;
ArrayList<String> columnNames = new ArrayList<String>();
int updateRow = 0;
// Opening spreadsheet
Sheet sheetName = workBook.getSheet(ProductExecutionconfig);
// Collecting row Iterator
rows = sheetName.rowIterator();
// Reading first row of excel as column Titles
if (rows.hasNext()) {
columnNames = this.getAllColumnTitles(rows.next().cellIterator());
}
// Reading rest of rows as column values
while (rows.hasNext()) {
// getting next row
Row row = rows.next();
// converting cell type to String type to read cell value as string
row.getCell(columnNames.indexOf("modulename")).setCellType(
Cell.CELL_TYPE_STRING);
row.getCell(columnNames.indexOf("testcategory")).setCellType(
Cell.CELL_TYPE_STRING);
if ((String.valueOf(row.getCell(columnNames.indexOf("modulename")))
.equalsIgnoreCase(strModuleName))
&& (String.valueOf(row.getCell(columnNames
.indexOf("testcategory")))
.equalsIgnoreCase(strTestCategory))) {
{
// updating row
row.getCell(columnNames.indexOf("executed")).setCellValue(
"Yes");
updateRow++;
}
}
}
LocalLogManager.logf.info(Product + "ExecutionConfig for Module "
+ strModuleName + " has been updated for rows " + updateRow);
WritingExcel();
}
public void connectionClose() throws SQLException, IOException {
if (con != null){
con.close();
fileInputStream.close();}
}
public Connection getCon() {
return con;
}
public Statement getSt() {
return st;
}
public void closeStmt() throws SQLException {
if (st != null)
st.close();
}
public void updateTestCaseDoc(String sModuleName, String sActionName,
String sStatus) throws SQLException, ClassNotFoundException {
// Declaring Variables
Iterator<Row> rows = null;
ArrayList<String> columnNames = new ArrayList<String>();
String BrowserName = ((RemoteWebDriver)LocalDriverFactory.getDriver()).getCapabilities().getBrowserName();
BrowserName = BrowserName.equalsIgnoreCase("Internet Explorer") ? "IE": BrowserName;
System.out.println("BROWSER NAME IS XXXXXXXXXXXXXXXXXXXXXXXXXXXXX " + BrowserName);
// Opening spreadsheet
Sheet sheetName = workBook.getSheet(sModuleName);
// Collecting row Iterator
rows = sheetName.rowIterator();
// Reading first row of excel as column Titles
if (rows.hasNext()) {
columnNames = this.getAllColumnTitles(rows.next().cellIterator());
}
// Reading rest of rows as column values
while (rows.hasNext()) {
// getting next row
Row row = rows.next();
// converting cell type to String type to read cell value as string
row.getCell(columnNames.indexOf("action")).setCellType(
Cell.CELL_TYPE_STRING);
// checking if multiple cell has right value for paramiterziation to
// update row
if (String.valueOf(row.getCell(columnNames.indexOf("action")))
.equalsIgnoreCase(sActionName)) {
// updating row
row.getCell(columnNames.indexOf(BrowserName.toLowerCase() + "_passfail")).setCellValue(sStatus);
}
}
WritingExcel();
}
/**
* getMappedColValues method to retrieve map object including MappedFields
* type of object with first primary identifyable column name. This will be
* handled by SqlSheet API
*
* #param sheetname
* #return
* #throws Exception
*/
public Map<String, MappedFields> getMappedColValues(String sheetname)
throws Exception {
String strQuery = "SELECT * FROM " + this.getSheetFormat(sheetname);
Map<String, MappedFields> returnMap = new HashMap<String, MappedFields>();
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultSet = st.executeQuery(strQuery);
while (resultSet.next()) {
MappedFields mapFields = new MappedFields(resultSet.getString(2),
resultSet.getString(3), resultSet.getString(4));
returnMap.put(resultSet.getString(1), mapFields);
}
resultSet.close();
return returnMap;
}
/**
* getRowCount method to get total rows in sheet. This will be handled by
* SqlSheet API
*
* #param strSheetName
* #return
* #throws SQLException
*/
public int getRowCount(String strSheetName) throws SQLException {
int totalRow = 0;
String strQuery = "SELECT * FROM " + this.getSheetFormat(strSheetName);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
while (resultSet.next())
totalRow++;
resultSet.close();
return totalRow;
}
/**
* getRowCount method to get total rows in sheet. This will be handled by
* SqlSheet API
*
* #param strSheetName
* #return
* #throws SQLException
*/
public int getActiveTCRowCount(String groupName) throws SQLException {
String module, testCategory, riskLevel;
module = groupName.split("_")[0];
testCategory = groupName.split("_")[1];
riskLevel = groupName.split("_")[2];
String strQuery = "SELECT * FROM " + this.getSheetFormat(module);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
int rowIndex= 0;
while (resultSet.next()) {
if (testCategory.equalsIgnoreCase(resultSet.getString("TestCategory")) &&
riskLevel.equalsIgnoreCase(resultSet.getString("RiskLevel")) &&
"1".equalsIgnoreCase(resultSet.getString("Enabled"))){
rowIndex++;
}
}
return rowIndex;
}
/**
* getAllRowValue method to get all rows of parameterized sheet.
*
* #param strSheetName
* #return
* #throws SQLException
*/
public List<Map<String, String>> getAllRowValue(String strSheetName)
throws SQLException {
List<Map<String, String>> returnRowList = new ArrayList<Map<String, String>>();
String strQuery = "SELECT * FROM " + this.getSheetFormat(strSheetName);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
while (resultSet.next()) {
int columntCount = resultSetMetaData.getColumnCount();
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i <= columntCount; i++) {
map.put(resultSetMetaData.getColumnName(i),
resultSet.getString(i));
}
returnRowList.add(map);
}
resultSet.close();
return returnRowList;
}
/**
* getPreDataSelectedModules method to get list of module. This will be
* handled by SqlSheet API
*
* #return
* #throws SQLException
*/
public List<String> getPreDataSelectedModules() throws SQLException {
List<String> listReturn = new ArrayList<String>();
String product = DriverScript.product;
String productExecutionconfig = product + "ExecutionConfig";
// TO DO.. how to execute Where in SQL Query in JAVA 8
String strQuery = "SELECT * FROM " + this.getSheetFormat(productExecutionconfig);
/* + " WHERE CreateAutomationPreData='Yes' ORDER BY Ranking"; */
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
while (resultSet.next()) {
// Temporary solution until Where clause working in SQL Query
if ("Yes".equalsIgnoreCase(resultSet
.getString("CreateAutomationPreData")))
listReturn.add(resultSet.getString("ModuleName"));
}
resultSet.close();
return listReturn;
}
/**
* getAllRowValueForModules method to get list of module columns based on
* provided strWhreCluase including module names. This will be handled by
* SqlSheet API
*
* #param strSheetName
* #param strWhereClause
* #return
* #throws SQLException
*/
public List<Map<String, String>> getAllRowValueForModules(
String strSheetName, String strWhereClause) throws SQLException {
// Fetching module name values from strWhereClause parameter.
List<String> moduleNames = new ArrayList<String>();
Matcher p = Pattern.compile("'(.*?)'").matcher(strWhereClause);
while (p.find()) {
moduleNames.add(p.group().replace("'", "").replace("%", "").toLowerCase());
}
List<Map<String, String>> returnRowList = new ArrayList<>();
String strQuery = "SELECT * FROM " + this.getSheetFormat(strSheetName);
/* + "WHERE " + strWhereClause; */
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
while (resultSet.next()) {
if (moduleNames.contains(resultSet.getString(2).toLowerCase())) {
int columntCount = resultSetMetaData.getColumnCount();
Map<String, String> map = new HashMap<>();
for (int i = 1; i < columntCount; i++) {
map.put(resultSetMetaData.getColumnName(i),
resultSet.getString(i));
}
returnRowList.add(map);
}
}
resultSet.close();
return returnRowList;
}
/**
* getAllColumnTitles method to provide all column names in
* ArrayList<String> object This will be used by methods where only POI
* worked.
*
* #param cells
* #return
*/
private ArrayList<String> getAllColumnTitles(Iterator<Cell> cells) {
// Temporary variable to send
ArrayList<String> columns = new ArrayList<String>();
// filling value of cells from parameter to arrayList<String>
while (cells.hasNext()) {
Cell cell = cells.next();
columns.add(cell.getStringCellValue().toLowerCase());
}
// returning list of columns
return columns;
}
private String getSheetFormat(String sheetName){
if (System.getProperty("java.version").contains("1.8"))
return sheetName;
else
return "[" + sheetName + "$]";
}
/** updateAnyColumnValue method to update any column value for single row or all rows
* #param sheetNm -> Name of Sheet
* #param columnNm -> Name of column to be updated with value
* #param ParamValue -> Value to be updated to for given column name
* #param ParamName -> Value of row identifier from frist column. If empty, assumed that all rows should be updated of given column with ParamValue
* #throws Exception
* #author Mitul Thesiya
*/
public void updateAnyColumnValue(String sheetNm, String columnNm, String ParamValue, String... ParamName) throws Exception{
// Declaring Variables
String paramName = ParamName.length > 0 ? ParamName[0]: "";
Iterator<Row> rows = null;
ArrayList<String> columnNames = new ArrayList<String>();
// Opening spreadsheet
Sheet sheetName = workBook.getSheet(sheetNm);
// Collecting row Iterator
rows = sheetName.rowIterator();
// Reading first row of excel as column Titles
if (rows.hasNext()) { columnNames = this.getAllColumnTitles(rows.next().cellIterator()); }
boolean isForSingleRow = !paramName.isEmpty();
// Reading rest of rows as column values
while (rows.hasNext()) {
// getting next row
Row row = rows.next();
//if ParamName parameter is found with row identifier value from first column by default
if(isForSingleRow) {
// converting cell type to String type to read cell value as string
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
// checking if ParamName column title has same keyName of field as from parameter
if (String.valueOf(row.getCell(0)).equalsIgnoreCase(paramName)) {
// updating row
row.getCell(columnNames.indexOf(columnNm.toLowerCase())).setCellValue(ParamValue);
break;
}
}
else //Else update each row turn by turn in loop
row.getCell(columnNames.indexOf(columnNm.toLowerCase())).setCellValue(ParamValue);
}
WritingExcel();
}
public void updatedDataIncrementNumbers(String sheetNm, String ParamName, String ParamValue) throws Exception{
// Declaring Variables
Iterator<Row> rows = null;
ArrayList<String> columnNames = new ArrayList<String>();
// Opening spreadsheet
Sheet sheetName = workBook.getSheet(sheetNm);
// Collecting row Iterator
rows = sheetName.rowIterator();
// Reading first row of excel as column Titles
if (rows.hasNext()) { columnNames = this.getAllColumnTitles(rows.next().cellIterator()); }
// Reading rest of rows as column values
while (rows.hasNext()) {
// getting next row
Row row = rows.next();
// converting cell type to String type to read cell value as string
row.getCell(columnNames.indexOf("paramname")).setCellType(Cell.CELL_TYPE_STRING);
// checking if ParamName column title has same keyName of field as from parameter
if (String.valueOf(row.getCell(columnNames.indexOf("paramname"))).equalsIgnoreCase(ParamName)) {
// updating row
//if(ParamValue.length() != 0) {row.getCell(columnNames.indexOf("paramname")+1).setCellValue(ParamValue);}
row.getCell(columnNames.indexOf("paramname")+1).setCellValue(ParamValue);
break;
}
}
WritingExcel();
}
public int getPriorityOfTC(String actionName, String groupName) throws Exception{
String module, testCategory, riskLevel;
module = groupName.split("_")[0];
testCategory = groupName.split("_")[1];
riskLevel = groupName.split("_")[2];
int lastPriority = this.getMaxPriorityOfModule(module, testCategory, riskLevel);
String strQuery = "SELECT * FROM " + this.getSheetFormat(module);
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
Map<String, Integer> foundTestMethods = new LinkedHashMap();
try{
while (resultSet.next()) {
//if (resultSet.getString("Action").toLowerCase().equalsIgnoreCase(actionName.toLowerCase()) &&
if (testCategory.equalsIgnoreCase(resultSet.getString("TestCategory")) &&
riskLevel.equalsIgnoreCase(resultSet.getString("RiskLevel")) &&
"1".equalsIgnoreCase(resultSet.getString("Enabled"))){
foundTestMethods.put(resultSet.getString("Action"), resultSet.getString("ExecutionPriority").isEmpty() ? 0 : Integer.valueOf(resultSet.getString("ExecutionPriority")));
}
}
Integer[] uniquePriorityNumbers = foundTestMethods.values().stream().sorted().distinct().toArray(Integer[]::new);
int setPriroity = lastPriority;
for(int currentNumber : uniquePriorityNumbers){
if(currentNumber == foundTestMethods.get(actionName)){
for(Entry<String, Integer> keyValue : foundTestMethods.entrySet()){
if(keyValue.getValue() == currentNumber){
setPriroity++;
if(keyValue.getKey().equalsIgnoreCase(actionName)){
break;
}
}
}
break;
}
setPriroity += (int) foundTestMethods.values().stream().filter(e -> e.compareTo(currentNumber)== 0).count();
}
return setPriroity;
}
catch(Exception ex){
return lastPriority; //No matched row having same action name found
}
}
public int getMaxPriorityOfModule(String moduleName, String testCategory, String riskLevel) throws Exception{
int maxEligibleRowCount = 0;
int loopCount = Integer.valueOf(DriverScript.listModule.stream().filter(e -> moduleName.equalsIgnoreCase(e.getStrModuleName()) &&
testCategory.equalsIgnoreCase(e.getStrTestCat()) &&
riskLevel.equalsIgnoreCase(e.getStrRiskLevel())).findAny().get().getModulePriority());
for(int i=0; i<loopCount; i++){
String strQuery = "SELECT * FROM " + this.getSheetFormat(DriverScript.listModule.get(i).getStrModuleName());
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
try{
while (resultSet.next()) {
if ("1".equalsIgnoreCase(resultSet.getString("Enabled"))){
maxEligibleRowCount++;
}
}
}
catch(Exception ex){
return maxEligibleRowCount; //No matched row having same action name found
}
}
return maxEligibleRowCount;
}
}
The column names are not as expected. The ArrayList columnNames does not contain either
columnNames.indexOf("action")
or
columnNames.indexOf(BrowserName.toLowerCase() + "_passfail")
so that indexOf returns -1. Hence the IllegalArgumentException in getCell. Correct the column names in your Excel file and think about error handling for such cases.
Related
I have below DBImporter class which is working fine and also inserting data correctly in database table. I am trying to fetch data from .CSV file and inserting into Oracle table.
Till now i was processing only one file in my directory and which is working fine. Now i want to process more than one file. So during run the first file process correctly and inserted data, in second file it started reading data and throw an error as :
java.lang.IllegalArgumentException: SQL array must not be empty
Below is my DBimporter class. I think the error is during final commit batch somewhere in line here but not sure
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
#Service
public class DBImporter {
private final static Logger log = LoggerFactory.getLogger(DBImporter.class);
private static final List<String> NULL_VALUES = Arrays.asList("", "N.A", "N.A", "UNKNOWN");
private static final List<String> COL_HEADERS = Arrays.asList("ID", "NM", "TYE", "SA");
private static final int BATCH_SIZE = 50;
private boolean eof = false;
private String tableName;
#Autowired
private JdbcTemplate jdbcTemplate;
public void setTableName(String tableName) {
this.tableName = tableName;
}
#Transactional(rollbackFor = IOException.class)
public void processFile(BufferedReader reader, String tableName) {
this.tableName = tableName;
List<String> sqlBatch = new ArrayList<String>(BATCH_SIZE);
log.info("Starte auslesen der Daten");
long t1 = System.currentTimeMillis();
log.info("Start time: " + t1);
jdbcTemplate.execute("DELETE FROM " + tableName);
while (!eof) {
try {
Map<String, ColumnData> dbColumns = getDBColumns();
// Get a list of db column data related to the column headers.
List<ColumnData> columnData = COL_HEADERS.stream().map(dbColumns::get).collect(toList());
// Get the next valid data row if its starts from "FRO" or "BO".
List<String> dataRow = findNextLineStartingWith(reader, "R", "T");
String query = createSql(columnData, dataRow);
sqlBatch.add(query);
// Process batch.
if (sqlBatch.size() >= BATCH_SIZE) {
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
sqlBatch.clear();
}
} catch (IllegalStateException e) {
break;
} catch (IOException e) {
log.error(e.getLocalizedMessage());
}
}
// Commit the final batch.
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
sqlBatch.clear();
long delta = System.currentTimeMillis() - t1;
log.info("Total runtime : " + delta / 1000 + " seconds");
}
/**
* Create a SQL insert query using the data row.
*
* #param tableName Name of the table.
* #param columnData Column data list.
* #param dataRow Data row to be inserted.
* #return Generated SQL query string.
*/
private String createSql(List<ColumnData> columnData, List<String> dataRow) {
List<String> values = new ArrayList<>(columnData.size());
for (int i = 0; i < columnData.size(); i++) {
if (NULL_VALUES.contains(dataRow.get(i))) {
values.add("NULL");
} else if (columnData.get(i).getType() >= Types.NUMERIC && columnData.get(i).getType() <= Types.DOUBLE) {
values.add(dataRow.get(i));
} else {
values.add("'" + dataRow.get(i).replace("'", "''") + "'");
}
}
return "INSERT INTO " + tableName + " (" +
columnData.stream().filter(Objects::nonNull).map(ColumnData::getName).collect(joining(", ")) +
", SYSTEM_INSERTED_AT) VALUES (" +
values.stream().collect(joining(", ")) +
", CURRENT_TIMESTAMP)";
}
/**
* Find the next line starting with the given string and split it into columns.
*
* #param reader BufferedReader object to be used.
* #param prefixes A list of prefixes to look for in the string.
* #return List of data objects.
* #throws IOException
*/
private List<String> findNextLineStartingWith(BufferedReader reader, String... prefixes) throws IOException {
while (true) {
String line = readLineOrThrow(reader);
for (String prefix : prefixes)
if (line.startsWith(prefix)) {
ArrayList<String> data = new ArrayList<>();
// Split the line using the delimiter.
data.addAll(Arrays.asList(line.split(";")));
// Build the row to be inserted.
List<String> row = Arrays.asList(data.get(1), data.get(2).trim(), "", "");
return row;
}
}
}
/**
* Read a single line in the file.
*
* #param reader BufferedReader object to be used.
* #return
* #throws IOException
*/
private String readLineOrThrow(BufferedReader reader) throws IOException {
String line = reader.readLine();
if (line == null) {
this.eof = true;
throw new IllegalStateException("Unexpected EOF");
}
return line.trim();
}
/**
* Read database column metadata.
*
* #param tableName Name of the table to process.
* #return A map containing column information.
*/
private Map<String, ColumnData> getDBColumns() {
Map<String, ColumnData> result = new HashMap<>();
try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
ResultSet rs = connection.getMetaData().getColumns(null, null, tableName, null);
while (rs.next()) {
String columnName = rs.getString(4).toUpperCase();
int type = rs.getInt(5);
result.put(columnName, new ColumnData(columnName, type));
}
return result;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Please try below changes:
// Commit the final batch.
if (sqlBatch.size() > 0){
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
sqlBatch.clear();
}
And
#Transactional(rollbackFor = IOException.class)
public void processFile(BufferedReader reader, String tableName) {
eof = false;
...
But if you want a more clear and safe solution do changes in your code as below:
public class DBImporter {
private final static Logger log = LoggerFactory.getLogger(DBImporter.class);
private static final List<String> NULL_VALUES = Arrays.asList("", "N.A", "N.A", "UNKNOWN");
private static final List<String> COL_HEADERS = Arrays.asList("USER_ID", "NAME", "TYPE", "SRC_DATA");
private static final int BATCH_SIZE = 50;
#Autowired
private JdbcTemplate jdbcTemplate;
#Transactional(rollbackFor = IOException.class)
public void processFile(BufferedReader reader, String tableName) {
AtomicBoolean eof = new AtomicBoolean(false);
List<String> sqlBatch = new ArrayList<String>(BATCH_SIZE);
log.info("Starte auslesen der Daten");
long t1 = System.currentTimeMillis();
log.info("Start time: " + t1);
jdbcTemplate.execute("DELETE FROM " + tableName);
while (!eof.get()) {
try {
Map<String, ColumnData> dbColumns = getDBColumns(tableName);
// Get a list of db column data related to the column headers.
List<ColumnData> columnData = COL_HEADERS.stream().map(dbColumns::get).collect(toList());
// Get the next valid data row if its starts from "R" or "T".
List<String> dataRow = findNextLineStartingWith(reader, eof, "R", "T");
String query = createSql(tableName, columnData, dataRow);
sqlBatch.add(query);
// Process batch.
if (sqlBatch.size() >= BATCH_SIZE) {
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
sqlBatch.clear();
}
} catch (IllegalStateException e) {
break;
} catch (IOException e) {
log.error(e.getLocalizedMessage());
}
}
// Commit the final batch.
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
sqlBatch.clear();
long delta = System.currentTimeMillis() - t1;
log.info("Total runtime : " + delta / 1000 + " seconds");
}
/**
* Create a SQL insert query using the data row.
*
* #param tableName Name of the table.
* #param columnData Column data list.
* #param dataRow Data row to be inserted.
* #return Generated SQL query string.
*/
private String createSql(String tableName, List<ColumnData> columnData, List<String> dataRow) {
List<String> values = new ArrayList<>(columnData.size());
for (int i = 0; i < columnData.size(); i++) {
if (NULL_VALUES.contains(dataRow.get(i))) {
values.add("NULL");
} else if (columnData.get(i).getType() >= Types.NUMERIC && columnData.get(i).getType() <= Types.DOUBLE) {
values.add(dataRow.get(i));
} else {
values.add("'" + dataRow.get(i).replace("'", "''") + "'");
}
}
return "INSERT INTO " + tableName + " (" +
columnData.stream().filter(Objects::nonNull).map(ColumnData::getName).collect(joining(", ")) +
", SYSTEM_INSERTED_AT) VALUES (" +
values.stream().collect(joining(", ")) +
", CURRENT_TIMESTAMP)";
}
/**
* Find the next line starting with the given string and split it into columns.
*
* #param reader BufferedReader object to be used.
* #param prefixes A list of prefixes to look for in the string.
* #return List of data objects.
* #throws IOException
*/
private List<String> findNextLineStartingWith(BufferedReader reader, AtomicBoolean eof, String... prefixes) throws IOException {
while (true) {
String line = readLineOrThrow(reader, eof);
for (String prefix : prefixes)
if (line.startsWith(prefix)) {
ArrayList<String> data = new ArrayList<>();
// Split the line using the delimiter.
data.addAll(Arrays.asList(line.split(";")));
// Build the row to be inserted.
List<String> row = Arrays.asList(data.get(1), data.get(2).trim(), "", "");
// Insert type depending on the prefix.
if (prefix.equals("R"))
row.set(2, "USER");
else if (prefix.equals("T"))
row.set(2, "PERM");
row.set(3, String.join(";", row.subList(0, 3)));
return row;
}
}
}
/**
* Read a single line in the file.
*
* #param reader BufferedReader object to be used.
* #return
* #throws IOException
*/
private String readLineOrThrow(BufferedReader reader, AtomicBoolean eof) throws IOException {
String line = reader.readLine();
if (line == null) {
eof.set(true);
throw new IllegalStateException("Unexpected EOF");
}
return line.trim();
}
/**
* Read database column metadata.
*
* #param tableName Name of the table to process.
* #return A map containing column information.
*/
private Map<String, ColumnData> getDBColumns(String tableName) {
Map<String, ColumnData> result = new HashMap<>();
try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
ResultSet rs = connection.getMetaData().getColumns(null, null, tableName, null);
while (rs.next()) {
String columnName = rs.getString(4).toUpperCase();
int type = rs.getInt(5);
result.put(columnName, new ColumnData(columnName, type));
}
return result;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
There is the possibility that your final batch is empty.
This is possible in case you just commited BATCH_SIZE entries and have cleared the sqlBatch. In case your while loop exits at this point of time,
there are no elements to commit.
You'll want to fix that by adding a size check, for example:
// Commit the final batch (only if there is something left)
if (sqlBatch.isEmpty() == false) {
jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[sqlBatch.size()]));
sqlBatch.clear();
}
Edit:
As #Vasif pointed out you'll need to reset the eof between different calls of the method.
A simple solution (albeit somewhat hacky) would be
boolean eof = false
while (!eof) {
try {
} catch (IllegalStateException e) {
eof = true;
break;
} catch (IOException e) {
log.error(e.getLocalizedMessage());
}
}
A proper solution would be to refactor your code so that it does not rely on these exception being thrown.
Some tips:
Get rid of readLineOrThrow.
Remove the while(true) in findNextLineStartingWith and instead return an empty list if the next line is null.
Adjust the outside loop to handle this return value appropriately.
(Note: you might also need to break the loop if you get an IOException).
I need the output of my code to have columns of ID, NAME and CALORIES, but I'm not sure 'how'. I would like the id# to automatically populate itself, but when I had attempted what I had found on Oracle, it didn't work. And how do I go about having lines appear that separate everything?
This is my code:
package edu.umsl.java3816.foodItem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FoodItemTable {
Connection fit = null;
public static void main(String[] args) {
String createTableSQL = "create table FOOD_ITEM(ID INTEGER,NAME VARCHAR(256),CALORIES INTEGER)";
String insertTableSQL = "INSERT INTO FOOD_ITEM(ID,NAME,CALORIES) VALUES('1','hamburger','550')";
String selectSQLStatement = "SELECT * FROM FOOD_ITEM";
FoodItemTable fit = new FoodItemTable();
try {
fit.getConnection();
fit.createTable(createTableSQL);
fit.insertSQL(insertTableSQL);
fit.selectSQL(selectSQLStatement);
fit.shutdownDB();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
fit = DriverManager.getConnection("jdbc:hsqldb:mem", "sa", // username
"");
}
public void createTable(String sqlStatement) {
Statement statement = null;
try {
statement = fit.createStatement();
int i = statement.executeUpdate(sqlStatement);
if (i == -1) {
System.out.println("Error: " + sqlStatement);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertSQL(String insertSQLStatement) {
Statement statement = null;
try {
statement = fit.createStatement();
int i = statement.executeUpdate(insertSQLStatement);
if (i == -1) {
System.out.println("Error: " + insertSQLStatement);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void selectSQL(String selectSQLStatement) {
Statement statement = null;
try {
statement = fit.createStatement();
ResultSet rs = statement.executeQuery(selectSQLStatement);
while (rs.next()) {
System.out.println(rs.getInt("ID"));
System.out.println(rs.getString("NAME"));
System.out.println(rs.getInt("CALORIES"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void shutdownDB() {
try {
fit.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
It looks like you want to show the results of your query within a console window but in a table style orderly fashion (at least to some extent).
You can do this with the use of a java method but first you will need to place your returned query result set into an two dimensional (2D) Array. Here is how you might accomplish this (utilizing your current selectSQL() method):
public void selectSQL(String selectSQLStatement) {
PreparedStatement statement;
int resultSetCount = 0;
String rowCountSQL = "SELECT COUNT(*) AS rCount FROM FOOD_ITEM;";
try {
//Get the number of records within that will be
//retrieved from your query...
statement = fit.prepareStatement(rowCountSQL);
ResultSet rs = statement.executeQuery();
while (rs.next()) { resultSetCount = rs.getInt("rCount"); }
// Are there records to display?
if (resultSetCount == 0) {
// No there isn't
System.out.println("There are NO Records to display!");
return;
}
// Yes there is so let's declare our 2D Object Array...
Object[][] queriedRecords = new Object[resultSetCount][3];
// And now fill the array...
statement = fit.prepareStatement(selectSQLStatement);
rs = statement.executeQuery();
int counter = 0;
while (rs.next()) {
queriedRecords[counter][0] = rs.getInt("ID");
queriedRecords[counter][1] = rs.getString("NAME");
queriedRecords[counter][2] = rs.getInt("CALORIES");
counter++;
}
// Display the retrieved records in Console window...
// The table header names to be used when printed
String[] tableHeader = { "ID", "NAME", "CALORIES" };
consolePrintTable(tableHeader, queriedRecords, 2, false, true);
} catch (SQLException e) { e.printStackTrace(); }
}
You will notice at the bottom of this method is a call to yet another method named consolePrintTable(). This method will display the retrieved data to the console. Read the JavaDoc I have supplied with this method. Here is the method code :
/**
* This method will take the supplied data and print a table to console in a particular
* spaced format.<br><br>
*
* <b>Example Usage:</b><pre>
*
* final Object[][] table = new Object[4][];
* table[0] = new Object[] { "foo", "bar", "baz", "bar2", "foo2", "baz2" };
* table[1] = new Object[] { "bar2", "foo2", "baz2", "bar2", "foo2", "baz2" };
* table[2] = new Object[] { "baz3", "bar3", "foo3", "bar2", "foo2", "baz2" };
* table[3] = new Object[] { "foo4", "bar4", "baz4", "bar2", "foo2", "baz2" };
*
* String[] h = {"Header 1", "Header 2", "Header 3", "Header 4", "Header 5", "Header 6"};
* consolePrintTable(h, table, 4, false, true);
*
* // Output will be:
*
* --------------------------------------------------------------------
* Header 1 Header 2 Header 3 Header 4 Header 5 Header 6
* --------------------------------------------------------------------
* foo bar baz bar2 foo2 baz2
* bar2 foo2 baz2 bar2 foo2 baz2
* baz3 bar3 foo3 bar2 foo2 baz2
* foo4 bar4 baz4 bar2 foo2 baz2</pre>
*
* #param headerData (1D String Array) Column (header) titles for the table.
* If no Header is desired then supply <b>null</b><br>
*
* #param tableData (2D Object Array) The table data to display.<br>
*
* #param spacesBetweenCells (Integer) The table that will be printed is always
* spaced apart from one another based on the widest cell detected within both
* supplied header data or the 2D Object Array data. This parameter allows you
* add additional spacing between each column.<br>
*
* #param options (optional - Boolean) ...<pre>
*
* rightAlignment - (Default is false) If boolean true is supplied
* theTable is displayed as right justified. Boolean
* false make the table display as left justified.
*
* applyHeaderLines - (Default is true) By default lines are applied to
* the table so as to separate the header from table
* data. If false is supplied then header lines are
* not displayed. This option only applies if a Header
* 1D String Array is supplied (not null).
*
* </pre><br>
*/
public static void consolePrintTable(String[] headerData, Object[][] tableData, int spacesBetweenCells, boolean... options) {
if (tableData.length == 0) { return; }
boolean alignRight = false; // Default is Left Alignment
boolean addHeaderLines = true;
if(options.length > 0) {
if (options.length >= 1) { alignRight = options[0]; }
if (options.length == 2) { addHeaderLines = options[1]; }
}
// Get the widest Cell needed so that all the
// table cells will be the same when printed.
int widestCell = 0;
for (Object[] tableData1 : tableData) {
for (int j = 0; j < tableData[0].length; j++) {
int l = tableData1[j].toString().length();
if (l > widestCell) { widestCell = l; }
}
}
//Now check for the widest in header (if any)
if (headerData != null && headerData.length > 0) {
for(int i = 0; i < headerData.length; i++) {
if (headerData[i].length() > widestCell) {
widestCell = headerData[i].length();
}
}
}
widestCell = (widestCell + spacesBetweenCells);
// -------------------------------------------
int columns = tableData[0].length;
String format = "", alignStrg = "-";
if (alignRight) { alignStrg = ""; }
for (int i = 1; i <= columns; i++) {
format+= "%" + alignStrg + String.valueOf(widestCell) + "s";
}
format+= "\n";
//Print The Header (if any)...
if (headerData != null && headerData.length > 0) {
int charCount = columns*widestCell;
if (!alignRight) { charCount = ((columns*widestCell) - spacesBetweenCells);}
String gridline = "\n" + String.join("", Collections.nCopies(charCount, "-"));
if (addHeaderLines) { System.out.println(gridline); }
for(int i = 0; i < headerData.length; i++) {
System.out.printf("%" + alignStrg + String.valueOf(widestCell) + "s", headerData[i]);
}
if (addHeaderLines) { System.out.println(gridline); }
else { System.out.println(""); }
}
// Display the Table data...
for (final Object[] row : tableData) {
System.out.format(format, row);
}
}
If you want to show the data of ID, calories, and name on the front-end (HTML), you can use tables. A simple example would be:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>Calories</th>
</tr>
<tr>
<td>You set the data here</td>
<td>..</td>
<td>..</td>
</tr>
<tr>
<td>..</td>
<td>..</td>
<td>..</td>
</tr>
</table>
</body>
</html>
I have a database schema that its name is "Navid"
there is many tables in this schema.
definitely each table, has some columns.
what I need is a java class that:
Connect to my database.
Have a method that loop on all tables
2-1. Have an inner loop to define all columns of the table.
Make create table query statement .(I want to create the same table in another database).
Execute that query.
I write some code but I do not know what to do next.
public class automateExport {
static String value;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// ResultSet rs = null;
String table_name;
String column_name;
String tableName = null;
StringBuilder sb = new StringBuilder(1024);
Connection DB2 = getConnection();
String sql = "SELECT TABSCHEMA,TABNAME,COLNAME FROM SYSCAT.COLUMNS WHERE TABSCHEMA NOT LIKE 'SYS%'";
PreparedStatement mainStmt = DB2.prepareStatement(sql);
ResultSet rs = mainStmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
/* while(rs.next()){
table_name = rs.getString(2);
// for(int i = 1; i <= 1; i ++){
column_name = rs.getString(3);
System.out.println("SSS::::: " + table_name + " " + column_name );
// }
*/
for (int i = 1; i <= rows; i++) {
while (rs.next()) {
table_name = rs.getString(2);
if (i > 1) {
sb.append(", ");
}
column_name = rs.getString(3);
String columnType = rsmd.getColumnTypeName(i);
sb.append(" ").append(column_name).append(" ").append(columnType);
int precision = rsmd.getPrecision(i);
if (precision != 0) {
sb.append("( ").append(precision).append(" )");
}
} // for columns
sb.append(" ) ");
String sql2 = sb.toString();
PreparedStatement m = DB2.prepareStatement(sql);
m.executeQuery();
System.out.println(sb.toString());
}
}
private static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver");
Connection connection
= DriverManager.getConnection("jdbc:db2://localhost:50000/navid", "navid", "oracle");
return connection;
}
}
I need help extracting all the file names within a folder in Business Objects. With the code I have now I can get the name of a single file within a folder. I want to get all the file names in that folder. The variable temp is where I enter the name of the file that I want. The variable doc is where the file name is retrieved, and the variable reportname is where I store doc to write to an external spreadsheet.
public class variable {
private static String expressionEx;
private static String nameEx;
private static String nameXE[] = new String[11];
private static String expressionXE[] = new String[11];
private static String query;
public static String reportName;
private static String reportPath;
/** This method writes data to new excel file * */
public static void writeDataToExcelFile(String fileName) {
String[][] excelData = preapreDataToWriteToExcel();// Creates first
// sheet in Excel
String[][] excelData1 = preapreDataToWriteToExcel1();// Creates
// second
// sheet in
// Excel
String[][] excelData2 = preapreDataToWriteToExcel2();// Creates third
// sheet in
// Excel
HSSFWorkbook myReports = new HSSFWorkbook();
HSSFSheet mySheet = myReports.createSheet("Variables");
HSSFSheet mySheet1 = myReports.createSheet("SQL Statement");
HSSFSheet mySheet2 = myReports.createSheet("File Info");
// edits first sheet
mySheet.setFitToPage(true);
mySheet.setHorizontallyCenter(true);
mySheet.setColumnWidth(0, 800 * 6);
mySheet.setColumnWidth(1, 7000 * 6);
// edits second sheet
mySheet1.setFitToPage(true);
mySheet1.setHorizontallyCenter(true);
mySheet1.setColumnWidth(0, 800 * 6);
mySheet1.setColumnWidth(1, 7000 * 6);
// edits third sheet
mySheet2.setFitToPage(true);
mySheet2.setHorizontallyCenter(true);
mySheet2.setColumnWidth(0, 800 * 6);
mySheet2.setColumnWidth(1, 2000 * 6);
HSSFRow myRow = null;
HSSFCell myCell = null;
// first sheet
for (int rowNum = 0; rowNum < excelData[0].length; rowNum++) {
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 4; cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData[rowNum][cellNum]);
}
}
try {
FileOutputStream out = new FileOutputStream(fileName);
myReports.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
// second sheet
for (int rowNum = 0; rowNum < excelData1[0].length; rowNum++) {
myRow = mySheet1.createRow(rowNum);
for (int cellNum = 0; cellNum < 4; cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData1[rowNum][cellNum]);
}
}
try {
FileOutputStream out = new FileOutputStream(fileName);
myReports.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
// third sheet
for (int rowNum = 0; rowNum < excelData2[0].length; rowNum++) {
myRow = mySheet2.createRow(rowNum);
for (int cellNum = 0; cellNum < 4; cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData2[rowNum][cellNum]);
}
}
try {
FileOutputStream out = new FileOutputStream(fileName);
myReports.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* #param args
*/
public static String password;
public static String username;
public static String temp;
public variable() throws FileNotFoundException {
// TODO Auto-generated method stub
IEnterpriseSession oEnterpriseSession = null;
ReportEngines reportEngines = null;
try {
// String cmsname = "det0190bpmsdev3";
// String authenticationType = "secEnterprise";
String cmsname = "";
String authenticationType = "";
// Log in.
oEnterpriseSession = CrystalEnterprise.getSessionMgr().logon(
username, password, cmsname, authenticationType);
if (oEnterpriseSession == null) {
} else {
// Process Document
reportEngines = (ReportEngines) oEnterpriseSession
.getService("ReportEngines");
ReportEngine wiRepEngine = (ReportEngine) reportEngines
.getService(ReportEngines.ReportEngineType.WI_REPORT_ENGINE);
IInfoStore infoStore = (IInfoStore) oEnterpriseSession
.getService("InfoStore");
String query = "select SI_NAME, SI_ID from CI_INFOOBJECTS "
+ "where SI_KIND = 'Webi' and SI_INSTANCE=0 and SI_NAME ='"
+ temp + "'";
IInfoObjects infoObjects = (IInfoObjects) infoStore
.query(query);
for (Object object : infoObjects) {
IInfoObject infoObject = (IInfoObject) object;
String path = getInfoObjectPath(infoObject);
if (path.startsWith("/")) {
DocumentInstance widoc = wiRepEngine
.openDocument(infoObject.getID());
String doc = infoObject.getTitle();
reportPath = path;// this is the path of document
$$$$$$$$$ reportName = doc;// this is the document name
printDocumentVariables(widoc);
purgeQueries(widoc);
widoc.closeDocument();
}
}
// End processing
}
} catch (SDKException sdkEx) {
} finally {
if (reportEngines != null)
reportEngines.close();
if (oEnterpriseSession != null)
oEnterpriseSession.logoff();
}
}
public static void printDocumentVariables(DocumentInstance widoc) {
int i = 0;
// this is the report documents variables
ReportDictionary dic = widoc.getDictionary();
VariableExpression[] variables = dic.getVariables();
for (VariableExpression e : variables) {
nameEx = e.getFormulaLanguageID();
expressionEx = e.getFormula().getValue();
// stores variables in array
nameXE[i] = nameEx;
expressionXE[i] = expressionEx;
i++;
}
}
public static String getInfoObjectPath(IInfoObject infoObject)
throws SDKException {
String path = "";
while (infoObject.getParentID() != 0) {
infoObject = infoObject.getParent();
path = "/" + infoObject.getTitle() + path;
}
return path;
}
#SuppressWarnings("deprecation")
public static void purgeQueries(DocumentInstance widoc) {
DataProviders dps = widoc.getDataProviders();
for (int i = 0; i < dps.getCount(); ++i) {
DataProvider dp = (DataProvider) dps.getItem(i);
if (dp instanceof SQLDataProvider) {
SQLDataProvider sdp = (SQLDataProvider) dp;
sdp.purge(true);
query = sdp.getQuery().getSQL();
}
}
If Temp is going to hold the name of a unique folder, then you can use this code. Replace the String query = line with:
IInfoObjects oParents = infoStore.query("select si_id from ci_infoobjects where si_kind = 'folder' and si_name = '" + temp + "'");
if(oParents.size()==0)
return; // folder name not found
IInfoObject oParent = (IInfoObject)oParents.get(0);
String query = "select SI_NAME, SI_ID from CI_INFOOBJECTS "
+ "where SI_KIND = 'Webi' and si_parentid = " + oParent.getID();
I don't see in the code where you're populating the temp, username, or password variables, but I assume you just left that out of the listing intentionally. Obviously they will need to be defined.
Couple of other things I noticed in your code:
You are purging the query in each report, but not saving the report afterwards.
You are storing the report's SQL in an instance variable named query, but you also have a local variable named query in the variable constructor. This could very likely cause a conflict if you need the SQL value for something.
You have an "empty catch" for SDKException. SDKExceptions can happen for all kinds of reasons, and as it is you will not know why the program failed. You should at least be printing out the exception, or just let it bubble up.
I keep getting assertion errors when I send this QUERY to the database even though when I manually enter in the query into both databases it return the correct data.
The only difference is the column title sown literally below(left is the MYSQL and right is MSSQL)
_____________________________________________
count(*) | (No Column Name)
_____________________________________________
43 | 43
|
Here is my JAVA Code
public class UnitTestDMUtility_Select {
#Test
public void testQUERY_CHECKBOTHPARTS() throws Exception{
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
PreparedStatement stmt = con.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
stmt.setInt(1, 101);
ResultSet sourceVal = stmt.executeQuery();
//Here is the QUERY
//select count(*) from tr_demand where demandtypeid=101
stmt = conTarget.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
stmt.setInt(1, 101);
ResultSet targetVal = stmt.executeQuery();
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));
}
}
package a7.unittests.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class UnitTestHelper {
public Connection getConnection(String url)throws Exception{
return DriverManager.getConnection(url);
}
/*
* Use for Insert and Update and Delete Statements
*/
public boolean resultSetsEqual4 (int source, int target){
System.out.println("The source is: "+source+" The target is: "+target);
if(((source>0)&&(target>0)&&(target==source)))
{
return true;
}
else
{
return false;
}
}
/*
* Used to test SELECT statements passing in objects
*/
public boolean resultSetsEqual2 (ResultSet source, ResultSet target) throws SQLException{
System.out.println("The source is: "+source+" The target is: "+target);
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(!source.getObject(i).equals(target.getObject(i)))
{return false;}
}
}
return true;
}
public boolean resultSetsEqual (ResultSet source, ResultSet target) throws SQLException{
System.out.println("The source is: "+source+" The target is: "+target);
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(source.getObject(i) != target.getObject(i))
{return false;}
}
}
return true;
}
public boolean resultSetsEqual3 (ResultSet rs1, ResultSet rs2) throws SQLException {
int col = 1;
//ResultSetMetaData metadata = rs1.getMetaData();
//int count = metadata.getColumnCount();
while (rs1.next() && rs2.next()) {
final Object res1 = rs1.getObject(col);
final Object res2 = rs2.getObject(col);
// Check values
if (!res1.equals(res2)) {
throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
res1, res2, col));
}
// rs1 and rs2 must reach last row in the same iteration
if ((rs1.isLast() != rs2.isLast())) {
throw new RuntimeException("The two ResultSets contains different number of columns!");
}
}
return true;
}
}