Pessoal,
estou tentando implementar um programa que cria uma thread que por sua vez conecta no banco de dados, faz uma query e monta um ArrayList com os dados. Queria passar esses dados para o programa que “chamou” a thread, o programa compila mas quando eu executo eu tenho esse erro :
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at readStringArray.main(Unknown Source)
Ai vai o meu codigo :
import java.io.*;
import com.myown.utils.database.*;
import java.sql.*;
import java.util.*;
public class readStringArray {
public static void main(String[] args) throws Exception {
// array de bytes para receber o objeto.
byte[] receiver = new byte[2024];
PipedInputStream in = new PipedInputStream();
TProcessObjects tProcess = new TProcessObjects(in);
// create and start the thread.
Thread t1 = new Thread(tProcess);
t1.start();
// reads the PipedInputStream and set into receiver.
in.read(receiver);
// getting the object.
ObjectInputStream inObj = new ObjectInputStream(in);
ArrayList data = (ArrayList)inObj.readObject();
// stop thread.
t1.stop();
}
}
class TProcessObjects implements Runnable {
PipedOutputStream pOut;
byte[] objByte = new byte[2024];
Connection con = null;
Statement smt = null;
ResultSet rs = null ;
String[] fields = {"cod","nome","porcent"};
ArrayList array = new ArrayList();
public TProcessObjects(PipedInputStream pipeIn) throws Exception {
// link PipedOutputStream to PipedInputStream
pOut = new PipedOutputStream(pipeIn);
// create a db connection .
con = DBConnector.getConnection();
smt = con.createStatement();
}
public void run() {
try {
rs = smt.executeQuery("select * from estados");
// convert the ResultSet data to a String[][]
String[][] data = new ResultSet2ArrayString().convert(rs,fields);
// Arrat String to ArrayList.
for (int i = 0; i < data.length; i ++ ) { array.add(data[i]); }
ObjectOutputStream obj = new ObjectOutputStream(pOut);
// write my ArrayList array to ObjectArrayOutputStream.
obj.writeObject(array);
obj.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop() {
try {
rs.close();
smt.close();
con.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Alguem tem alguma ideia ?
Obrigado
//Daniel