Ich habe ein Beispiel für einen Datenexport ins Excelformat bei RoseIndia gefunden und das auf meine Datenbank übertragen. Aber mehr als den SQL Query bekomme ich nicht ausgegeben. Obwohl die Instanz
existiert, wird sie aber nicht erstellt.
Habt ihr einen Rat?
Code:
new Workbook()
Java:
import java.io.*;
import java.sql.*;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class ExcelFile {
public static Connection con;
public static void openUserConnection() throws Exception {
try {
Class.forName("org.sqlite.JDBC");
DriverManager.registerDriver(new org.sqlite.JDBC());
con = DriverManager.getConnection ("jdbc:sqlite:Dberfassendb.db");
int level = Connection.TRANSACTION_SERIALIZABLE;
con.setTransactionIsolation(level);
System.out.println("2. geschafft");
}catch (SQLException e) {
String msg = "Es konnte keine Verbindung zur Datenbank hergestellt werden.\n"
+ "Die Applikation wird nun geschlossen.";
JOptionPane.showMessageDialog(null, msg, "Datenbank nicht gefunden!", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
private static void disconnet() {
if(con!=null);
}
public static void main(String[] args) {
String sql;
ResultSet rs;
Statement stmnt;
System.out.println("1. geschafft");
try {
openUserConnection();
stmnt = con.createStatement();
sql = "Select zeit, ort, strasse, info from tour ";
System.out.println(sql);
rs = stmnt.executeQuery(sql);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Zeit");
rowhead.createCell((short) 1).setCellValue("Ort");
rowhead.createCell((short) 2).setCellValue("Strasse");
rowhead.createCell((short) 8).setCellValue("Info");
int index = 1;
while (rs.next()) {
System.out.println("3. geschafft");
HSSFRow row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(rs.getInt(1));
row.createCell((short) 1).setCellValue(rs.getString(2));
row.createCell((short) 2).setCellValue(rs.getString(3));
row.createCell((short) 3).setCellValue(rs.getInt(4));
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\excelFile.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("Data is saved in excel file.");
rs.close();
stmnt.close();
disconnet();
} catch (Exception e) {
}
}
}
Habt ihr einen Rat?