Java Project
Java Project
Java Project
CRUD refers to the four basic operations a software application should be able to perform – Create,
Read/reterive, Update, and Delete.
In such apps, users must be able to create data, have access to the data in the UI by reading the data,
update or edit the data, and delete the data
In DB do the following
mysql> use empdb;
Database changed
Source code:
DBcode.java
package crud;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
String dburl="jdbc:mysql://localhost:3306/empdb";
String dbuser="root";
String dbpass="accord";
public DBCode() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection(dburl, dbuser, dbpass);
e.printStackTrace();
Product.java
package crud;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
PreparedStatement pst=con.prepareStatement(q);
pst.setInt(1,id);
pst.setString(2,name);
pst.setString(3,brand);
pst.setInt(4,qty);
pst.setDouble(5, price);
int res=pst.executeUpdate();
return res;
}
PreparedStatement pst=con.prepareStatement(q);
pst.setString(2,name);
pst.setDouble(1,price);
int res=pst.executeUpdate();
return res;
PreparedStatement pst=con.prepareStatement(q);
pst.setInt(1, id);
int res=pst.executeUpdate();
return res;
PreparedStatement pst=con.prepareStatement(q);
ResultSet rs=pst.executeQuery();
while(rs.next()) {
int id=rs.getInt(1);
String name=rs.getString(2);
String brand=rs.getString(3);
int qty=rs.getInt(4);
double price=rs.getDouble(5);
System.out.println(id+"\t"+name+"\t"+brand+"\t"+qty+"\tRs."+price);
ProductManagement.java
package crud;
import java.sql.SQLException;
import java.util.Scanner;
System.out.println("1.ADD\n2.VIEW\n3.EDIT\n4.DELETE\n5.Exit");
int ch=sc.nextInt();
sc.nextLine();
if(ch==1) {
System.out.println("Product name");
String name=sc.next();
System.out.println("Product brand");
String brand=sc.next();
System.out.println("Product qty");
int qty=sc.nextInt();
double price=sc.nextDouble();
System.out.println(res);
else if(ch==2) {
System.out.println("ID\tNAME\tBRAND\tQTY\tPRICE");
db.select();
else if(ch==3) {
String name=sc.next();
double price=sc.nextDouble();
System.out.println((r>0)?"Updated":"Not Updated");
}
else if(ch==4) {
int id=sc.nextInt();
int r=db.delete(id);
System.out.println((r>0)?"Deleted":"Not Deleted");
else if(ch==5) {
System.out.println("Operation terminated");