[go: up one dir, main page]

0% found this document useful (0 votes)
23 views6 pages

Java Project

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

CRUD operations

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

mysql> create table product(id int,name varchar(20),brand varchar(20),qty int,price double);

Source code:

DBcode.java
package crud;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DBCode {

public Connection con ;

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);

} catch (ClassNotFoundException | SQLException e) {

e.printStackTrace();

Product.java
package crud;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class Product extends DBCode {

int insert(int id,String name,String brand,int qty,double price) throws SQLException {

String q="insert into product values(?,?,?,?,?)";

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;
}

int update(String name,double price) throws SQLException {

String q="update product set price=? where name=?";

PreparedStatement pst=con.prepareStatement(q);

pst.setString(2,name);

pst.setDouble(1,price);

int res=pst.executeUpdate();

return res;

int delete(int id) throws SQLException {

String q="delete from product where id=?";

PreparedStatement pst=con.prepareStatement(q);

pst.setInt(1, id);

int res=pst.executeUpdate();

return res;

void select() throws SQLException {

String q="select * from product";

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;

public class ProductManagement {

public static void main(String[] args) throws SQLException {

Product db=new Product();

Scanner sc=new Scanner(System.in);

System.out.println("Product Management Dashboard");

System.out.println("1.ADD\n2.VIEW\n3.EDIT\n4.DELETE\n5.Exit");

System.out.print("Enter Your Choice : ");

int ch=sc.nextInt();

sc.nextLine();

if(ch==1) {

System.out.println("Enter the proudct ID");


int id=sc.nextInt();

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();

System.out.println("Product price per qty");

double price=sc.nextDouble();

int r=db.insert(id, name, brand, qty, price);

String res=(r>0)?"Added to DB":"Not Added";

System.out.println(res);

else if(ch==2) {

System.out.println("ID\tNAME\tBRAND\tQTY\tPRICE");

db.select();

else if(ch==3) {

System.out.println("Product name whose price to be updated");

String name=sc.next();

System.out.println("Update Product price per qty ");

double price=sc.nextDouble();

int r=db.update(name, price);

System.out.println((r>0)?"Updated":"Not Updated");

}
else if(ch==4) {

System.out.println("Enter the proudct ID to be deleted");

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");

You might also like