| Previous | Table of Contents | Next |
rollbackTransaction
The rollbackTransaction method works similarly to the commitTransaction() method. If this method is successful, the current transaction rolls back and all changes done within the scope of the transaction are undone.
public boolean rollbackTransaction()
{
boolean rollbackIsOk = false;
try
{
//Check if auto commit is on. If it is, dont roll back.
rollbackIsOk = !connection.getAutoCommit();
if(rollbackIsOk)
{
// rollback transaction
connection.rollback();
//Turn on Auto Commit; this is default.
connection.setAutoCommit(true);
}
}catch(SQLException e)
{
System.out.println(e);
rollbackIsOk = false;
}
//refresh
if(rollbackIsOk)
this.executeQuery();
return rollbackIsOk;
}
}
Miscellaneous TableRelated Methods
The following are housekeeping methods required by the JDBCTableModel.
The close method is called to close the connection to the database and all associated resources.
public void close() throws SQLException
{
resultSet.close();
statement.close();
connection.close();
}
The close method is called from finalize(), which has been overridden to call close.
protected void finalize() throws Throwable
{
close();
super.finalize();
}
The getColumnName method is a convenience method that returns the name of a column based on its positional index.
public String getColumnName(int col)
{
String retVal;
retVal = (String) names.elementAt(col);
if(retVal == null)
retVal = ;
return retVal;
}
The method isCellEditable returns true if the column is one of the first five. This method is a sort of hack, so we dont have to add too much code for processing the logic of each individual column. The intent is to keep the example straightforward:
public boolean isCellEditable(int row, int col)
{
/*
Only the first four columns after the product id are
updateable in this example (adjust for index difference).
*/
return ((col < 6) && (col > 0));
}
The getColumnCount method returns the number of columns in the table.
public int getColumnCount()
{
return names.size();
}
The getRowCount method returns the number of rows in the table.
public int getRowCount()
{
return data.size();
}
The getValueAt method returns the value of a specific cell of the table.
public Object getValueAt(int row, int col)
{
Vector rowData = (Vector)data.elementAt(row);
return rowData.elementAt(col);
}
WindowCloser
WindowCloser is used to respond to a windowClosing event. It quits the application.
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}
This example highlights the basic use of JDBC 1.0. Sun has recently released the specification for JDBC 2.0, which is even more powerful. The next section highlights some of the features that will be available when JDBC 2.0 product is released in full.
JDBC 2.0 adds many new features to the overall architecture of JDBC. When drivers start supporting these features, database access activities will be even easier and more flexible. In general, JDBC 2.0 supports the goals of JDBC 1.0, which include ease of use and good integration with the rest of the Java 2 platform, and Java philosophy of software design.
The specific goals of JDBC 2.0 include the following:
JDBC 2.0 is designed so that JDBC 1.0 programmers can easily adopt its functionality. JDBC 2.0 extends the capabilities of JDBC 1.0.
There are many improvements in the JDBC 2.0 specification. The most notable improvements include these:
JDBC 2.0 is more powerful because it allows you more flexibility in how you access and manipulate data. It is also easy to use because it simply extends the features of JDBC 1.0, which means everything you have learned here will apply to JDBC 2.0. However, there are some design changes, as discussed in the next section.
| Previous | Table of Contents | Next |