import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class manipulatingData extends JPanel implements ActionListener
{
private JButton btnAdd, btnEdit, btnDelete, btnSearch, btnSave, btnCancel;
private JFrame f;
private JPanel p,panel;
private JDialog addData;
private JLabel lblEnterName;
private JTextField txtName;
MyModel model = new MyModel();
ResultSet rs;
JTable table;
JTableHeader header;
TableColumn tc;
String strCount;
String name = "";
String sql;
private int confirm = 0;
public manipulatingData()
{
model.connectDB();
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setPreferredScrollableViewportSize(new Dimension(300,150));
table.setFillsViewportHeight(true);
table.setRowSelectionInterval(0,0);
table.changeSelection(0,0,false,false);
table.tableChanged(new TableModelEvent (model));
f = new JFrame();
f.setTitle("Data Manipulation");
f.setSize(527,467);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
header = table.getTableHeader();
header.setBackground(new Color(25,125,250));
header.setReorderingAllowed(false);
header.setResizingAllowed(false);
btnAdd = new JButton("Add");
btnEdit = new JButton("Edit");
btnDelete = new JButton("Delete");
btnSearch = new JButton("Search");
btnAdd.addActionListener(this);
btnEdit.addActionListener(this);
btnDelete.addActionListener(this);
btnSearch.addActionListener(this);
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(10,20,500,300);
btnAdd.setBounds(10,350,120,30);
btnEdit.setBounds(150,350,120,30);
btnDelete.setBounds(10,390,120,30);
btnSearch.setBounds(350,350,120,30);
p = new JPanel();
p.setLayout(null);
p.setBackground(new Color(115,55,93));
p.add(scroll);
p.add(btnAdd);
p.add(btnEdit);
p.add(btnDelete);
p.add(btnSearch);
f.add(p);
}
public static void main (String [] args)
{
new manipulatingData();
}
public void actionPerformed (ActionEvent e)
{
rs = model.rs;
if(e.getSource()==btnAdd)
{
//JOptionPane.showMessageDialog(null, model);
}
else if (e.getSource()==btnDelete)
{
confirm = JOptionPane.showConfirmDialog(null,"Do you want to delete this?","Confirmation",0);
if(confirm == 0)
{
try
{
rs.beforeFirst();
while(rs.next())
{
if(table.getSelectedRow() == 0 && rs.getRow() == 1)
{
rs.deleteRow();
// JOptionPane.showMessageDialog(null,"Record Deleted","Successful",1);
break;
}
else if(table.getSelectedRow() == rs.getRow())
{
rs.next();
rs.deleteRow();
table.tableChanged(new TableModelEvent (model));
// JOptionPane.showMessageDialog(null,"Record Deleted","Successful",1);
break;
}
}
}
catch (SQLException a)
{
System.err.println(a);
}
}
}
}
}
class MyModel extends AbstractTableModel
{
Connection con;
Statement s;
ResultSet rs;
ResultSetMetaData rsmd;
String cDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
Object rowData[][] = {};
String columnNames[] = {};
int count = 0;
int recordCount = 0;
public int getColumnCount()
{
return columnNames.length;
}
public String getColumnName(int column)
{
return columnNames[column];
}
public int getRowCount()
{
return rowData.length;
}
public Object getValueAt(int row, int column)
{
return rowData[row][column];
}
public void setValueAt(Object value, int row, int column)
{
rowData[row][column] = value;
fireTableCellUpdated(row, column);
}
public boolean isCellEditable(int row, int column)
{
return false;
}
public void connectDB()
{
try
{
Class.forName(cDriver);
con = DriverManager.getConnection("jdbc:odbc:testDBjava3.accdb","","");
s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = s.executeQuery("Select * from friends");
rsmd = rs.getMetaData();
recordCount = 0;
count = rsmd.getColumnCount();
String fieldNames[] = new String[count];
for(int a=1;a<=count;a++)
{
fieldNames[a-1] = rsmd.getColumnName(a);
}
while(rs.next())
{
recordCount++;
}
Object data [][] = new Object[recordCount][count];
rs.beforeFirst();
for(int x = 0;x<recordCount;x++)
{
rs.next();
for(int y = 0;y<count;y++)
{
data[x][y] = rs.getObject(y+1);
}
}
columnNames = fieldNames;
rowData = data;
}
catch (ClassNotFoundException exp)
{
System.err.println(exp);
}
catch(SQLException exp)
{
System.err.println(exp);
}
}
}