Java mysql resultSet错误

时间:2014-08-20 10:51:09

标签: java mysql resultset

好的,基本上我有这个代码:

    preparedStatement = connect
      .prepareStatement("select * from FEEDBACK.COMMENTS where NAZWISKO= ? ; ");
      preparedStatement.setString(1, surname3);

while (resultSet.next()) {

    String id = resultSet.getString("i

d");
          String user = resultSet.getString("IMIE");
          String website = resultSet.getString("NAZWISKO");
          String summary = resultSet.getString("ADRES");
          String date = resultSet.getString("EMAIL");
          String comment = resultSet.getString("TELEFON");
          String opisso = resultSet.getString("OPIS");


          JTextField myOutput = new JTextField(1600);
    myOutput.setText("id w bazie danych to " + id + " imie to " + user + " nazwisko to " + website + " adres to " + summary + " email to " + date + " teelefon to " + comment + " opis to " + opisso);



         add(myOutput);
    }

错误: 查询没问题,但错误出现在这里“while(resultSet.next())”

 SEVERE: null
java.lang.NullPointerException
    at jdbcexample.Main.readDataBase(Main.java:416)
    at jdbcexample.Main$7.mousePressed(Main.java:346)

我该如何解决?

2 个答案:

答案 0 :(得分:2)

你忘记了:

ResultSet resultSet = preparedStatement.executeQuery();

这就是为什么你的结果集是null,因为你没有用执行预准备语句返回的值初始化它。您的resultSet为null,导致访问空引用(null.next()),从而导致NullPointerException

答案 1 :(得分:0)

获取结果集,你必须执行查询

ResultSet rs = preparedStatement.executeQuery();