Zapisanie wynikow rset do pliku Excel

0

Witam wszystkich
Korzystam z biblioteka JExcelApi do tworzenia plikow excel. Program ktory napisalam tworzy najpierw w pliku Excel naglowek w pierwszym wierszu , a nastepnie wykkonuje dwa zapytania: pierwsze wyciaga z bazy danych ile jest wierszy w danej tabeli, natomiast drugie zapytanie wyciaga konkretne dane z tabeli. Teraz chcialabym zapisac wynik drugiego zbioru rset do pliku w ktorym stworzylam naglowek.
Ale zamiast wpisywac mi tam wszystkie rozne rekordy to wpisuje mi tylko pierwszy tyle razy ile jest rekordow w tabeli. Bardzo prosze, niech ktos z Was rzuci okiem i powie mi gdzie mam blad.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

import javax.swing.JOptionPane;
import jxl.Cell.*;
import jxl.write.*;
import jxl.write.Number;

import jxl.*;

public class Rec_ex {
	
	
	 static double ID_TECHNOLOGII;
	 static double ID_MIESZANKI;
	 static double ID_RECEPTY;
	 static String CYKL;
	 static String NAWAZKA;
	 static String KOD_SUROWCA;
	 static String NAZWA_HANDLOWA;
	 static String WAGA;
	 static String DOKLADNOSC;
	 static String WAZENIE_NA;
	 static String ID_WAGI;
	 static String TYP_RECEPTY;
	 public PrintWriter wy1;
	 public PrintWriter wy;
	 public FileOutputStream strim;
	 public Calendar cal = Calendar.getInstance();
	 public java.sql.Date data = new java.sql.Date( cal.getTime().getTime() );
	 public String path ="C:\\Z4TECHNOLODZY\\widok_recepty\\all_"+data+".xls";
	 public WritableWorkbook workbook;
	 static int k;
	 static int ilosc;
	 //public WritableSheet sheet;
	 

