Link to home
Start Free TrialLog in
Avatar of Cyart
CyartFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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();
    }
  }
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What is 'value' when it contains a date?
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
Avatar of Cyart

ASKER

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.

Have you written a custom TableModel?
Avatar of Cyart

ASKER


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

}
As what type is your date column stored in the RS?
Avatar of Cyart

ASKER

java.sql.Date
In setValueAt, i see no attempt to locate the row that's to be updated in the RS ...
Avatar of Cyart

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cyart

ASKER

Sorry forgot to close