DBMS using connections(Client-Data sever, two tier) Oracle/MySQL (ODBC/JDBC), SQL prompt to create data base tables insert, update data values, delete table, use table, select queries with/without where clause. ,demonstrate use of stored procedure / function (create procedure at the data side and make use of it on the client side)


 import java.sql.*;  
 import java.util.*;  
 public class JDBC {  
      static String insert(){  
           Scanner s = new Scanner(System.in);  
           int book_id;  
           String book_name = null, book_author = null, book_publ=null;  
           System.out.print("Enter book ID: ");  
           book_id = s.nextInt();  
           System.out.print("Enter book Name: ");  
           book_name = s.next();  
           System.out.print("Enter book Author: ");  
           book_author = s.next();  
           System.out.print("Enter book Publication: ");  
           book_publ = s.next();  
           String query = "insert into books values("+book_id+","+"'"+book_name+"'"+","+"'"+book_author+"'"+","+"'"+book_publ+"')";  
           return query;  
      }  
      static String delete()  
      {  
           Scanner s = new Scanner(System.in);  
           String book_name = null;  
           System.out.print("Enter book name to be deleted from database: ");  
           book_name = s.next();  
           String query = "delete from books where book_title = '"+book_name+"'";  
           return query;  
      }  
      static String update(){  
           Scanner s = new Scanner(System.in);  
           System.out.print("Enter book name to be Updated: ");  
           String old_book_name = s.next();  
           System.out.print("Enter the new name for book: ");  
           String new_book_name = s.next();  
           String query = "update books set book_title = '" + new_book_name + "' where book_title = '" + old_book_name + "'";  
           return query;  
      }  
      public static void main(String[] argc){  
           try{  
                Scanner s = new Scanner(System.in);  
                String query = null;  
                Class.forName("com.mysql.jdbc.Driver");  
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/library", "root", "");  
                Statement st = con.createStatement();  
                System.out.println("\nDATABASE SLELECTED : library");  
                System.out.println("\nTABLE SLELECTED  : books");  
                String choice;  
                do{  
                     System.out.println("**********************************************************************");  
                     System.out.println("\t\t\tJDBC");  
                     System.out.println("**********************************************************************");  
                     System.out.println("\n1.Insert data into table books ");  
                     System.out.println("\n2.Delete book from books table ");  
                     System.out.println("\n3.Display books from book table ");  
                     System.out.println("\n4.Update Book name");  
                     System.out.println("\n5.Exit");  
                     System.out.print("\nEnter choice: ");  
                     choice = s.next();  
                     switch(choice){  
                     case "1":  
                               query = insert();  
                               st.executeUpdate(query);  
                               System.out.println("Successfully Added Book");  
                               break;  
                     case "2":  
                               query = delete();  
                               st.executeUpdate(query);  
                               System.out.println("Successfully deleted Book record");  
                               break;  
                     case "3":  
                               query = "select * from books";  
                               ResultSet r = st.executeQuery(query);  
                               while(r.next()){  
                                    String book_id = r.getString("book_id");  
                                    String book_name = r.getString("book_title");  
                                    String book_author = r.getString("book_author");  
                                    String book_publ = r.getString("book_publication");  
                                    System.out.println(""+book_id + "\t" + book_name+"\t\t\t"+book_author+"\\t"+book_publ);  
                               }  
                               break;  
                     case "4":  
                               query = update();  
                               st.executeUpdate(query);  
                               System.out.println("Successfully Updated !!!!!!!!!");  
                               break;  
                     default:  
                               System.out.println("Invlid choice !!!!!!!!!");  
                               break;  
                     }  
                }while(choice != "5");  
           }  
           catch(Exception e){  
                e.printStackTrace();  
           }  
      }  
 }  

Post a Comment

Previous Post Next Post