How to list data from a Mysql database using the Java List (jdbc) method?

Asked

Viewed 4,099 times

0

I’m having trouble trying to list the data.

Exception in thread "main" java.lang.Nullpointerexception at modelo.Dao.getLista(Dao.java:111) at principal.Main.listarViagem(Main.java:161) at principal.Main.menu(Main.java:58) at principal.Main.main(Main.java:39) ()

My codes:

Class

 public List<Bean> getLista() {
     try {
         List<Bean> viagens = new ArrayList<Bean>();
 
         PreparedStatement stmt = ConexaoMySQL.
                 prepareStatement("select * from viagem");
         ResultSet rs = stmt.executeQuery();
 
         while (rs.next()) {
             // criando o objeto viagem
             Bean viagem = new Bean();
             viagem.setIdViagem(rs.getInt("idViagem"));
             
             Calendar dataInicio = Calendar.getInstance();
             dataInicio.setTime(rs.getDate("dataInicio"));
             viagem.setDataInicio(dataInicio);
             
             Calendar dataF = Calendar.getInstance();
             dataF.setTime(rs.getDate("dataFim"));
             viagem.setDataFim(dataF);
             
             viagem.setCidade(rs.getString("cidade"));
             viagem.setUf(rs.getString("uf"));
             viagem.setValorDiaria(rs.getDouble("valorDiaria"));
             viagem.setColaborador(rs.getString("colaborador"));
             viagem.setCliente(rs.getString("cliente"));
 
             // adicionando o objeto à lista
             viagens.add(viagem);
         }
         rs.close();
         stmt.close();
         return viagens;
     } catch (SQLException e) {
         throw new RuntimeException(e);
     }
   }
 }

Main class

private static void listarViagem() {
    Dao dao = new Dao();
            
    List<Bean> viagens = dao.getLista();
            
    for (Bean contato : viagens) {
        System.out.println("IdViagem: " + contato.getIdViagem());
        System.out.println("tipo: " + contato.getTipo());
        System.out.println("Data de inicio: " + 
                              contato.getDataInicio().getTime() + "\n");
        System.out.println("Data de fim: " + 
                          contato.getDataFim().getTime() + "\n");
        System.out.println("cidade: " + contato.getCidade());
        System.out.println("uf: " + contato.getUf());
        System.out.println("valor da diaria: " + contato.getValorDiaria());
        System.out.println("colaborador: " + contato.getColaborador());
        System.out.println("cliente: " + contato.getCliente());
    
    
    }    
}
            

Is there a simpler way to list the data using jdbc in java ? What’s the simplest way to do this?

  • Is making a mistake?

  • 1

    for your code in main and DAO to become simpler, you can create a Bean constructor by passing the resultset as parameter to set the attributes, and you can also override the toString method, that your for would be only System.out.println(contact)

  • By the console is giving an error in the getLista method.

  • Post your Stacktrace @Jarwin

  • Exception in thread "main" java.lang.Nullpointerexception at modelo.Dao.getLista(Dao.java:111) at principal.Main.listarViagem(Main.java:161) at principal.Main.menu(Main.java:58) at principal.Main.main(Main.java:39)

  • I just wanted a List method that listed my data to complete my CRUD '-'

  • And which line would be 111 from the DAO?

  • Resultset rs = stmt.executeQuery();

  • how you are implementing Conexaomysql?

  • http://pastebin.com/eVsmD2JR

  • 1

    @Jarwin your way out prepareStatement("select * from viagem"); returns null so there is exception when trying to execute stmt.executeQuery();

  • The compiler tells me to create a prepareStatement method in my mysql connection class, there is another way to do my dao method ?

Show 7 more comments

1 answer

0


Solution of my problem:

I had to set my Conexaomysql before Preparedstatement so it gave error. Now compiled :-)

ConexaoMySQL.conectar();

          PreparedStatement stmt = ConexaoMySQL.getConexao()
                  .prepareStatement("select * from viagem ");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.