在mysql数据库中更新数据到表失败

时间:2012-09-28 19:28:53

标签: java mysql netbeans jdbc

以下代码未显示任何错误且代码已执行,但计算并存储在rfv中的数据未在数据库中更新。

try{
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  com.mysql.jdbc.Connection con = (com.mysql.jdbc.Connection)  DriverManager.getConnection("jdbc:mysql://localhost:3308/rk","root","root");
  ResultSet rsfact=null;
  PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact");
  PreparedStatement psgraph=(PreparedStatement) con.prepareStatement("INSERT INTO graph values(?,?,?,?)");
  int cp,ic,rc,fi,ct,tr;
  while(rsfact.next())
  {
      cp=rsfact.getInt("1");
      ic=rsfact.getInt("2");
      rc=rsfact.getInt("3");
      fi=rsfact.getInt("4");
      ct=rsfact.getInt("5");
      tr=rsfact.getInt("6");
      int rfv;

      rfv=(cp+ic+rc+fi+ct+tr)/6;
      graph.setInt(1, rfv);
      graph.executeUpdate();
  }
 }
 catch(Exception e){
 System.out.println(e);
 }

我检查了代码,一切似乎对我都没问题,但我无法弄清楚未插入表中的数据的原因。 执行此操作时,我得到以下内容

显示java.lang.NullPointerException

 try{
   Class.forName("com.mysql.jdbc.Driver").newInstance();
    com.mysql.jdbc.Connection con = (com.mysql.jdbc.Connection)        DriverManager.getConnection("jdbc:mysql://localhost:3308/rk","root","root");
   ResultSet rsfact=null;
  ResultSet rsfault=null;
  ResultSet rspriority=null;
  ResultSet rsreq=null;
  PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact");
  PreparedStatement psfault=(PreparedStatement) con.prepareStatement("SELECT * FROM fault");
  PreparedStatement pspriority=(PreparedStatement) con.prepareStatement("SELECT * FROM priority");
  PreparedStatement psreq=(PreparedStatement) con.prepareStatement("SELECT * FROM req");
  PreparedStatement graph=(PreparedStatement) con.prepareStatement("INSERT INTO gr values(?)");
  int cp,ic,rc,fi,ct,tr;
  rsfact = psfact.executeQuery();
  rsfault=psfault.executeQuery();
  rspriority=pspriority.executeQuery();
  rsreq=psreq.executeQuery();
  while(rsfact.next())
  {
      cp=rsfact.getInt(1);
      ic=rsfact.getInt(2);
      rc=rsfact.getInt(3);
      fi=rsfact.getInt(4);
      ct=rsfact.getInt(5);
      tr=rsfact.getInt(6);
      int rfv;

      rfv=(cp+ic+rc+fi+ct+tr)/6;
      graph.setInt(1, rfv);
      graph.executeUpdate();
  }
  while(rsfault.next()){
  cp=rsfact.getInt(1);
      fic=rsfact.getInt(2);
      frc=rsfact.getInt(3);
      ffi=rsfact.getInt(4);
      fct=rsfact.getInt(5);
      ftr=rsfact.getInt(6);
      int tsfv;

      tsfv=((fcp*2)+(fic*3)+(frc*4)+(ffi*3.14)+(fct-4)+(ftr+9))/6;
      graph.setInt(2, tsfv);
      graph.executeUpdate();

  }
 }

1 个答案:

答案 0 :(得分:1)

您没有实例化rsfact(ResultSet对象)并尝试调用结果集的方法,因此 NULLPOINTEREXCEPTION

    ResultSet rsfact=null; // **rsfact is null**
       int colCount=1;
      PreparedStatement psfact=(PreparedStatement) con.prepareStatement("SELECT * FROM fact");
      PreparedStatement psgraph=(PreparedStatement) con.prepareStatement("INSERT INTO graph values(?,?,?,?)");
      int cp,ic,rc,fi,ct,tr;
      rsfact = psFact.executeQuery(); **// this instantiates ResultSet, if you dont includee this statement rsfact would still be null.**
while(rsfact.next()) // **if rsfact null and u execute this line it would throw nullpointer exception(cuz rsfact is null)**
  {
      cp=rsfact.getInt("1");
      ic=rsfact.getInt("2");
      rc=rsfact.getInt("3");
      fi=rsfact.getInt("4");
      ct=rsfact.getInt("5");
      tr=rsfact.getInt("6");
      int rfv;

      rfv=(cp+ic+rc+fi+ct+tr)/6;
      graph.setInt(colCount, rfv);
       colCount++;
}
      graph.executeUpdate();