What I basically want to do is adding a column of checkboxes in my table which is responsible for showing the contents of my database. And then I am planning to open up as many JFrames as there are checked-boxes when user clicks on the "Update..." button, and delete the rows corresponding to ones with checked-boxes when user hits "Delete".
interface I am planning to implement
package dbInterface;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.*;
public class Interface {
public Interface(Connection myConn) throws SQLException {
intFrame=new JFrame();
intFrame.setTitle("DBInterface");
intFrame.setLayout(null);
intFrame.setVisible(true);
intFrame.setSize(600, 450);
intFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
showDB(myConn);
insertButton=new JButton("Add...");
insertButton.setBounds(85, 300, 85, 25);
insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
insertToDB(myConn);
} catch (SQLException ex) {
Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
deleteButton=new JButton("Delete");
deleteButton.setBounds(255, 300, 85, 25);
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
delFromDB(myConn);
} catch (SQLException ex) {
Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
updateButton=new JButton("Update...");
updateButton.setBounds(425, 300, 85, 25);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
updateDB(myConn);
} catch (SQLException ex) {
Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
intFrame.add(updateButton);
intFrame.add(insertButton);
intFrame.add(deleteButton);
}
private void updateDB(Connection myConn) throws SQLException {
}
private void delFromDB(Connection myConn) throws SQLException {
}
private void showDB(Connection myConn) throws SQLException {
dataTable=new JTable();
Vector<Object> data = new Vector<Object>();
Vector<Object> columnNames=new Vector<Object>();
Statement showStmt=myConn.createStatement();
ResultSet rs=showStmt.executeQuery("select * from employees");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
columnNames.addElement(md.getColumnName(i));
while(rs.next()) {
Vector<Object> row = new Vector<Object>(6);
for(int i=1;i<=6;i++)
row.addElement(rs.getObject(i));
data.addElement(row);
}
rs.close();
showStmt.close();
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override
public Class getColumnClass(int column) {
for (int row = 0; row < getRowCount(); row++) {
Object o = getValueAt(row, column);
if (o != null)
return o.getClass();
}
return Object.class;
}
};
dataTable=new JTable(model);
scrollPane = new JScrollPane(dataTable);
scrollPane.setBounds(50, 20, 500, 200);
intFrame.add(scrollPane);
}
private void insertToDB(Connection myConn) throws SQLException {
insertForm=new JFrame();
insertForm.setTitle("Add");
insertForm.setLayout(null);
insertForm.setVisible(true);
insertForm.setSize(600, 450);
JLabel name=new JLabel("Name:");
name.setBounds(100,100,50,25);
JLabel surname=new JLabel("Surname:");
surname.setBounds(100,150,50,25);
JLabel email=new JLabel("E-Mail:");
email.setBounds(100,200,50,25);
JLabel dept=new JLabel("Department:");
dept.setBounds(100,250,50,25);
JLabel salary=new JLabel("Salary:");
salary.setBounds(100,300,50,25);
JTextField nameT=new JTextField();
nameT.setBounds(200,100,100,25);
JTextField surnameT=new JTextField();
surnameT.setBounds(200,150,100,25);
JTextField emailT=new JTextField();
emailT.setBounds(200,200,100,25);
JTextField deptT=new JTextField();
deptT.setBounds(200,250,100,25);
JTextField salaryT=new JTextField();
salaryT.setBounds(200,300,100,25);
JButton insert=new JButton("Add");
insert.setBounds(400,400,50,25);
insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
PreparedStatement insStmt=myConn.prepareStatement("insert into employees" +
"(last_name, first_name, email, department, salary)" +
"values" +
"(?, ?, ?, ?, ?)");
insStmt.setString(1, surnameT.getText());
insStmt.setString(2, nameT.getText());
insStmt.setString(3, emailT.getText());
insStmt.setString(4, deptT.getText());
insStmt.setInt(5, Integer.parseInt(salaryT.getText()));
insStmt.executeUpdate();
insertForm.dispose();
intFrame.remove(scrollPane);
showDB(myConn);
} catch (SQLException ex) {
Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
insertForm.add(name);
insertForm.add(surname);
insertForm.add(email);
insertForm.add(dept);
insertForm.add(salary);
insertForm.add(nameT);
insertForm.add(surnameT);
insertForm.add(emailT);
insertForm.add(deptT);
insertForm.add(salaryT);
insertForm.add(insert);
}
private JFrame intFrame,rowsFrame,insertForm;
private JButton showButton, insertButton, deleteButton, updateButton;
private JTable dataTable;
private JScrollPane scrollPane;
}
Aucun commentaire:
Enregistrer un commentaire