Hello everyone!
I have a button on my main form that allows me to get the visitInfo Table. The data are displayed inside a JTable.
What I would like to know is how do I edit or delete a row from that Jtable by the click of a save button or delete button.
Here is the class that shows the JTable with the Save, delete and Exit Buttons.
/*This class shows the result of the visit information
* related to a specific type of service.
* When the user select a service type and
* click on the Get visitInfo Button.
*/
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
public class VisitTable extends JFrame
{
//Specifies the driver name
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
// specifies the location of the database in which the table resides
String url = "JDBC:ODBC:Student";
//No userid nor password is required to access the database
String userid = "";
String password = "";
//Connection,statement and resulset are all set to null for now
Connection connection =null;
Statement stmt = null;
ResultSet rs = null;
JTable visittable;
DefaultTableModel model;
Vector columnNames;
Vector visitdata ;
JButton SaveBtn,DelBtn,ExitBtn;
//constructor
public VisitTable(Object selectedItem){
super("Visit Information");
columnNames = new Vector();
visitdata = new Vector();
/*SQL query that retrieves all the visit information
* about a specific type of visit
chosen by the user from a combobox*/
String query = "SELECT * FROM VisitTable WHERE TypeofService='"+ selectedItem+"'";
try{
// Connect to the Database
Class.forName(driver);
connection = DriverManager.getConnection( url, userid, password );
// Read data from a table
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
//Get number of columns
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++){
columnNames.addElement( md.getColumnName(i) );
}
// Get row data
while ((rs!= null)&&(rs.next())){
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement( rs.getObject(i) );
}
visitdata.addElement( row );
}
//Close the resultset, statement and the connection
rs.close();
connection.close();
stmt.close();
}catch(Exception e){
System.out.println( e );
}
// Create table with data queried from the table "VisitTable"
visittable = new JTable(visitdata, columnNames);
model = new DefaultTableModel(visitdata,columnNames);
model.insertRow(visittable.getRowCount(),new Object[] {"","","","",""});
JScrollPane scrollPane = new JScrollPane( visittable );
JPanel BtnPanel = new JPanel();
// This method add new record to the table
SaveBtn = new JButton("Save Row");
SaveBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//This method remove the selected row from the table
DelBtn = new JButton("Delete Row");
DelBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//This method close the Visit Information frame
ExitBtn = new JButton("Exit");
ExitBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//Add the buttons to the GUI
BtnPanel.add(SaveBtn);
BtnPanel.add(DelBtn);
BtnPanel.add(ExitBtn);
Container c =getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
c.add(scrollPane);
c.add(BtnPanel);
}
}
Please I'd be very grateful to recieve a help on this.
Thanks for the support!