This OOP Student Crud Project will teach you how to do basic database functions that are CREATE RETIEVE, UPDATE and DELETE and SEARCH using mysql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.will discuss about how to work with oop using crud operations are (create,read,update,delete).
java oops concepts must when you moving to the java frameworks.
We will learn how to INSERT, SELECT, UPDATE and DELETE and SEARCH in database by writing code to manage the records table in the database named studentinfo. students table consist of following columns fname,course,fee.
First you have to create package com.example.oop.model.
package com.example.oop.model; public class Student { private int id; private String fname; private String course; private int fee; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public int getFee() { return fee; } public void setFee(int fee) { this.fee = fee; } }
we created the database studentinfo.inside the database created the table which name is students in the mysql database.
After that created the package com.example.oop.studentdb.
package com.example.oop.studentdb; import java.sql.Connection; import java.sql.DriverManager; public class StudentDb { static Connection con; static String driver = "com.mysql.jdbc.Driver"; static String url = "jdbc:mysql://localhost/studentinfo"; static String uname = "root"; static String pass = ""; public static Connection getConnection() throws Exception{ if(con == null){ Class.forName(driver); con = DriverManager.getConnection(url,uname, pass); } return con; } }
After that created the package com.example.oop.studentcontroller. create the interface StudentDAO.
public interface StudentDAO { public void save(Student students); public void update(Student students); public void delete(Student students); public Student get(int id); public List<Student> list(); }
after that create the class StudentDAOImp and implement the particular interface StudentDAO. inside the class
we create the crud operation methods.
package com.example.oop.studentcontoller; import com.example.oop.model.Student; import com.example.oop.studentdb.StudentDb; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; public class StudentDAOImp implements StudentDAO { @Override public void save(Student students) { try { Connection con = StudentDb.getConnection(); String sql = "INSERT INTO students(fname,course,fee) VALUES (?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, students.getFname()); ps.setString(2, students.getCourse()); ps.setInt(3, students.getFee()); ps.executeUpdate(); JOptionPane.showMessageDialog(null, "Saved!"); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } } @Override public void update(Student students) { try { Connection con = StudentDb.getConnection(); String sql = "UPDATE students SET fname=?,course=?,fee=? WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, students.getFname()); ps.setString(2, students.getCourse()); ps.setInt(3, students.getFee()); ps.setInt(4, students.getId()); ps.executeUpdate(); JOptionPane.showMessageDialog(null, "Updated!"); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } } @Override public void delete(Student students) { try { Connection con = StudentDb.getConnection(); String sql = "delete from students WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, students.getId()); ps.executeUpdate(); JOptionPane.showMessageDialog(null, "Deleteddd!"); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } } @Override public Student get(int id) { Student st = new Student(); try { Connection con = StudentDb.getConnection(); String sql = "SELECT * FROM students WHERE id=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if(rs.next()){ st.setId(rs.getInt("id")); st.setFname(rs.getString("fname")); st.setCourse(rs.getString("course")); st.setFee(rs.getInt("fee")); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } return st; } @Override public List<Student> list() { List<Student> list = new ArrayList<Student>(); try { Connection con = StudentDb.getConnection(); String sql = "SELECT * FROM students "; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()){ Student st = new Student(); st.setId(rs.getInt("id")); st.setFname(rs.getString("fname")); st.setCourse(rs.getString("course")); st.setFee(rs.getInt("fee")); list.add(st); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error"); } return list; } }
we designed the swing GUI design.After that created the package com.example.oop.studentview.
save button which we use to save data in to the mysql database.
double click the save button paste the code code inside.
Student st = new Student(); String sname = txtsname.getText(); String course = txtcourse .getText(); int fee = Integer.parseInt(txtfee .getText()); st.setFname(sname); st.setCourse(course); st.setFee(fee); StudentDAOImp dao = new StudentDAOImp(); dao.save(st); Load(); txtsname.setText(""); txtcourse.setText(""); txtfee.setText(""); txtsname.requestFocus();
Get the data from the mysql database to pass into JTable.so create the method which load() inside the method we paste the following code.
public void Load() { StudentDAOImp dao = new StudentDAOImp(); List<Student> list = dao.list(); DefaultTableModel DFT = (DefaultTableModel) jTable1.getModel(); DFT.setRowCount(0); for(Student st: list) { int sid = st.getId(); String stname = st.getFname(); String course = st.getCourse(); int fee = st.getFee(); DFT.addRow(new Object[]{sid,stname,course,fee}); } }
after done it. you have to call the load method inside the constructor of class.so when application runs all data will be loaded.i shown below how to call inside the constructor.
public StudentForm() { initComponents(); Load(); }
Search button which we use to seach data from the mysql database.double click the seach button paste the code code inside.
search = Integer.parseInt(JOptionPane.showInputDialog("Enter Student ID")); StudentDAOImp dao = new StudentDAOImp(); Student st = dao.get(search); txtsname.setText(st.getFname()); txtcourse.setText(st.getCourse()); txtfee.setText(String.valueOf(st.getFee()));
After search the record you have to make a changes what you need and click edit button.
Student st = new Student(); String sname = txtsname.getText(); String course = txtcourse .getText(); int fee = Integer.parseInt(txtfee .getText()); st.setFname(sname); st.setCourse(course); st.setFee(fee); st.setId(search); StudentDAOImp dao = new StudentDAOImp(); dao.update(st); Load(); txtsname.setText(""); txtcourse.setText(""); txtfee.setText(""); txtsname.requestFocus();
After search the record by using student what you want delete. and click delete button.
Student st = new Student(); st.setId(search); StudentDAOImp dao = new StudentDAOImp(); dao.delete(st); Load(); txtsname.setText(""); txtcourse.setText(""); txtfee.setText(""); txtsname.requestFocus();
This article explain how to make a Fish Inventory Management App in Angular.this app explain…
Introduction to Fish Inventory Management In the aquaculture industry, managing fish inventory is crucial for…
Introduction to Java GUI CRUD Java is a powerful programming language widely used for building…
Introduction to Login Form Design Designing an effective and beautiful login form is crucial for…
Introduction In today creating a responsive login form is essential for providing a seamless user…
Introduction to Inventory Management Systems In today's fast-paced digital environment, businesses require efficient inventory management…