Category form
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
/*
* Created by JFormDesigner on Sat Jul 31 22:34:33 PDT 2021
*/
/**
* @author CJ Clem
*/
public class Category extends JFrame {
Connection1 con = new Connection1();
Connection conobj = [Link]();
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Category form1 = new Category();
[Link]();
[Link](true);
public void updateTable() throws SQLException {
String quer1 = "Select * from category";
PreparedStatement query = [Link](quer1);
ResultSet rs = [Link]();
ResultSetMetaData Res = [Link]();
int c = [Link]();
DefaultTableModel df = (DefaultTableModel) [Link]();
[Link](0);
[Link]();
int z = [Link]();
[Link]();
String[][] array = new String[0][];
if(z>0) {
array= new String[z][2];
}
int j=0;
while([Link]()) {
array[j][0] = [Link]("catcode");
array[j][1] = [Link]("catdesc");
++j;
String[] cols = {"Category Code", "Category Description"};
DefaultTableModel model = new DefaultTableModel(array,cols);
[Link](model);
[Link]([Link], null);
public Category() throws SQLException, ClassNotFoundException {
initComponents();
private void btnaddActionPerformed(ActionEvent e) throws SQLException {
String catcode, catdesc;
catcode = [Link]();
catdesc = [Link]();
String quer1 = "Select * from category where catcode=?";
PreparedStatement query = [Link](quer1);
[Link](1, catcode);
ResultSet rs = [Link]();
if([Link]()) { //[Link]() is true if the cursor
[Link](null, "The catcode you are trying to enter already exists ");
[Link]("");
[Link]("");
[Link]();
return;
String quer2 = "INSERT INTO Category VALUES ( ?, ? )";
query = [Link](quer2);
[Link](1, catcode);
[Link](2, catdesc);
[Link]();
[Link](null, "One record added ");
updateTable();
private void jtdataMouseClicked(MouseEvent e) {
DefaultTableModel df = (DefaultTableModel) [Link]();
int index1 = [Link]();
[Link]([Link](index1,0).toString());
[Link]([Link](index1,1).toString());
private void btneditActionPerformed(ActionEvent e) throws SQLException {
//get old value
DefaultTableModel df = (DefaultTableModel) [Link]();
int index1 = [Link]();
String catcode, catdesc;
catcode = [Link]();
catdesc = [Link]();
String oldvalue=[Link](index1,0).toString();
PreparedStatement query;
query = [Link]("Update category set catcode=?, catdesc=? where catcode = ?");
[Link](1, catcode);
[Link](2, catdesc);
[Link](3, oldvalue);
[Link]();
[Link](null, "One record edited ");
updateTable();
private void btndeleteActionPerformed(ActionEvent e) throws SQLException {
String catcode, catdesc;
catcode = [Link]();
catdesc = [Link]();
int result = [Link](null,"Are you sure you want to delete?", "Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.NO_OPTION){
return;
PreparedStatement query;
query = [Link]("Delete from category where catcode = ?");
[Link](1, catcode);
[Link]();
[Link](null, "One record deleted ");
updateTable();
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner non-commercial license
label1 = new JLabel();
txtcatcode = new JTextField();
label2 = new JLabel();
txtcatdesk = new JTextField();
scrollPane1 = new JScrollPane();
jtdata = new JTable();
btnadd = new JButton();
btnedit = new JButton();
btndelete = new JButton();
//======== this ========
var contentPane = getContentPane();
[Link](new MigLayout(
"hidemode 3",
// columns
"[fill]" +
"[fill]",
// rows
"[]" +
"[]" +
"[]" +
"[]"));
//---- label1 ----
[Link]("Enter the category code");
[Link](label1, "cell 0 0");
//---- txtcatcode ----
[Link](17);
[Link](txtcatcode, "cell 1 0");
//---- label2 ----
[Link]("Enter the category description");
[Link](label2, "cell 0 1");
[Link](txtcatdesk, "cell 1 1");
//======== scrollPane1 ========
{
//---- jtdata ----
[Link](new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
jtdataMouseClicked(e);
}
});
[Link](jtdata);
}
[Link](scrollPane1, "cell 1 2");
//---- btnadd ----
[Link]("Add");
[Link](e -> {
try {
btnaddActionPerformed(e);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
});
[Link](btnadd, "cell 1 3");
//---- btnedit ----
[Link]("Edit");
[Link](e -> {
try {
btneditActionPerformed(e);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
});
[Link](btnedit, "cell 1 3");
//---- btndelete ----
[Link]("Delete");
[Link](e -> {
try {
btndeleteActionPerformed(e);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
});
[Link](btndelete, "cell 1 3");
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner non-commercial license
private JLabel label1;
private JTextField txtcatcode;
private JLabel label2;
private JTextField txtcatdesk;
private JScrollPane scrollPane1;
private JTable jtdata;
private JButton btnadd;
private JButton btnedit;
private JButton btndelete;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
Connection 1
import [Link];
import [Link];
public class Connection1 {
public [Link] connect() throws SQLException, ClassNotFoundException {
[Link]("[Link]");
[Link] con1 = [Link]("jdbc:mysql://localhost/inventory", "root", "");
return con1;