RunCodes
Programming & Tech

CRUD System in java – How to Insert, Delete, Update and Read in java using NetBeans & MySQL DB?[With Source Code]

0 153

Code:

1. create code[inside the clicking event of create button]


boolean isEmpty=false;
Component[] components = getContentPane().getComponents();
for(Component component:components){
if(component instanceof JTextField){
if(((JTextField) component).getText().isEmpty()){
isEmpty=true;
}
}
}
if(isEmpty){
JOptionPane.showMessageDialog(null, "please fill the form");
}
else{
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/javacrud","root","admin");
String createQuery = "INSERT INTO javacrud (name, email, username, password) VALUES (?,?,?,?)";
PreparedStatement ps;
ps=con.prepareStatement(createQuery);
ps.setString(1, txtName.getText());
ps.setString(2, txtEmail.getText());
ps.setString(3, txtUser.getText());
ps.setString(4, txtPass.getText());
ps.execute();

JOptionPane.showMessageDialog(null, "created Successfully");


} catch (SQLException ex) {
Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
}
}

 

 

2. update code: [inside the clicking event of update button]


try {
// TODO add your handling code here:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/javacrud","root","admin");
String updateQuery="UPDATE javacrud SET name=?, email=?, username=?, password=? WHERE id=?";
PreparedStatement ps;
ps=con.prepareStatement(updateQuery);
ps.setString(1, txtName.getText());
ps.setString(2, txtEmail.getText());
ps.setString(3, txtUser.getText());
ps.setString(4, txtPass.getText());
ps.setString(5, txtID.getValue().toString());
ps.execute();
JOptionPane.showMessageDialog(null, "updated Successfully");

} catch (SQLException ex) {
Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
}

 

3. read [inside the clicking event of read button]


Component[] components = getContentPane().getComponents();
for(Component component:components){
if(component instanceof JTextField || component instanceof JLabel || component instanceof JSpinner){
component.setVisible(false);
}
}
showJTable();
repaint();

 

showJTabel function code:


private void showJTable(){
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/javacrud","root","admin");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM javacrud");
DefaultTableModel model = new DefaultTableModel();
int columnCount = rs.getMetaData().getColumnCount();
//add columns to the model
for(int i =1;i<=columnCount; i++){
model.addColumn(rs.getMetaData().getColumnName(i));
}

//add rows to the model
while(rs.next()){
Object[] rowData = new Object[columnCount];
for(int i =0; i<columnCount;i++){
rowData[i]=rs.getObject(i+1);
}
model.addRow(rowData);

}

//create jtable

JTable table = new JTable(model);
table.setRowHeight(30);

JScrollPane scrollPane = new JScrollPane(table);
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane,BorderLayout.CENTER);
panel.setBounds(50,50,400,300);
getContentPane().add(panel);
repaint();
rs.close();
stmt.close();
con.close();




} catch (SQLException ex) {
Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
}
}

 

4. Delete [inside the clicking event of delete button]


try {
// TODO add your handling code here:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/javacrud","root","admin");
String deleteQuery="DELETE FROM javacrud WHERE id=?";
PreparedStatement ps;
ps=con.prepareStatement(deleteQuery);
ps.setString(1, txtID.getValue().toString());
ps.execute();
JOptionPane.showMessageDialog(null, "deleted successfully");
} catch (SQLException ex) {
Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
}

 

5. Home [inside the clicking event of home button]


Component[] components = getContentPane().getComponents();
for(Component component:components){
if(component instanceof JPanel){
component.setVisible(false);
}else{
component.setVisible(true);
if(component instanceof JTextField){
((JTextField) component).setText("");
}
}
}

More:

 

Leave a comment