I'm having trouble connecting to SQL with java

1

Well ... I did some classes, and it's time to connect to get a die in the bank; Okay. He had already connected with the DB, but when I did the class to fetch the data, it gets nullException, as if the database did not exist. Take out:

public void executeSQL(String sql){
        try {
            stm = conn.createStatement(rs.TYPE_SCROLL_INSENSITIVE,rs.CONCUR_READ_ONLY);
            rs = stm.executeQuery(sql);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro:" + e);
        }
    }

Here I look like this:

csql.executeSQL("select * from usuarios");

then the error occurs (But of course I instantiated);

Help me, please.

    
asked by anonymous 22.03.2016 / 02:03

1 answer

0

NPE means that some variable is not initialized properly. If you had said which line the problem would be, it would be easier to figure out which invalid reference.

If the error is actually in the above code (which obviously is incomplete), I would say it could be as follows:

  • conn was not initialized correctly. If conn is null , then conn.createStatement will result in an NPE.
  • rs is accessed prior to the assignment, for example in rs.TYPE_SCROLL_INSENSITIVE . If rs is null , you will get a NPE even if the attribute is static, so it is not good practice to access static constants in variables. Swap with ResultSet.TYPE_SCROLL_INSENSITIVE .
  • 23.03.2016 / 02:31