I have written the below Java code, which is executing fine in order to export the results in xlsx file from Oracle DB. But can someone help me to improve the performance of this code by which this below code can extract the result very fast.
Below is the Java Code which I have tried as of now; But it is slow while exporting result as compared to excel export from SQL Developer
public class FilesFromFolder {
private Workbook writeWorkbook;
public void ExportService(DBConnection con) {
writeWorkbook = new XSSFWorkbook();
Sheet desSheet = writeWorkbook.createSheet("Data");
Statement stmt = null;
ResultSet rs = null;
int columnsNumber = 0;
ResultSetMetaData rsmd = null;
FileOutputStream fileOut = null;
Connection cntn = null;
String filePath = "C:\\Users\\Desktop\\OracleExport";
File files = new File(filePath);
File[] file = files.listFiles();
String fileNameWithOutExt = null;
if (file != null) {
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
String tempFilename = file[i].getName();
fileNameWithOutExt = tempFilename.replaceFirst("[.][^.]+$", "");
File fileTemp = file[i];
try {
String fileContent = FileUtils.readFileToString(fileTemp, "UTF8");
// System.out.println(fileContent);
cntn = con.getConnection();
stmt = cntn.createStatement();
rs = stmt.executeQuery(fileContent);
rsmd = rs.getMetaData();
columnsNumber = rsmd.getColumnCount();
Row desRow1 = desSheet.createRow(0);
for (int col = 0; col < columnsNumber; col++) {
Cell newpath = desRow1.createCell(col);
newpath.setCellValue(rsmd.getColumnLabel(col + 1));
}
while (rs.next()) {
System.out.println("Row number -->" + rs.getRow());
Row desRow = desSheet.createRow(rs.getRow());
for (int col = 0; col < columnsNumber; col++) {
Cell newpath = desRow.createCell(col);
newpath.setCellValue(rs.getString(col + 1));
}
String outputFile = "C:\\Users\\Desktop\\OracleExport\\" + fileNameWithOutExt
+ ".xlsx";
fileOut = new FileOutputStream(outputFile);
writeWorkbook.write(fileOut);
}
System.out.println(fileNameWithOutExt + " export complete");
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (fileOut!= null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (cntn != null) {
con.closeConnection();
}
}
}
}
}
}
}
Related
I want to print the result set obtained from JDBC connection and want it to print each result set in aloop.So my code is below:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;
public class zone {
List<model> listOfBlogs;
public zone() {
Connection conn = null;
listOfBlogs = new ArrayList<model>();
// connect to the database
conn = connectToDatabaseOrDie();
// get the data
populateListOfTopics(conn, listOfBlogs);
//print the data
printTopics(listOfBlogs);
}
public List<model> printTopics(List<model> listOfBlogs) {
Iterator<model> it = listOfBlogs.iterator();
while (it.hasNext()) {
model blog = (model) it.next();
System.out.println(blog.day + " " + blog.hour + " " + blog.zone_id);
}
return listOfBlogs;
}
private void populateListOfTopics(Connection conn, List<model> listOfBlogs) {
try {
for (int j = 9; j < 10; j++) {
for (int i = 10; i < 12; i++) {
String sql = "SELECT day,hour,zone_id FROM public.zone_block_table where day=? and hour=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, j);
pstmt.setInt(2, i);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
model blog = new model();
blog.day = rs.getInt("day");
blog.hour = rs.getInt("hour");
blog.zone_id = rs.getInt("zone_id");
listOfBlogs.add(blog);
}
convertToCsv(rs, i, j);
rs.close();
pstmt.close();
}
}
} catch (SQLException se) {
System.err.println("Threw a SQLException creating the list of state.");
System.err.println(se.getMessage());
} catch (Exception e) {
System.out.println("Err");
e.printStackTrace();
}
}
private Connection connectToDatabaseOrDie() {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/mvc_data_base";
conn = DriverManager.getConnection(url, "postgres", "122333");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void convertToCsv(ResultSet rs, int k, int l) throws SQLException, FileNotFoundException {
PrintWriter csvWriter = new PrintWriter(new File("state_" + k + "_" + l + ".csv"));
ResultSetMetaData meta = rs.getMetaData();
int numberOfColumns = meta.getColumnCount();
String dataHeaders = "\"" + meta.getColumnName(1) + "\"";
for (int i = 2; i < numberOfColumns + 1; i++) {
dataHeaders += ",\"" + meta.getColumnName(i) + "\"";
}
csvWriter.println(dataHeaders);
while (rs.next()) {
String row = "\"" + rs.getString(1) + "\"";
for (int i = 2; i < numberOfColumns + 1; i++) {
row += ",\"" + rs.getString(i) + "\"";
}
csvWriter.println(row);
}
csvWriter.close();
}
}
But the resultset is printing perfectly.But I could not find any csv files generated in any of the destination.Any help is appreciated.
There is a Direct method to create CSV file from resultset using opencsv. Try that
CSVWriter writer = new CSVWriter(new FileWriter(csvtemp), ','
,CSVWriter.NO_QUOTE_CHARACTER , CSVWriter.NO_ESCAPE_CHARACTER , "\r\n");
writer.writeAll(myResultSet, true);
writer.close();
Quick questions...
I'm trying to make a Dynamic JTree but I can't get to put every database I have into one single node for each one. This is my code so far:
jTree2 = new javax.swing.JTree();
try {
String DSN = "jdbc:mysql://localhost";
String user = "root";
String password = "";
conexion = DriverManager.getConnection(DSN, user, password);
}
catch(Exception e) {
System.out.println("ERROR");
}
try {
sentencia = conexion.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
catch(Exception e) {
System.out.println("ERROR2");
}
try {
String hi = "";
ResultSet rs1 = conexion.getMetaData().getCatalogs();
ResultSetMetaData rsmd = rs1.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs1.next()) {
for (int i = 1; i <= columnCount; i++ ) {
hi = hi + rs1.getString(i) + ", ";
}
//for
String sb = hi.substring(0, hi.length()-2);
jTree2.setModel(new FileSystemModel(new File(sb)));
}
}
catch(Exception ae) {
System.out.println("ERROR3");
}
jScrollPane3.setViewportView(jTree2);
And the result I get is this:
Every database is splitted by a "," but I want them to be on a single node for each one. Any help?
This should do it for you:
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Databases", true);
while (rs1.next()) {
for (int i = 1; i <= columnCount; i++) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(rs1.getString(i), true);
parent.add(node);
}
jTree2.setModel(new DefaultTreeModel(parent));
}
I need to embed files
(.ppt(x), .doc(x), .jpg, .txt,...)
in excel (format xlsx) using Java Apache POI.
I have found an example to embed files in excel (format xls) using
POI-HSSF
(Embed files into Excel using Apache POI),
but this example does not work with excel xlsx format.
Does somebody know if it is possible to do this using POI-XSSF ?
'try this it works for me...' use this jars to run this code.....
1) poi-3.13-20150929.jar
2) poi-ooxml-3.13-20150929.jar
3) poi-ooxml-schemas-3.13-20150929.jar
public String getReport() throws RecordNotFoundException{
ResultSet rs = null;
ResultSet rs1 = null;
Connection conn = null;
CallableStatement cstm = null;
PreparedStatement pstmt = null;
try {
HttpServletRequest request;
HttpSession session;
request = (HttpServletRequest) ActionContext.getContext().get(
"com.opensymphony.xwork2.dispatcher.HttpServletRequest");
session = request.getSession();
conn = JndiConnection.getOracleConnection();
String sReportName = "";
ArrayList<String> mainData = new ArrayList<String>();
conn = JndiConnection.getOracleConnection();
cstm = conn.prepareCall("your procedure name");
cstm.setString(1, first parameter);
cstm.setString(2, second parameter);
cstm.registerOutParameter(3, OracleTypes.CURSOR);
cstm.execute();
rs1 = (ResultSet) cstm.getObject(3);
ResultSetMetaData rsmd = rs1.getMetaData();
String sColumnData="Sr.No";
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
sColumnData+="~~" + rsmd.getColumnName(i);
}
String headerString=sColumnData.replace("Sr.No~~", "");
mainData.add(headerString);
while (rs1.next()) {
System.out.println("data...............::" + rs1.getString(1));
String sData = "data";
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String column=rs1.getString(rsmd.getColumnName(i));
if (column == null) {
String finalData = "";
sData += "~~" + finalData;
} else {
sData += "~~" + rs1.getString(rsmd.getColumnName(i));
}
}
String bodyData=sData.replace("data~~", "");
mainData.add(bodyData);
System.out.println();
}
System.out.println("main data...............::" + mainData);
session.setAttribute("myReportData", mainData);
sReportName = genReport(mainData, title in file, "file path");
if (!"".equals(sReportName)) {
session.setAttribute("myReportDataExcelFile", sReportName);
}
setFileInputStream(new BufferedInputStream(new FileInputStream(sReportName)));
setFileName(new File(sReportName).getName());
} catch (SQLException se) {
throw new RecordNotFoundException(se);
} catch (NestableException ne) {
throw new RecordNotFoundException(ne);
} catch (Exception e) {
throw new RecordNotFoundException(e);
} finally {
try {
JndiConnection.freeConnections(cstm, conn, rs, rs1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return SUCCESS;
}"
'Above method will get the from database and put it into the list, now use bellow method
to write xls file with POI HSSF.'
private String genReport(ArrayList<String> arrDisplayDataList, String FileName, String sTitleName, String sPath)
throws Exception {
String fileNameWithPath;
Exception exception;
int col = 0;
int sno = 1;
fileNameWithPath = "";
String sFromate = "";
Connection con = null;
String ColumnDataList[] = null;
System.out.println("::::: Inside genReport() 2:::::");
try {
String[] arrColumnList = arrDisplayDataList.get(0).split("~~");
System.out.println("Length:::" + arrColumnList.length);
if (arrColumnList.length > 1) {
System.out.println("==Inside Of Record Data===");
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyyHHmmss");
Calendar cal = Calendar.getInstance();
sFromate = dateFormat.format(cal.getTime());
FileName = (new StringBuilder()).append(FileName).append("_").append(sFromate).toString();
fileNameWithPath = (new StringBuilder()).append(sPath).append("/").append(FileName).append(".xls")
.toString();
File f1 = new File((new StringBuilder()).append(sPath).append("/").append(FileName).append(".xls")
.toString());
f1.getParentFile().mkdirs();
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream(f1);
HSSFSheet sheets = wb.createSheet("Sheet" + (sno++));
int row = 1;
HSSFRow rows;
if (row == 1) {
rows = sheets.createRow(0);
HSSFCell cells = rows.createCell(0);
cells.setCellType(1);
HSSFCellStyle cellStyle1 = wb.createCellStyle();
cellStyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cells.setCellStyle(cellStyle1);
cells.setCellValue(sTitleName);
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
fontStyle.setColor(IndexedColors.BLACK.getIndex());
fontStyle.setBoldweight((short) 700);
fontStyle.setFontHeightInPoints((short) 12);
cellStyle.setFillPattern((short) 1);
cellStyle.setFont(fontStyle);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cells.setCellStyle(cellStyle);
sheets.addMergedRegion(new CellRangeAddress(0, 0, 0, arrColumnList.length - 1));
for (int columnIndex = 0; columnIndex < 50; columnIndex++) {
sheets.autoSizeColumn(columnIndex);
}
}
rows = sheets.createRow(row++);
for (col = 0; col < arrColumnList.length; col++) {
HSSFCell cells = rows.createCell(col);
String sVal = (String) arrColumnList[col];
cells.setCellType(1);
cells.setCellValue(sVal);
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
fontStyle.setColor(IndexedColors.BLACK.getIndex());
fontStyle.setBoldweight((short) 700);
fontStyle.setFontHeightInPoints((short) 10);
cellStyle.setFont(fontStyle);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cells.setCellStyle(cellStyle);
for (int columnIndex = 0; columnIndex < 100; columnIndex++) {
sheets.autoSizeColumn(columnIndex);
}
}
int rownum = 2;
int rowcount = 0;
for (int i = 1; i < arrDisplayDataList.size(); i++) {
if (rownum >= 65000) {
sheets = wb.createSheet("Sheet " + (sno++));
rownum = 0;
}
rows = sheets.createRow(rownum++);
String sData = ((String) arrDisplayDataList.get(i)).toString();
String sSplitData[] = sData.split("~~");
int xcount = 0;
for (col = 0; col < sSplitData.length; col++) {
HSSFCell cells = rows.createCell(xcount++);
String sVal = sSplitData[col];
if (sVal.equalsIgnoreCase("Total")) {
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
fontStyle.setColor(IndexedColors.BLACK.getIndex());
fontStyle.setBoldweight((short) 600);
fontStyle.setFontHeightInPoints((short) 12);
cellStyle.setFont(fontStyle);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// cells.setCellStyle(cellStyle);
cells.setCellType(1);
for (int k = 3; k < rows.getLastCellNum(); k++) {// For
// each
// cell
// in
// the
// row
rows.getCell(k).setCellStyle(cellStyle);// Set
// the
// sty;e
}
cells.setCellValue(sVal);
System.out.println("TTT:" + sVal);
} else {
/*
* HSSFCellStyle cellStyle = wb.createCellStyle();
* cellStyle
* .setBorderBottom(HSSFCellStyle.BORDER_THIN);
* cellStyle
* .setBorderTop(HSSFCellStyle.BORDER_THIN);
* cellStyle
* .setBorderRight(HSSFCellStyle.BORDER_THIN);
* cellStyle
* .setBorderLeft(HSSFCellStyle.BORDER_THIN);
* //cells.setCellStyle(cellStyle);
* cells.setCellType(1);
*/
cells.setCellValue(sVal);
}
}
}
wb.write(fileOut);
fileOut.close();
} else {
fileNameWithPath = "NO RECORD FOUND";
}
} catch (Exception e) {
System.out.println("Exception ::::" + e);
e.printStackTrace();
throw e;
}
return fileNameWithPath;
}
I am trying to write some query results in a csv file.
I thought it was working fine, until I saw the last line of the file after it's been written:
value;nettingNodeData;BLE57385;CVAEngine;BLE;;.4;;BDR;NA;ICE;;RDC;;CVAEngine;;4841263;RDC
value;ne
The part where I am writing the file :
public int getLeNodes(String runId, File file) {
Connection connect = null;
PreparedStatement ps = null;
ResultSet rs = null;
int lines = 0;
try {
connect = newConnection();
ps = connect.prepareStatement(HIER_NTT.replace("?", runId));
ps.setFetchSize(1500);
rs = ps.executeQuery();
lines += nnpw.writeCore(file, rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs, ps, connect);
}
return lines;
}
public int writeCore(File file, ResultSet rs) throws FileNotFoundException, IOException {
int count = 0;
try {
BufferedWriter output = new BufferedWriter(new FileWriter(file, true));
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
while (rs.next()) {
for (int col = 1; col <= colCount; col++) {
try {
String val = rs.getString(col);
if (rs.wasNull()) {
val = "";
}
output.append(val);
} catch (ArrayIndexOutOfBoundsException e) {
String dec = rs.getBigDecimal(col).toPlainString();
System.err.println(String.format("%s %d %s %s %d %s %d", rs.getString(1), col,
rsmd.getColumnTypeName(col), file, count, dec, dec.length()));
output.append(dec);
}
if (col < colCount) {
output.append(";");
}
}
output.newLine();
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
And the SQL (HIER_NTT):
select 'value', 'nettingNodeData', 'BLE' || d.deal_numadm,
'CVAEngine', 'BLE', '', (1-ntt.lgd_cp), '', 'BDR', 'NA', 'ICE', '', 'RDC', '',
'CVAEngine', '', d.ntt_id, 'RDC' from ntt ntt
join deals d on d.ntt_id = ntt.ntt_id and d.deal_scope='Y'
join dt_runs r on r.run_id = ntt.run_id
where r.run_id=? and d.deal_numadm!=0 group by d.deal_numadm, d.deal_nummas, d.ntt_id, ntt.lgd_cp
So, why does my file end abruptly like this?
You should call output.close() when you are done writing to the file. The missing output is probably still in the buffer when the java process exits
I have a oracle(10.2) PLSQL procedure which fetches 15 records from a table in a sysrefcursor.
I then pass this cursor to a java class as a resultset. This java class is loaded to oracle.
Driver name : Oracle JDBC driver
Driver Version : 10.2.0.1.0
Driver Major Version : 10
Driver Minor Version : 2
Observations:
1 Inside the java class, when I iterate through the resultset I get only the first 10 records.
2 If the cursor fetched (20 or more records) or (10 or less) I could get all the records while iterating the resultset.
3 I found that the default fetchsize for the resultset is 10. If I change the fetchSize to 5, and the cursor fetches 8 records, I could get the first 5 records while iterating the resultset.
4 If the cursor fetched (10 or more records) or (5 or less) I could get all the records while iterating the resultset.
5 If I change the resultset fetchSize to 1, I could get all the records in the resultset no matter how many records are fetched by the cursor.
Why is the resultset behaving weirdly?
public static BLOB createZip(BLOB prevBlob, String outPrefix, ResultSet entries, ResultSet rs, Long[] resultRows) throws Exception
{
OracleConnection conn = null;
BLOB retBLOB = null;
int page = 1;
int curRow = 0;
long totalRows = 0;
try
{
conn = (OracleConnection) new OracleDriver().defaultConnection();
ArrayList entryList = loadEntries(entries);
retBLOB = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);
retBLOB.open(BLOB.MODE_READWRITE);
OutputStream bOut = retBLOB.setBinaryStream(0L);
ZipOutputStream zipOut = new ZipOutputStream(bOut);
PrintStream out = new PrintStream(zipOut);
zipOut.putNextEntry(new ZipEntry(outPrefix + "-p" + page + ".csv"));
writeHeader(out, entryList);
while (rs.next())
{
curRow++;
totalRows++;
if (curRow >= maxPageSize)
{
zipOut.closeEntry();
page++;
zipOut.putNextEntry(new ZipEntry(outPrefix + "-p" + page + ".csv"));
writeHeader(out, entryList);
curRow = 0;
}
for (int i = 0; i < entryList.size(); i++)
{
Entry e = (Entry) entryList.get(i);
if (i != 0)
{
out.print(",");
}
if (e.isEscape())
out.print("\"" + escapeExcel(rs.getString(e.getColumn())) + "\"");
else
out.print("\"" + emptyExcel(rs.getString(e.getColumn())) + "\"");
}
out.println();
}
if (totalRows == 0)
{
out.println("\"No Entries Found\"");
}
resultRows[0] = new Long(totalRows);
out.flush();
zipOut.closeEntry();
if (prevBlob != null)
{
byte[] buf = new byte[1024];
InputStream bIn = prevBlob.binaryStreamValue();
ZipInputStream zipIn = new ZipInputStream(bIn);
ZipEntry inEntry = zipIn.getNextEntry();
while (inEntry != null)
{
zipOut.putNextEntry(new ZipEntry(inEntry.getName()));
int len;
while ((len = zipIn.read(buf)) > 0) {
out.write(buf, 0, len);
}
inEntry = zipIn.getNextEntry();
}
zipIn.close();
try
{
prevBlob.freeTemporary();
}
catch (SQLException e) { }
}
zipOut.close();
retBLOB.close();
return retBLOB;
}
catch (Exception sex)
{
if (retBLOB != null)
{
try
{
retBLOB.freeTemporary();
}
catch (SQLException e) { }
}
throw sex;
}
finally
{
try { entries.close(); } catch (SQLException sex) { }
try { rs.close(); } catch (SQLException sex) { }
try
{
if (conn != null || !conn.isClosed())
{
conn.close();
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
There is a workaround. I fetched the column index before while(rs.next()).
The below snippet works absolutely fine for me. But I still fail to understand why resultSet.getString(columnName); failed inside while(rs.next()).
ArrayList entryList = loadEntries(entries);
int[] colIndx = new int[entryList.size()];
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
for (int i = 0; i < entryList.size(); i++){
Entry e = (Entry) entryList.get(i);
for (int j = 1; j <= numberOfColumns; j++){
if(rsmd.getColumnName(j).equalsIgnoreCase(e.getColumn()))
colIndx[i] = j;
}
}
try{
while (rs.next()){
for (int i = 0; i < colIndx.length ; i++){
System.out.println("Column Values["+colIndx[i]+"] : "+rs.getString(colIndx[i]));
}
}
}