	public static void main(String[] args) {
		Rec_ex log = new Rec_ex();
	}
	
	
	public Rec_ex(){
	

	try{
	 workbook = Workbook.createWorkbook(new File(path)); 
	WritableSheet sheet = workbook.createSheet("Recepty", 0); 
	
	WritableFont times10font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, true); 
	WritableCellFormat times10format = new WritableCellFormat (times10font); 
	
	Label label = new Label(0, 0, "ID_TECHNOLOGII",times10format); 
	sheet.addCell(label); 
	Label label1 = new Label(1,0, "ID_MIESZANKI",times10format); 
	sheet.addCell(label1);
	Label label2 = new Label(2,0, "ID_RECEPTY",times10format); 
	sheet.addCell(label2);
	Label label3 = new Label(3,0, "CYKL",times10format); 
	sheet.addCell(label3);
	Label label4 = new Label(4,0, "NAWAZKA",times10format); 
	sheet.addCell(label4);
	Label label5 = new Label(5,0, "KOD_SUROWCA",times10format); 
	sheet.addCell(label5);
	Label label6 = new Label(6,0, "NAZWA HANDLOWA",times10format); 
	sheet.addCell(label6);
	Label label7 = new Label(7,0, "WAGA",times10format); 
	sheet.addCell(label7);
	Label label8 = new Label(8,0, "DOKLADNOSC",times10format); 
	sheet.addCell(label8);
	Label label9 = new Label(9,0, "WAZENIE_NA",times10format); 
	sheet.addCell(label9);
	Label label10 = new Label(10,0, "ID_WAGI",times10format); 
	sheet.addCell(label10);
	Label label11 = new Label(11,0, "TYP_RECEPTY",times10format); 
	sheet.addCell(label11);
	
	//workbook.write();
	//workbook.close();
	
	Connection conn;
	Statement stmt;
	Connection conn2;
	Statement stmt2;
	String driver = "jdbc:oracle:thin:";
	String host = "****";
	String port = "1521";
	String SERVICE_NAME = "****";
	String uzyt = "****";
	String password = "****";
	String zlicz = "SELECT COUNT(*) FROM RECEPTY.v_namiarownia_all ";
	String sql = "SELECT ID_TECHNOLOGII,ID_MIESZANKI,ID_RECEPTY" +
			",CYKL,NAWAZKA,KOD_SUROWCA,NAZWA_HANDLOWA,WAGA,DOKLADNOSC,WAZENIE_NA," +
			"ID_WAGI,TYP_RECEPTY FROM RECEPTY.v_namiarownia_all";

	ResultSet rset;
	ResultSet rset2;
	String connString2 = driver + "@" + host + ":" + port + ":"
	+ SERVICE_NAME;
	
	try {
		DriverManager
				.registerDriver(new oracle.jdbc.driver.OracleDriver());
		conn2 = DriverManager.getConnection(connString2, uzyt,password);
		stmt2 = conn2.createStatement();
		rset2 = stmt2.executeQuery(zlicz);
		//rset2 = stmt.executeQuery(zlicz);
         
   

		while (rset2.next()) {
			
			k = rset2.getInt(1);
			ilosc = k;
		
		}
	
		rset2.close();
		stmt2.close();
		conn2.close();
	}
		
		catch (SQLException eve) {
			JOptionPane.showMessageDialog(null,eve.getMessage());
			
		}
		
	String connString = driver + "@" + host + ":" + port + ":"
				+ SERVICE_NAME;

		try {
			DriverManager
					.registerDriver(new oracle.jdbc.driver.OracleDriver());
			conn = DriverManager.getConnection(connString, uzyt,password);
			stmt = conn.createStatement();
			rset = stmt.executeQuery(sql);
		


			while (rset.next()) {
				
               
				ID_TECHNOLOGII = rset.getDouble(1);
					
				ID_MIESZANKI=rset.getDouble(2);
				
				ID_RECEPTY=rset.getDouble(3);
				
				//MIESZANKA=rset.getObject(4);
				
				//OLD_MIESZANKA=rset.getObject(5);
				
				//NUMER_RECEPTY=rset.getString(6);
				
				CYKL=rset.getString(4);
				
				NAWAZKA=rset.getString(5);
				
				KOD_SUROWCA=rset.getString(6);
				
				NAZWA_HANDLOWA=rset.getString(7);
				
				WAGA=rset.getString(8);
				
				DOKLADNOSC=rset.getString(9);
				
				WAZENIE_NA=rset.getString(10);
				
				ID_WAGI=rset.getString(11);
				
				//WAGA_AUTOMATYCZNA=rset.getObject(15);
				
				TYP_RECEPTY=rset.getString(12);
				
		
	               for(int i = 1;i<=ilosc;i++){
					
					
					Number number = new Number(0, i, ID_TECHNOLOGII); 
					sheet.addCell(number);
					
					
					//workbook.write();
						
				}
			
			}
		
			rset.close();
			stmt.close();
			conn.close();
		}
			
			catch (SQLException eve) {
				JOptionPane.showMessageDialog(null,eve.getMessage());
				
			}
	
			workbook.write();
			workbook.close();

	}
	catch(IOException e)
	{
		
	}
	catch(WriteException w)
	{
		
	}
	
	

}

}

Z gory bardzo dziekuje i pozdrawiam.

0

Przepraszam jesli komus zabralam czas, ale juz mnie olsnilo :) Zamiast wyciagac dane na temat tego ile jest rekordow, i uzywac petli for na koncu zbioru rset, zrobilam cos znacznie prostszego. Nie uzywam ilosci rekordow tylko ustawiam zmienna i ktora zwiekszam po kazdym elemencie rseta. W tej petli zapisuje wartosci na wybranej kolumnie i aktualnej pozycji wartosci i. Tyle dziala jak chcialam [browar]

Pozdrawiam

0

Cieszę się że sobie poradziłeś sam. Na przyszłość radzę użyć najpierw debugera ;) a dopiero potem atakować innych swoim długim kodem źródłowym.

1 użytkowników online, w tym zalogowanych: 0, gości: 1