Не забываем полкючить jxl и oracle jdbc драйвер
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gethbnames;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Cell;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
*
* @author brudanovom
*/
public class GetHBNames {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {
// TODO code application logic here
Connection con = null;
Statement stmt = null;
ResultSet rset = null;
try {
/*Подключаемся и выполняем запрос*/
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.1:1521:SID","user","password");///необходимо в библиртеки проекта добавить драйвер
stmt = con.createStatement();
rset = stmt.executeQuery (
"select " +
"pc.l_name, pc.f_name, pc.m_name, pc.sex, pc.d_birth "
"from table1 pe " +
",table2 pc " +
"where 1=1 " +
"and pe.id = pc.id " +
"and pe.d_out>sysdate " +
")"
);
/*создаем EXCEL книгу*/
String fileName = "TodayNames.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
workbook.createSheet("Sheet1", 0);
WritableSheet excelSheet = workbook.getSheet(0);
/*Заполняем книгу из открытого oracle курсора*/
int i = 0,j = 0;
int rowcnt = rset.getMetaData().getColumnCount();
while (rset.next ())
{
for(j=0;j<=rowcnt-1;j++)
{ Label label = new Label(j, i, rset.getString(j+1));
excelSheet.addCell(label);
}
i++;
}
/*Закрываем книгу*/
workbook.write();
workbook.close();
} catch (WriteException | IOException | SQLException ex) {
Logger.getLogger(GetHBNames.class.getName()).log(Level.SEVERE, null, ex);
}
finally{ //В любом случае, даже при ошибке отключаемся от БД oracle
if (rset!=null){rset.close();}
if (stmt!=null){stmt.close();}
if (con!=null){con.close();}
System.out.println ("END");
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gethbnames;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Cell;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
*
* @author brudanovom
*/
public class GetHBNames {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {
// TODO code application logic here
Connection con = null;
Statement stmt = null;
ResultSet rset = null;
try {
/*Подключаемся и выполняем запрос*/
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.1:1521:SID","user","password");///необходимо в библиртеки проекта добавить драйвер
stmt = con.createStatement();
rset = stmt.executeQuery (
"select " +
"pc.l_name, pc.f_name, pc.m_name, pc.sex, pc.d_birth "
"from table1 pe " +
",table2 pc " +
"where 1=1 " +
"and pe.id = pc.id " +
"and pe.d_out>sysdate " +
")"
);
/*создаем EXCEL книгу*/
String fileName = "TodayNames.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
workbook.createSheet("Sheet1", 0);
WritableSheet excelSheet = workbook.getSheet(0);
/*Заполняем книгу из открытого oracle курсора*/
int i = 0,j = 0;
int rowcnt = rset.getMetaData().getColumnCount();
while (rset.next ())
{
for(j=0;j<=rowcnt-1;j++)
{ Label label = new Label(j, i, rset.getString(j+1));
excelSheet.addCell(label);
}
i++;
}
/*Закрываем книгу*/
workbook.write();
workbook.close();
} catch (WriteException | IOException | SQLException ex) {
Logger.getLogger(GetHBNames.class.getName()).log(Level.SEVERE, null, ex);
}
finally{ //В любом случае, даже при ошибке отключаемся от БД oracle
if (rset!=null){rset.close();}
if (stmt!=null){stmt.close();}
if (con!=null){con.close();}
System.out.println ("END");
}
}
}
Комментариев нет:
Отправить комментарий