Clob to String to Clob + JTextArea [Resolvido][Dica]

0 respostas
germanosk

Olá pessoal

Vejo bastante gente com duvida em Clob eu também estava.
Gostaria de deixar umas dicas aqui de como eu resolvi alguns problemas
Primeiro String to Clob

String s = "String bem grande!";
Clob c = Hibernate.createClob(s);

E para ler string ou recuperar o conteúdo de um JTextArea eu criei a seguinte classe

import java.io.BufferedReader;
import java.sql.Clob;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 *
 * @author Germano Rodrigo Paiva de Assis
 */
public class LeitorClob {

    public String lerString(Clob arquivo){
        String aux = null;
        if (arquivo == null) {
        return "";
        }
        else {
            StringBuffer strOut = new StringBuffer();

            try {
                BufferedReader br = new BufferedReader(arquivo.getCharacterStream());
                while ((aux=br.readLine())!=null){
                strOut.append(aux);
                }
            }
            catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex);
            }
            aux = strOut.toString();
        }
        return aux;
    }
    
    public JTextArea lerTextArea(Clob arquivo,JTextArea strOut){

        String aux = null;
        if (arquivo == null) {
            return strOut;
        }
        else {
            try {
                BufferedReader br = new BufferedReader(arquivo.getCharacterStream());
                while ((aux=br.readLine())!=null){
                    strOut.append(aux+"\n");
                }
            }
            catch(Exception ex){
                JOptionPane.showMessageDialog(null, ex);
            }
        }
        return strOut;
    }
}

Espero ter ajudado alguém com isso :smiley: !
E se outra pessoa tiver uma solução melhor, favor postar ai!

Criado 8 de novembro de 2010
Respostas 0
Participantes 1