ram817
asked on
update database based on edited JTable
Help! I would like to be able to edit my JTable and update the database based on the changes made in the JTable. I know that I need an update sql statement but how do I know that what I'm passing to the database are actually the changes made in the JTable? I am quite lost and don't know where to start. I'd appreciate all the help! Here is my code (another class calls on diseaseEditPanel):
class diseaseEditPanel extends JPanel implements ActionListener
{
Border bname;
JTable diseaseTable;
JScrollPane diseaseScroll;
Vector columnNames, rows;
String ewan = null;
public diseaseEditPanel()
{
bname = BorderFactory.createEtched Border();
setBorder(bname);
setLayout(null);
// Initialize a JTable with the rows of objects and column headings
diseaseTable = new JTable(rows, columnNames);
diseaseTable.getTableHeade r().setReo rderingAll owed(false );
// Add the table to a JScrollPane to make it display nicely
diseaseScroll = new JScrollPane(diseaseTable);
// Add the scroll pane to the container
add(diseaseScroll);
diseaseScroll.setBounds(0, 0,600,230) ;
}//end constructor
public void displayEditDisease(String sick) //sick being the selected item in the combobox
{
Connection conn = null;
ResultSet rs = null;
Statement statement = null;
ResultSetMetaData rsmd;
int numCols;
try {
// Connect to DB
Class.forName("com.mysql.j dbc.Driver ").newInst ance();
conn = DriverManager.getConnectio n("jdbc:my sql:///und erwriting" ,
"root", "");
// create a statement, execute query
statement = conn.createStatement();
rs = statement.executeQuery("SE LECT D_DiseaseCode, D_Description, D_RuleCode, D_Rule, D_Action, D_NumArrow, D_Arrow FROM Disease WHERE" + " D_Description = '" +sick+ "' ORDER BY D_DiseaseCode, D_RuleCode");
ResultSetMetaData metaData = rs.getMetaData();
//Get columns
int numberOfColumns = metaData.getColumnCount();
columnNames = new Vector();
for(int column = 0; column < numberOfColumns; column++)
{
columnNames.addElement(met aData.getC olumnLabel (column+1) );
}
// Get all rows.
rows = new Vector();
while (rs.next())
{
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++)
{
newRow.addElement(rs.getOb ject(i));
}
rows.addElement(newRow);
}
conn.close();
statement.close();
diseaseTable.setModel(new DefaultTableModel(rows, columnNames));
} catch (SQLException sqle) {
//sqle.printStackTrace();
System.out.println("+++++" +sqle.getM essage());
} catch(Exception e) {
System.err.println("Except ion: " + e.getMessage());
}
//hide 1st 2 columns of table
diseaseTable.getColumnMode l().getCol umn(0).set MaxWidth(0 );
diseaseTable.getColumnMode l().getCol umn(0).set MinWidth(0 );
diseaseTable.getColumnMode l().getCol umn(1).set MaxWidth(0 );
diseaseTable.getColumnMode l().getCol umn(1).set MinWidth(0 );
diseaseTable.getTableHeade r().getCol umnModel() .getColumn (0).setMax Width(0);
diseaseTable.getTableHeade r().getCol umnModel() .getColumn (0).setMin Width(0);
diseaseTable.getTableHeade r().getCol umnModel() .getColumn (1).setMax Width(0);
diseaseTable.getTableHeade r().getCol umnModel() .getColumn (1).setMin Width(0);
}//end displayEditDisease
public void actionPerformed(ActionEven t e)
{
}//end of actionPerformed
}//end diseasePanel
class diseaseEditPanel extends JPanel implements ActionListener
{
Border bname;
JTable diseaseTable;
JScrollPane diseaseScroll;
Vector columnNames, rows;
String ewan = null;
public diseaseEditPanel()
{
bname = BorderFactory.createEtched
setBorder(bname);
setLayout(null);
// Initialize a JTable with the rows of objects and column headings
diseaseTable = new JTable(rows, columnNames);
diseaseTable.getTableHeade
// Add the table to a JScrollPane to make it display nicely
diseaseScroll = new JScrollPane(diseaseTable);
// Add the scroll pane to the container
add(diseaseScroll);
diseaseScroll.setBounds(0,
}//end constructor
public void displayEditDisease(String sick) //sick being the selected item in the combobox
{
Connection conn = null;
ResultSet rs = null;
Statement statement = null;
ResultSetMetaData rsmd;
int numCols;
try {
// Connect to DB
Class.forName("com.mysql.j
conn = DriverManager.getConnectio
"root", "");
// create a statement, execute query
statement = conn.createStatement();
rs = statement.executeQuery("SE
ResultSetMetaData metaData = rs.getMetaData();
//Get columns
int numberOfColumns = metaData.getColumnCount();
columnNames = new Vector();
for(int column = 0; column < numberOfColumns; column++)
{
columnNames.addElement(met
}
// Get all rows.
rows = new Vector();
while (rs.next())
{
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++)
{
newRow.addElement(rs.getOb
}
rows.addElement(newRow);
}
conn.close();
statement.close();
diseaseTable.setModel(new DefaultTableModel(rows, columnNames));
} catch (SQLException sqle) {
//sqle.printStackTrace();
System.out.println("+++++"
} catch(Exception e) {
System.err.println("Except
}
//hide 1st 2 columns of table
diseaseTable.getColumnMode
diseaseTable.getColumnMode
diseaseTable.getColumnMode
diseaseTable.getColumnMode
diseaseTable.getTableHeade
diseaseTable.getTableHeade
diseaseTable.getTableHeade
diseaseTable.getTableHeade
}//end displayEditDisease
public void actionPerformed(ActionEven
{
}//end of actionPerformed
}//end diseasePanel
Listening
>> conn.close();
>> statement.close();
You should close the statement first and then the connection.
>> statement.close();
You should close the statement first and then the connection.
UPDATE Disease SET D_DiseaseCode = 'newValue', D_RuleCode = 'newValue', ....
WHERE D_Description = '" +sick+ "' " ;
?
WHERE D_Description = '" +sick+ "' " ;
?
First you must implement javax.swing.table.TableMod el .You can extend AbstractTableModel if you don't want to implement all methods. Implement method setValueAt to store data to DB, and getValueAt to display data at given position.Next you must setup this model via JTable.setModel method.
ASKER
>> UPDATE Disease SET D_DiseaseCode = 'newValue', D_RuleCode = 'newValue', ....
>>WHERE D_Description = '" +sick+ "' " ;
yes, I know how to set up my update sql statement. I just wanted to know how I would go about updating with my JTable and reflecting these changes in my database.
sir albel, if I use the method setValueAt, will I still need and update statement in sql or does the method do that automatically?.
>>WHERE D_Description = '" +sick+ "' " ;
yes, I know how to set up my update sql statement. I just wanted to know how I would go about updating with my JTable and reflecting these changes in my database.
sir albel, if I use the method setValueAt, will I still need and update statement in sql or does the method do that automatically?.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks sir albel, I'll give it a try and hopefully, I can get it to work.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.