You are on page 1of 2

import java.sql.

Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Book_Jdbc{

public static void main(String[] args) {


// TODOAuto-generated method stub
int bookid,price,year;
String name,Author,publisher;

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@vklnld1385:1556:synu1","HR","Delta#rw123");
Statement stmt=con.createStatement();
Scanner sc=new Scanner(System.in);
int choice;
do{
System.out.println("Enter the choice \n1.Add\n2.display \n3.Delete \
n4.search \n5.update\n6.Exit\n");
choice=sc.nextInt();
sc.nextLine();
switch(choice){
case 1:
System.out.println("Enter the Name of Book: ");
name=sc.nextLine();
System.out.println("Enter Author name");
Author=sc.nextLine();
System.out.println("Enter the publisher");
publisher=sc.nextLine();
System.out.println("Enter the year");
year=sc.nextInt();
System.out.println("Enter the price");
price=sc.nextInt();
String query1="Insert into abhi_1
(name,Author,publisher,price,year)values('"+name+"','"+Author+"','"+publisher+"',"
+price+","+year+")";
int count=stmt.executeUpdate(query1);
//                     while(rs.next()){
//                        System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getString(4)+" "+rs.getInt(5)+" "+rs.getInt(6));
//                     }
// System.out.println(count);
break;
case 2:
String query2="select * from abhi_1";
ResultSet rs=stmt.executeQuery(query2);
System.out.println("ID   Name  Author  Publisher    Price     Year");
while(rs.next()){
System.out.println(rs.getInt(1)+"    "+rs.getString(2)+"    "+rs.getString(3)+"   
"+rs.getString(4)+"       "+rs.getInt(5)+"    "+rs.getInt(6));
}
break;
case 3:
System.out.println("Enter the name of book:\n");
String i=sc.nextLine();
String query3="Delete from abhi_1 where name='"+i+"'";
int c=stmt.executeUpdate(query3);
if(c==1) System.out.println("------Deletion Successful------");
else System.out.println("====Operation failed======");
break;
case 4:
System.out.println("Enter the book name: ");
String s=sc.nextLine();
String query4="select * from abhi_1 where name='"+s+"'";
ResultSet m=stmt.executeQuery(query4);
while(m.next()){
System.out.println(m.getInt(1)+" "+m.getString(2)+" "+m.getString(3)+"
"+m.getString(4)+" "+m.getInt(5)+" "+m.getInt(6));
}
break;
case 5:
System.out.print("Enter the name of book: ");
String ss=sc.nextLine();
System.out.println("Enter the parameter\n1.price \n2.Year\n3.Author\n4.Publisher\
n5.Exit\n");
int c2=sc.nextInt();
switch(c2){
case 1:
System.out.print("Enter new price: ");
int value=sc.nextInt();
int count1=stmt.executeUpdate("update abhi_1 set price="+value+"where
name='"+ss+"'");
if(count1==1) System.out.println("------UPDATE SUCCESSFUL------");
else System.out.println("===== OPERATION Failed======");
break;
case 2:
System.out.print("Enter new year:");
int value1=sc.nextInt();
int count2=stmt.executeUpdate("update abhi_1 set year="+value1+"where
name='"+ss+"'");
if(count2==1) System.out.println("------UPDATE SUCCESSFUL------");
else System.out.println("===== OPERATION Failed======");
break;
}
case 6:
break;
}
}while(choice!=6);
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);}
}
}

You might also like