【问题标题】:Why do I get one extra result with null when using ResultSet?为什么我在使用 ResultSet 时会得到一个带有 null 的额外结果?
【发布时间】:2017-05-27 14:05:36
【问题描述】:

我在这里尝试做的是从 MySQL 数据库中读取一些数据并将它们转换为对象数据并将其序列化到文件中。显然,确实如此。但是我在控制台中得到一个空值,我无法理解它来自哪里。如果我检查错误本身,它会在这一行显示:

pepi = (personita) ois.readObject();

你得到的输出是:

Betty, Laferez, 87.0

Manolo,Lopez,45.0

Manuel, Rodriguez, 12.0

Patricia, Alonso, 12.0

那么,你能帮我理解为什么这条线会导致 null 问题吗?

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());
    }

}

}

【问题讨论】:

    标签: mysql resultset


    【解决方案1】:

    编辑 2 - 我改变了答案

    看完这篇Question

    您的代码是正确的。 您得到的 null 是 EOFException 捕获中的 e.getMessage() ,您可以通过用 ';' 替换 System.println 语句来避免它。

    }catch(EOFException e){
       ;
    }
    

    与我认为您没有在 steram 的 endo 读取空值相反,因此要知道您何时到达流的末尾,请使用此 EOFException。在我提到的问题中,需要考虑一下,因为您可以知道 EOF 引发是因为没有更多对象要读取,还是因为出现错误并且流已关闭。

    【讨论】:

    • 可以替换 System.out.println(e.getMessage());通过 e.printStackTrace();并告诉我它是否打印出不同的东西?
    • 使用我的原始代码: java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream 的 java.io.EOFException .readObject(Unknown Source) at Tema8.PracticaBDmasobjetosserializasdos.practicabdyserializableobjetos.main(practicabdyserializableobjetos.java:53) (第 53 行与我上面强调的相同)
    • 编辑 2:所以,我认为这是因为没有更多的对象可供阅读。然后,这似乎不是一个错误的代码,但我需要它来使它工作。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2015-02-13
    相关资源
    最近更新 更多