Olá eu tenho esse código q esta dando uns erros…
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.*;
public class Relatorio extends JFrame {
public Connection con = null;
public Statement stm = null;
public ResultSet rs;
String nome = "";
public Relatorio(){
}
public void adicionaString(){
String nome = "Ricardo";
String snome = "Marzano";
String fim = String.format("%-4s\t %-4s\n",nome, snome);
}
public void setNome(String n){
nome = n;
}
public void salvar(){
String teste = "", t = "";
String nc = "";
float vlr = 0;
String venc = "";
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/dbsgod", "root", "");
stm = con.createStatement();
rs = stm.executeQuery("select * from contaextra");
while(rs.next()){
nc = rs.getString(2);
vlr = rs.getFloat(3);
venc = rs.getString(4);
teste = String.format("%-4s\t %4.2f\t %-4s\n", nc, vlr, venc);
System.out.println(teste);
}//fim while
}//fim try
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Apresente este erro\nao desenvolvedor do sistema.","E R R O",JOptionPane.INFORMATION_MESSAGE);
//e.printStackTrace();
}//fim catch
try{
File f = new File("c:\teste\relatorio.txt");
FileWriter fw = new FileWriter(f);
fw.write("Cod\tNome\tSobrenome \n\n");
fw.write(teste);
fw.close();//
} //fim 2º try
catch (Exception e){
System.out.println("Erro ao gravar no aquivo");
}//fim 2º catch
}//fim metodo salvar
public Formatter arqS;
public void salvar2(){
Relatorio rel = new Relatorio();
rel.setNome("Ricardo");
try{
arqS = new Formatter("c:\teste\relat.txt");
rel.setNome("Ricardo");
arqS.format("%s", rel);
} catch (Exception e){
}
}
public static void main (String args[]) throws FileNotFoundException {
Relatorio a = new Relatorio();
a.salvar();//é o 1º metodo
}
}
e da o seguinte:
- quando ele le no while do banco e imprime na tela pega tudo
- na hora de gravar no arquivo ele só grava o ultimo registro
e essa string formata fica ok…
Por favor se alguem souber onde esta o erro e puder me ajudar OK.
Obrigado por enquanto
Abraços
Ricardo