如何从多行 PostgreSQL ResultSet 中获取一行并将其存储在 Java 中?

How to get one row from a multiple row PostgreSQL ResultSet and store it in Java?

我的函数访问 PostgreSQl 数据库,returns 1 行并将列的值存储在我的 jogo 对象中。它工作得很好。代码:

public Jogo BuscaJogoId(int x){
    Jogo jogo = new Jogo(null, null);
    String sql = "SELECT * FROM jogo WHERE id = " + x;
    try {
        ResultSet rs = db.ExecutaBusca(sql);
        //rs.next();
        jogo.setId(rs.getInt("id"));
        jogo.setTitulo(rs.getString("titulo"));
        jogo.setCapa(rs.getString("capa"));
        jogo.setPreco(rs.getFloat("preco"));
        jogo.setNota(rs.getFloat("nota"));
        jogo.setDescricao(rs.getString("descricao"));
        jogo.setFotos(StringToArray(rs.getString("fotos")));
        jogo.setQtd(rs.getInt("qtd"));
        jogo.setChaves(StringToArray(rs.getString("chaves")));
        jogo.setGenero(rs.getString("generos"));
        jogo.setIdade(rs.getString("idade"));
        System.out.println("sucesso ao acessar o BD");
        return jogo;
    } catch (Exception e) {
        System.out.println("falha ao acessar o BD");
        e.printStackTrace();
        return null;
    }
}

然后我创建了另一个类似于那个的函数,但是现在结果集 returns 3 行而不是 1 行。另外,我将 jogo 更改为 jogo[i] 这样我就可以return 一个数组。问题是:第一行捕获了一个异常。新功能代码:

public Jogo[] BuscaJogoGenero(String str){
    int limit = 3;
    Jogo[] jogo = new Jogo[limit];
    String sql = "SELECT * FROM jogo WHERE generos LIKE '%"+str+"%' ORDER BY qtd DESC LIMIT "+limit;
    ResultSet rs = db.ExecutaBusca(sql);
    int i = 0;
    try{
        while(i!=limit){
            jogo[i].setId(rs.getInt("id")); //Line 118 where is the problem
            jogo[i].setTitulo(rs.getString("titulo"));
            jogo[i].setCapa(rs.getString("capa"));
            jogo[i].setPreco(rs.getFloat("preco"));
            jogo[i].setNota(rs.getFloat("nota"));
            jogo[i].setDescricao(rs.getString("descricao"));
            jogo[i].setFotos(StringToArray(rs.getString("fotos")));
            jogo[i].setQtd(rs.getInt("qtd"));
            jogo[i].setChaves(StringToArray(rs.getString("chaves")));
            jogo[i].setGenero(rs.getString("generos"));
            jogo[i].setIdade(rs.getString("idade"));
            i++;
        }
    } catch (Exception e){
        System.out.println("DAO failed");
        e.printStackTrace();
    }
    return jogo;
}

异常:

 [java] java.lang.NullPointerException
 [java] DAO failed
 [java] 
 [java]     at dao.JogoDAO.BuscaJogoGenero(JogoDAO.java:118)
 [java]     at service.JogoService.GeraJogo(JogoService.java:77)
 [java]     at controller.FXMLDocumentController.CarregarJogoGenero(FXMLDocumentController.java:399)
 [java]     at controller.FXMLDocumentController.CarregarTelaProduto(FXMLDocumentController.java:365)
 [java]     at controller.FXMLDocumentController.ClicouJogo(FXMLDocumentController.java:418)
 [java]     at controller.FXMLDocumentController.clicouPopular3(FXMLDocumentController.java:291)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
 [java]     at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
 [java]     at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
 [java]     at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
 [java]     at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
 [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
 [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
 [java]     at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
 [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
 [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 [java]     at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
 [java]     at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
 [java]     at javafx.event.Event.fireEvent(Event.java:198)
 [java]     at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
 [java]     at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3398)
 [java]     at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
 [java]     at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
 [java]     at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
 [java]     at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
 [java]     at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
 [java]     at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
 [java]     at com.sun.glass.ui.View.notifyMouse(View.java:941)
 [java]     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
 [java]     at com.sun.glass.ui.win.WinApplication.lambda$null(WinApplication.java:185)
 [java]     at java.lang.Thread.run(Thread.java:748)
 [java] Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 [java]     at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
 [java]     at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
 [java]     at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
 [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
 [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
 [java]     at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
 [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
 [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 [java]     at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
 [java]     at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
 [java]     at javafx.event.Event.fireEvent(Event.java:198)
 [java]     at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
 [java]     at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3398)
 [java]     at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
 [java]     at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
 [java]     at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
 [java]     at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
 [java]     at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
 [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
 [java]     at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
 [java]     at com.sun.glass.ui.View.notifyMouse(View.java:941)
 [java]     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
 [java]     at com.sun.glass.ui.win.WinApplication.lambda$null(WinApplication.java:185)
 [java]     at java.lang.Thread.run(Thread.java:748)
 [java] Caused by: java.lang.reflect.InvocationTargetException
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
 [java]     at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
 [java]     at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
 [java]     ... 31 more
 [java] Caused by: java.lang.NullPointerException
 [java]     at controller.FXMLDocumentController.CarregarTelaProduto(FXMLDocumentController.java:366)
 [java]     at controller.FXMLDocumentController.ClicouJogo(FXMLDocumentController.java:418)
 [java]     at controller.FXMLDocumentController.clicouPopular3(FXMLDocumentController.java:291)
 [java]     ... 41 more

我不知道怎么了。我该如何解决?

PS.: SQL 字符串都是正确的并且 return 是我想要的。

您没有迭代 ResultSet。您需要对其调用 next()。 你最好返回 List<Jogo>。所以:

List<Jogo> results = new ArrayList<>;
while rs.next() {
   Jogo j = new Jogo();
   results.add(j);
   // Set all the attributes of 'j'
}
// return 'results' (List<Jogo>)

并在 try-with-resources 块中获取 ResultSet