package frist_pro;
import java.sql.*;
import java.util.*;
public class Frist_pro {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int ch;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Properties prop= new java.util.Properties();
// prop.put("root","");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "");
Statement stmt = con.createStatement();
Outer:
do {
System.out.println("\n\n**Db Crud Opration**\n\n1..Read\n2..Insert\
n3..Update\n4..Delete\n5..Exit\n\n");
System.out.print("Enter Your Choice: ");
ch = sc.nextInt();
switch (ch) {
case 1://display
ResultSet rs = stmt.executeQuery("select * from student");
while (rs.next()) {
System.out.println("Id:-" + rs.getInt(1) + " " +
"Name:-" + " " + rs.getString(2) + " " + "BirthDate:-" + " " + rs.getDate(3));
}
break;
case 2:
System.out.print("Enter Name:");
String name=sc.next();
System.out.print("Enter DOB:");
String dob=sc.next();
String sql="INSERT INTO `student` (`id`, `name`, `dob`)
VALUES (NULL,'"+name+"','"+dob+"');";
int result = stmt.executeUpdate(sql);
if (result > 0) {
System.out.println("data Inserted Succefully");
} else {
System.out.println("Insert Have Error");
}
break;
case 3:
System.out.print("Enter Id To Update:");
int id=sc.nextInt();
System.out.print("Enter Name:");
name=sc.next();
System.out.print("Enter DOB:");
dob=sc.next();
sql="update student set name='"+name+"',dob='"+dob+"' where
id="+id+";";
result = stmt.executeUpdate(sql);
if (result > 0) {
System.out.println("Data updated succes;");
} else {
System.out.println("id not found");
}
break;
case 4:
System.out.print("Enter Id To Delete:");
id=sc.nextInt();
sql=("DELETE FROM `student` where id = "+id);
System.out.println(sql);
result = stmt.executeUpdate(sql);
if (result > 0) {
System.out.println("Data deleted succes;");
} else {
System.out.println("id not found");
}
break;
case 5:
break Outer;
default:
System.out.println("Wrong Choice");
break;
}
} while (ch != 5);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}