radish
radish

Reputation: 115

updating of data to table in mysql database failed

The following code doesn't show any error and the code executes but the data that is calculated and stored in rfv is not updated in the database.

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

I checked the code and everything seems to be fine for me but i couldn't figure out the reason for the data which is not inserted into the table. I get the following when this is executed

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

  }
 }

Upvotes: 0

Views: 656

Answers (1)

PermGenError
PermGenError

Reputation: 46428

you are not instatiating rsfact(ResultSet object) and trying to invoke methods of result set thus 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();

Upvotes: 1

Related Questions