Cyart
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();
}
}
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();
}
}
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
http://javaalmanac.com/egs/java.text/ParseDate.html
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.
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?
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.isClos
{
rsConnection.closeConnecti
}
}
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.isClos
{
return meta.getColumnCount () ;
}
else
{
JOptionPane.showMessageDia
+ "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.isClos
{
return rowCount ;
}
else
{
JOptionPane.showMessageDia
+ "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.isClos
{
rs.absolute (row + 1) ;
return rs.getObject (column + 1) ;
}
else
{
JOptionPane.showMessageDia
+ "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(quer
if(!rsConnection.conn.isCl
{
meta = rs.getMetaData () ;
System.out.println(meta) ;
rs.last () ;
rowCount = rs.getRow () ;
}
}
catch (SQLException e)
{
// Could not connect to the database
/*JOptionPane.showMessageD
+ "was unsuccessful.",
"Database Connection Closed",
JOptionPane.ERROR_MESSAGE)
System.out.println(e.getMe
}
catch (NullPointerException n)
{
/*JOptionPane.showMessageD
+ "was unsuccessful.",
"Database Connection Closed",
JOptionPane.ERROR_MESSAGE)
System.out.println(n.getMe
}
}
//get the name of the selected column
public String getColumnName(int column) throws IllegalStateException{
try
{
if(!rsConnection.conn.isCl
{
return meta.getColumnName(column + 1);
}
else
{
JOptionPane.showMessageDia
+ "closed unexpectedly.",
"Database Connection Closed", JOptionPane.ERROR_MESSAGE)
}
}
catch(SQLException sqlException)
{
sqlException.printStackTra
}
return null;
}
//get the column data type
public Class getColumnClass(int column) throws IllegalStateException
{
try
{
if (!rsConnection.conn.isClos
{
String classname = meta.getColumnClassName (column + 1) ;
return Class.forName (classname) ;
}
else
{
JOptionPane.showMessageDia
+ "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?
ASKER
java.sql.Date
In setValueAt, i see no attempt to locate the row that's to be updated in the RS ...
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)
if((tempCol == 4) || (tempCol == 5))
{
rs.updateString (tempCol, String.valueOf (value)) ;
}
else if (tempCol == 6)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry forgot to close