私がここでやろうとしているのは、MySQLデータベースからいくつかのデータを読み込み、それをオブジェクトデータに変換してファイルにシリアライズすることです。それは明らかにそうです。しかし、私はそれがどこから来るのか理解できないということは、コンソールではnullになります。私は、エラー自体をチェックする場合は、この行でそれを言う:あなたが得るResultSetを使用しているときに、なぜnullを伴う余分な結果が1つ得られますか?
pepi = (personita) ois.readObject();
出力は次のとおりです。
ベティ、Laferez、87.0 < - データベースから、[OK]をクリックします。
Manolo、Lopez、45.0 < - データベースから、
マヌエル、ロドリゲス、12.0 < - データベースより。
パトリシア、アロンソ、12.0 < - データベースから、ok。
ヌル< - ???
なぜこの回線がヌル問題を引き起こしているのか理解できたら助かりますか?
public class practicabdyserializableobjetos {
public static void main(String[] args) throws ClassNotFoundException, IOException {
Class.forName("com.mysql.jdbc.Driver");
Connection conexion = null;
try {
conexion = DriverManager.getConnection("jdbc:mysql://localhost/programacion", "root", "password");
Statement sentencia = (Statement) conexion.createStatement();
ResultSet resultado = sentencia.executeQuery("Select * from ejemplo");
File persofiles = new File("./persofiles.txt");
if (!persofiles.exists())
persofiles.createNewFile();
FileOutputStream fioust = new FileOutputStream(persofiles);
ObjectOutputStream oboust = new ObjectOutputStream(fioust);
int p=0;
while (resultado.next()) {
personita per = new personita();
per.setNombre(resultado.getString(1));
per.setApellido(resultado.getString(2));
per.setEdad(resultado.getDouble(3));
oboust.writeObject(per);
}
oboust.close();
fioust.close();
FileInputStream fis = new FileInputStream(persofiles);
@SuppressWarnings("resource")
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
personita pepi = new personita();
pepi = (personita) ois.readObject();
System.out.println(pepi.getNombre() + ", " + pepi.getApellido() + ", " + pepi.getEdad());
}
} catch (SQLException e) {
e.printStackTrace();
}
catch(EOFException e){
System.out.println(e.getMessage());
}
}
}
あなたが言ったようにしましたか?まったく同じエラー。 –
編集後、私はまだnullを取得します。 –
System.out.println(e.getMessage())を置き換えることはできますか? e.printStackTrace()によって;それが何か違うものを印刷するかどうか教えてください。私の元のコードで – Juan