I’m having trouble connecting SQL with java

Asked

Viewed 143 times

1

Well... I did some classes, and it was time to connect to search for a data in the bank; Ok. He had already connected with the BD, however, when I did the class to fetch the data, he keeps giving nullException, as if the bank did not exist. Loot:

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

Then I search like this:

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

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

Help me out, please.

  • Put the stacktrace

  • Put Error. Another way you are not closing connections.

1 answer

0


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

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

  1. conn has not been initialized correctly. If conn is null, then conn.createStatement will result in an NPE.
  2. rs is accessed before the assignment, for example in rs.TYPE_SCROLL_INSENSITIVE. If rs is null, you will get an NPE even if the attribute is static, so it is not good practice to access static constants in variables. Switch to ResultSet.TYPE_SCROLL_INSENSITIVE.

Browser other questions tagged

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