package adding;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class Employee {
private int empId;
private String empName;
private int Salary;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getSalary() {
return Salary;
}
public void setSalary(int salary) {
Salary = salary;
}
public Employee(int empId, String empName, int salary) {
super();
this.empId = empId;
this.empName = empName;
Salary = salary;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + ", Salary=" + Salary + "]";
}
public static void main(String[] args) {
List<Employee>collect=new ArrayList<Employee>();
Scanner scanner = new Scanner(System.in);
Scanner scanner1 = new Scanner(System.in);
int ch;
do {
System.out.println("1. INSERT");
System.out.println("2. DISPLAY");
System.out.println("3. SEARCH");
System.out.println("4. DELETE");
System.out.println("5. UPDATE");
System.out.print("6. Enter your choice: ");
ch=scanner.nextInt();
switch (ch) {
case 1:
System.out.println("Enter empNo: ");
int empNo = scanner.nextInt();
System.out.print("Enter empName: ");
String empName = scanner1.nextLine();
System.out.print("Enter Salary: ");
int Salary = scanner.nextInt();
collect.add(new Employee(empNo,empName,Salary));
break;
case 2:
System.out.println("-------------------");
Iterator<Employee> i=collect.iterator();
while(i.hasNext()) {
Employee e = i.next();
System.out.println(e);
}
System.out.println("-------------------");
break;
case 3:
boolean found = false;
System.out.println("Enter empNo to search: ");
empNo = scanner.nextInt();
System.out.println("-------------------");
i=collect.iterator();
while(i.hasNext()) {
Employee e = i.next();
if(e.getEmpId()== empNo) {
System.out.println(e);
found = true;
}
}
if(!found) {
System.out.println("Record not found..");
}
System.out.println("-------------------");
break;
case 4:
found = false;
System.out.println("Enter empNo to Delete: ");
empNo = scanner.nextInt();
System.out.println("-------------------");
i=collect.iterator();
while(i.hasNext()) {
Employee e = i.next();
if(e.getEmpId()== empNo) {
i.remove();
found = true;
}
}
if(!found) {
System.out.println("Record not found..");
}else {
System.out.println("Record deleted successfully..");
}
System.out.println("-------------------");
break;
case 5:
found = false;
System.out.println("Enter empNo to Update: ");
empNo = scanner.nextInt();
System.out.println("-------------------");
ListIterator<Employee> list = collect.listIterator();
while(list.hasNext()) {
Employee e = list.next();
if(e.getEmpId()== empNo) {
System.out.print("Enter new name: ");
empName = scanner1.nextLine();
System.out.print("Enter new salary: ");
Salary = scanner.nextInt();
list.set(new Employee(empNo, empName, Salary));
found = true;
}
}
if(!found) {
System.out.println("Record not found..");
}else {
System.out.println("Record updated successfully..");
}
System.out.println("-------------------");
break;
}
}
while(ch!=0);
}
}
public class TestRecursion {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(num(7));
}
public static int num(int n) {
if(n<=1)
return n;
else
return num(n-1)+num(n-2);
}
}
ackage cust.code;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CustomerDetails {
public static void main(String[] args) {
String jdbcURL="jdbc:mysql://localhost:3306/sampleDb";
String dbusername="root";
String dbpassword="password123";
String password = "h123";
String fullname = "Hong Dim";
String email = "hd@gmail.com";
String username = "hond";
try {
Connection connection = DriverManager.getConnection(jdbcURL, dbusername, dbpassword);
String sql="UPDATE users SET password=?, fullname=?, email=? WHERE username=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, password);
statement.setString(2, fullname);
statement.setString(3, email);
statement.setString(4, username);
int rows = statement.executeUpdate();
if (rows > 0) {
System.out.println("Record updated...");
}
// String sql=”UPDATE users SET password=’abc’ WHERE username=’mondreed'”;
// Statement statement = connection.createStatement();
// int row=statement.executeUpdate(sql);
// if(row>0) {
// System.out.println(“Record updated…”);
// }
//
/*String sql="SELECT * FROM users";
Statement statement = connection.createStatement();
ResultSet result=statement.executeQuery(sql);
while(result.next()) {
int userId=result.getInt("user_id");
String username=result.getString("username");
String fullname=result.getString("fullname");
String email=result.getString("email");
String password=result.getString("password");
System.out.println(userId + ":" +username + " , " +
fullname + " , " + email + " , " + password);
}*/
/*String sql="INSERT INTO users (username,email,fullname,password)"
+ "VALUES(?, ?, ?, ?)";
*
* PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, email);
statement.setString(3, fullname);
statement.setString(4, password);
int row=statement.executeUpdate();*/
//String sql="INSERT INTO users (username,email,fullname,password)"
// + "VALUES(?, ?, ?, ?)";
//Statement statement = connection.createStatement();
//int row=statement.executeUpdate(sql);
/*if(row>0) {
System.out.println("Record has been inserted..");
}*/
connection.close();
/*if(connection != null) {
System.out.println("Connected");
}*/
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Spring.jpa.hibernate.ddl-auto=none
Spring.datasource.url=jdbc:mysql://localhost:3306/sampleDb
Spring.datasource.username=root
Spring.datasource.password=password123
package crud.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Product {
private Integer id;
private String name;
private float price;
public Product() {
super();
}
public Product(Integer id, String name, float price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
package crud.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends JpaRepository{
}
package crud.demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private ProductRepository repo;
public List<Product> listAll(){
return repo.findAll();
}
public void save(Product product) {
repo.save(product);
}
public Product get(Integer id) {
return repo.findById(id).get();
}
public void delete(Integer id) {
repo.deleteById(id);
}
}
package crud.demo;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@Autowired
private ProductService service;
@GetMapping(“/products”)
public List<Product> list(){
return service.listAll();
}
// @GetMapping(“/products/{id}”)
// public Product get(@PathVariable Integer id) {
// return service.get(id);
// }
@GetMapping(“/products/{id}”)
public ResponseEntity <Product> get(@PathVariable Integer id) {
try {
Product product=service.get(id);
return new ResponseEntity<Product>(product, HttpStatus.OK);
}catch(NoSuchElementException e){
return new ResponseEntity<Product>(HttpStatus.NOT_FOUND);
}
}
@PostMapping(“/products”)
public void add(@RequestBody Product product) {
service.save(product);
}
@PutMapping(“/products/{id}”)
public ResponseEntity<?> update(@RequestBody Product product, @PathVariable Integer id) {
try {
Product existProduct = service.get(id);
service.save(product);
return new ResponseEntity<>(HttpStatus.OK);
}catch(NoSuchElementException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping(“/products/{id}”)
public void delete(@PathVariable Integer id) {
service.delete(id);
}
}