Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

ResultSet change date

hello,

I have been able to change string values in a ResultSet to automatically update the MySQL db, but I cannot change a date field. The date field does not seem editable, how is this implemented correctly I was trying to use this but to no joy

 public void setValueAt(Object value, int row, int col)
  {

    try
    {
    int tempCol = col ;
    tempCol ++ ;
    if((tempCol == 4) || (tempCol == 5))
    {
      rs.updateString (tempCol, String.valueOf (value)) ;
    }
    else
      rs.updateDate (tempCol, Date.valueOf (String.valueOf(value))) ;
   
      rs.updateRow(  );

    }
    catch (SQLException s)
    {
      s.printStackTrace();
    }
  }
0
Cyart
Asked:
Cyart
  • 5
  • 5
1 Solution
 
CEHJCommented:
What is 'value' when it contains a date?
0
 
objectsCommented:
If you date string is not in the expected format then you will need to use SimpleDateFormat to  parse it.

http://javaalmanac.com/egs/java.text/ParseDate.html
0
 
CyartAuthor Commented:
Hello,

The object that is passed back is a java.sql.Date object, but when it is displayed in the Jtable it appears as dd-mmm-yyyy, so there is some sort of conversion going on behind the scenes. If Cast to char in the java sql statement it brings it back as yyyy-mm-dd, but I recieve resultset is not editable error.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
Have you written a custom TableModel?
0
 
CyartAuthor Commented:

Please find attached the two classes I am using its the editableResults class that is giving me the date trouble




class DBResultSet extends AbstractTableModel
{
  protected ResultSet rs  ;
  DBConnection rsConnection;
  protected ResultSetMetaData meta;
  protected int rowCount;
  boolean closing = false;


//close the database connection
  void closeConnection ()
  {
    closing = true;
    try
    {
      if (!rsConnection.conn.isClosed ())
      {
        rsConnection.closeConnection () ;
      }
    }
    catch(SQLException e)
    {
      e.printStackTrace();
    }
  }


  public DBResultSet(String query)
  {
    rsConnection = new DBConnection();
//Execute the database query (connection is opened during DBConnection.getquery)
    getQuery(query);
  }

//required for extending abstractTableModel
  //get the number of columns in the resultset
  public int getColumnCount() throws IllegalStateException
  {
    try
    {
      if (!rsConnection.conn.isClosed ())
      {
        return meta.getColumnCount () ;
      }
      else
        {
          JOptionPane.showMessageDialog(null, "The database connection has been"
              + "closed unexpectedly.",
              "Database Connection Closed", JOptionPane.ERROR_MESSAGE);
        return -1;
        }

    }
    catch(SQLException e)
    {
      e.printStackTrace();
    }
    catch (NullPointerException n)
    {

    }
    return 0;
  }

//required for extending abstractTableModel
  //get the number of rows in the resultset
  public int getRowCount() throws IllegalStateException
  {
    if (!closing)
    {
      try
      {
        if (!rsConnection.conn.isClosed ())
        {
          return rowCount ;
        }
        else
        {
          JOptionPane.showMessageDialog(null, "The database connection has been"
              + "closed unexpectedly.",
              "Database Connection Closed", JOptionPane.ERROR_MESSAGE);
          return -1;
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace () ;
      }
      catch (NullPointerException n)
      {
        return -1;
      }
    }
    return 0;
  }


//required for extending abstractTableModel
//get the value of a specific cell in the resultset
  public Object getValueAt(int row, int column)
      throws IllegalStateException
  {
    try
    {
      if (!rsConnection.conn.isClosed())
      {
        rs.absolute (row + 1) ;
        return rs.getObject (column + 1) ;
      }
      else
        {
          JOptionPane.showMessageDialog(null, "The database connection has been"
              + "closed unexpectedly.",
              "Database Connection Closed", JOptionPane.ERROR_MESSAGE);
        }

    }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    return "error";
  }

//execute the database query
  protected void getQuery (String query)
  {
    try
    {

      rs = rsConnection.getQuery(query);//create connection in dbconnection.getQuery
      if(!rsConnection.conn.isClosed())
      {
        meta = rs.getMetaData () ;
        System.out.println(meta) ;
        rs.last () ;
        rowCount = rs.getRow () ;
      }
    }
    catch (SQLException e)
    {
      // Could not connect to the database
      /*JOptionPane.showMessageDialog(null, "The database connection attempt "
                              + "was unsuccessful.",
                              "Database Connection Closed",
                              JOptionPane.ERROR_MESSAGE);*/
      System.out.println(e.getMessage());
    }
    catch (NullPointerException n)
    {
    /*JOptionPane.showMessageDialog(null, "The database connection attempt "
                                  + "was unsuccessful.",
                                  "Database Connection Closed",
                                  JOptionPane.ERROR_MESSAGE);*/
    System.out.println(n.getMessage());
  }
  }

//get the name of the selected column
  public String getColumnName(int column) throws IllegalStateException{
  try
    {
      if(!rsConnection.conn.isClosed())
      {
      return meta.getColumnName(column + 1);
      }
      else
        {
          JOptionPane.showMessageDialog(null, "The database connection has been"
              + "closed unexpectedly.",
              "Database Connection Closed", JOptionPane.ERROR_MESSAGE);
        }
    }
    catch(SQLException sqlException)
    {
      sqlException.printStackTrace();
    }
    return null;
  }

//get the column data type
  public Class getColumnClass(int column) throws IllegalStateException
  {
    try
    {
      if (!rsConnection.conn.isClosed ())
      {
        String classname = meta.getColumnClassName (column + 1) ;

        return Class.forName (classname) ;
      }
      else
        {
          JOptionPane.showMessageDialog(null, "The database connection has been"
              + "closed unexpectedly.",
              "Database Connection Closed", JOptionPane.ERROR_MESSAGE);
        }
    }
    catch (Exception exception)
    {
      exception.printStackTrace () ;
    }
    return Object.class;
  }

}








public class EditableResultSet extends DBResultSet
{
  public EditableResultSet (String query)
  {
    super (query) ;
  }

  public boolean isCellEditable (int row, int col)
  {
    return true ;
  }

//think this method will just update the result set
  public void setValueAt(Object value, int row, int col)
  {
    String temp = "" ;
    try
    {
    int tempCol = col ;
    tempCol ++ ;
    if((tempCol == 4) || (tempCol == 5))
    {
      rs.updateString (tempCol, String.valueOf (value)) ;
    }
    else if (tempCol == 6)
    {
     
      //rs.updateDate(6,thiscal);
     
    }

      rs.updateRow(  );

    }
    catch (SQLException s)
    {
      s.printStackTrace();
    }
  }

}
0
 
CEHJCommented:
As what type is your date column stored in the RS?
0
 
CyartAuthor Commented:
java.sql.Date
0
 
CEHJCommented:
In setValueAt, i see no attempt to locate the row that's to be updated in the RS ...
0
 
CyartAuthor Commented:
I did not need to for the this part it knew the row already as confirmed by checking the db just can't make the java.sql.Date field editable.

if((tempCol == 4) || (tempCol == 5))
    {
      rs.updateString (tempCol, String.valueOf (value)) ;
     
    }
    else if (tempCol == 6)
0
 
CEHJCommented:
>>//rs.updateDate(6,thiscal);

What does 'thiscal' refer to?
0
 
CyartAuthor Commented:
Sorry forgot to close
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now