public class JDBCAdapter extends AbstractTableModel
TableModel
interface. This class attempts to abstract the details
of the JDBC DB connection and query specifics. It presents all the results to
the user in the form of a Java TableModel
by extending/overiding
the AbstractTableModel
. This an then be used to as the basis for
a JTable.
JDBCAdapter jdbcTableAdapter;
private String dbVendorParam = "oracle", // this can be oracle or db2
dbNameParam = "someDB", // this is the name of the DB to hit
dbUsernameParam = "myUsername", // duh
dbPasswordParam = "myPassword, // duh
dbServerParam = "172.118.88.2", // the name or IP address of the server runing the DB
dbPortParam = "1521"; // the port to use for JDBC access (oracle does not have a seperate listening port it uses the main port)
// see the vendor docs for the specific port to access
boolean dbConnected = false;
String sampleQuery = "SELECT * from employee";
TableSorter dataSorter = new TableSorter();
Table table;
try {
jdbcTableAdapter = new JDBCAdapter(dbVendorParam, dbNameParam, dbUsernameParam, dbPasswordParam, dbServerParam, dbPortParam);
dbConnected = true;
}
catch (java.security.AccessControlException ex) {
System.out.println("Security Exception Connecting to " + dbVendorParam +"'s Database." );
dbConnected = false;
}
// retrieve data from the database
if (jdbcTableAdapter.isDbConnected()) {
System.out.println("Retrieve Raw data from the database");
jdbcTableAdapter.executeQuery(sampleQuery);
// now get the table set up.
dataSorter.setModel(jdbcTableAdapter);
table = new JTable(dataSorter);
}
AbstractTableModel
,
Serialized FormModifier and Type | Field and Description |
---|---|
private String[] |
columnNames
Description of the Field
|
private Connection |
connection
The JDBC Connection used for queries.
|
private String |
db2DriverName
Description of the Field
|
private String |
dbConnectString
Description of the Field
|
private String |
dbName
Description of the Field
|
private String |
dbPassword
Description of the Field
|
private String |
dbPort
Description of the Field
|
private String |
dbServer
Description of the Field
|
private String |
dbUsername
Description of the Field
|
private String |
dbVendor
Description of the Field
|
private String |
driverName
Description of the Field
|
private boolean |
errorCondition
Description of the Field
|
private String |
JDBCconnectString
Description of the Field
|
private String |
JDBCconnectString2
Description of the Field
|
private ResultSetMetaData |
metaData
Description of the Field
|
private String |
oracleDriverName
Description of the Field
|
private ResultSet |
resultSet
Description of the Field
|
private Vector |
rows
Description of the Field
|
private Statement |
statement
Description of the Field
|
listenerList
Constructor and Description |
---|
JDBCAdapter(JDBCAdapter jdbcAdapter)
Copy constructor for this class.
|
JDBCAdapter(String url,
String driverName,
String user,
String passwd)
This constructor takes a URL (in the form of a String) as well as the Drivername,
username, password and then creates the Adapter.
|
JDBCAdapter(String dbV,
String dbN,
String dbU,
String dbPass,
String dbS,
String dbP)
Main Constructor for the class.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
This method performs some clean up for the class.
|
String |
dbRepresentation(int column,
Object value)
Provides a method to get the Object data into a String form so it can be put
into the DB.
|
boolean |
executeQuery(String query)
This method takes a String and executes as an SQL statement on the connected
Database.
|
protected void |
finalize()
This method is here to ensure that we exit cleanly from the JDBC connections
that might have been made.
|
Class |
getColumnClass(int column)
This method returns the Java Class type held in the specified column. it will
be String.class, Boolean.class, Integer.class etc. this method is for the
Implementation of the TableModel Interface
|
int |
getColumnCount()
Provides and accessor for the number of columns in the table model. this method
is for the Implementation of the TableModel Interface
|
String |
getColumnName(int column)
Returns a String holding the name of the column name for the column number
that was passed in as a parameter this method is for the Implementation of
the TableModel Interface
|
private String |
getdbName()
Get method for this field
|
private String |
getdbPassword()
Get method for this field
|
private String |
getdbPort()
Get method for this field
|
private String |
getdbServer()
Get method for this field
|
private String |
getdbUsername()
Get method for this field
|
private String |
getdbVendor()
Get method for this field
|
boolean |
getErrorCondition()
Gets the ErrorCondition attribute of the JDBCAdapter object
|
int |
getRowCount()
Provides and accessor for the number of rows in the table model. this method
is for the Implementation of the TableModel Interface
|
protected Vector |
getRows()
Get method for this field
|
Object |
getValueAt(int aRow,
int aColumn)
Provides and accessor for the object at a specific cell in the table model.
|
boolean |
isCellEditable(int row,
int column)
This tells you if the specified cell is editable(ie writeable). this method
is for the Implementation of the TableModel Interface
|
boolean |
isDbConnected()
Tells whether there is an active and usable DB connection
|
void |
setErrorCondition(boolean val)
Sets the ErrorCondition attribute of the JDBCAdapter object
|
void |
setValueAt(Object value,
int row,
int column)
Sets the value at the specified cell in the DB to the object supplied.
|
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
private Connection connection
private String driverName
private String JDBCconnectString
private String JDBCconnectString2
private String dbConnectString
private String oracleDriverName
private String db2DriverName
private String[] columnNames
private ResultSetMetaData metaData
private String dbUsername
private String dbPassword
private boolean errorCondition
public JDBCAdapter(String dbV, String dbN, String dbU, String dbPass, String dbS, String dbP)
dbV
- DB VendordbN
- DB NamedbU
- UsernamedbPass
- PassworddbS
- The DB Server Name.dbP
- The JDBC Port on the DB Serverpublic JDBCAdapter(JDBCAdapter jdbcAdapter)
jdbcAdapter
- Description of Parameterpublic JDBCAdapter(String url, String driverName, String user, String passwd)
url
- Description of ParameterdriverName
- Description of Parameteruser
- Description of Parameterpasswd
- Description of Parameterpublic void setErrorCondition(boolean val)
val
- The new ErrorCondition valuepublic void setValueAt(Object value, int row, int column)
setValueAt
in interface TableModel
setValueAt
in class AbstractTableModel
value
- The new ValueAt valuerow
- The new ValueAt valuecolumn
- The new ValueAt valuepublic boolean getErrorCondition()
public boolean isDbConnected()
public String getColumnName(int column)
getColumnName
in interface TableModel
getColumnName
in class AbstractTableModel
column
- Description of Parameterpublic Class getColumnClass(int column)
getColumnClass
in interface TableModel
getColumnClass
in class AbstractTableModel
column
- Description of Parameterpublic boolean isCellEditable(int row, int column)
isCellEditable
in interface TableModel
isCellEditable
in class AbstractTableModel
row
- Description of Parametercolumn
- Description of Parameterpublic int getColumnCount()
public int getRowCount()
public Object getValueAt(int aRow, int aColumn)
aRow
- Description of ParameteraColumn
- Description of Parameterpublic boolean executeQuery(String query)
query
- Description of Parameterpublic void close()
public String dbRepresentation(int column, Object value)
column
- Description of Parametervalue
- Description of Parameterprotected void finalize() throws Throwable
private String getdbVendor()
private String getdbName()
private String getdbUsername()
private String getdbPassword()
private String getdbServer()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses