Skip to main content

Assignment No:3

//Assignment No:3


Set A 

a) Create a PROJECT table with fields project_id, Project_name, Project_description, Project_Status. etc. Insert values in the table. Display all the details of the PROJECT table in a tabular format on the screen.(using swing). 

CODE

👇

  1. package project;

  2. import java.sql.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.util.*;


  7. class Project extends JFrame implements ActionListener
  8. {          
  9.             JLabel l1,l2,l3,l4;
  10.             JTextField t1,t2,t3,t4;
  11.             JButton b1,b2,b3;
  12.             String sql;
  13.             JPanel p,p1;
  14.             Connection con;
  15.             PreparedStatement ps;

  16.             JTable t;
  17.             JScrollPane js;
  18.             Statement stmt ;
  19.             ResultSet rs ;
  20.             ResultSetMetaData rsmd ;
  21.             int columns;
  22.             Vector columnNames = new Vector();
  23.             Vector data = new Vector();

  24.             Project()
  25.             {

  26.                         l1 = new JLabel("Enter Project ID :");
  27.                         l2 = new JLabel("Enter Project name :");
  28.                         l3 = new JLabel(" Enter Project Info :");      
  29.                         l4 = new JLabel(" Enter Project Status :");
  30.                         t1 = new JTextField(20);
  31.                         t2 = new JTextField(20);
  32.                         t3 = new JTextField(30);
  33.                         t4 = new JTextField(30);
  34.                         b1 = new JButton("Save");
  35.                         b2 = new JButton("Display");
  36.                         b3 = new JButton("Clear");

  37.                         b1.addActionListener(this);
  38.                         b2.addActionListener(this);
  39.                         b3.addActionListener(this);

  40.                         p=new JPanel();
  41.                         p1=new JPanel();
  42.                         p.add(l1);
  43.                         p.add(t1);
  44.                         p.add(l2);
  45.                         p.add(t2);
  46.                         p.add(l3);
  47.                         p.add(t3);
  48.                         p.add(l4);
  49.                         p.add(t4);
  50.                         p.add(b1);
  51.                         p.add(b2);
  52.                         p.add(b3);

  53.                         add(p);
  54.                         setLayout(new GridLayout(2,1));
  55.                         setSize(600,800);
  56.                         setVisible(true);
  57.                         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.             }
  59.             public void actionPerformed(ActionEvent e)
  60.             {
  61.                         if((JButton)b1==e.getSource())
  62.                         {
  63.                                     int project_id = Integer.parseInt(t1.getText());
  64.                                     String project_name = t2.getText();
  65.                                     String project_desc = t3.getText();
  66.                                     String project_status = t4.getText();
  67.                                     System.out.println("Accept Values");
  68.                                     try
  69.                                     {
  70.                                                 Class.forName("org.postgresql.Driver");
  71.                                                 con=DriverManager.getConnection("jdbc:postgresql:DATABASE_NAME","postgres","TYPE_PASSWORD_OF_DATABASE");                                              
  72.                                                 sql = "insert into project values(?,?,?,?)";
  73.                                                 ps = con.prepareStatement(sql);
  74.                                                 ps.setInt(1,project_id);
  75.                                                 ps.setString(2, project_name);
  76.                                                 ps.setString(3,project_desc);
  77.                                                 ps.setString(4,project_status);
  78.                                                 
  79.                                                 System.out.println("values set");
  80.                                                 int n=ps.executeUpdate();
  81.                                                 if(n!=0)
  82.                                                 {
  83.                                                             JOptionPane.showMessageDialog(null,"Record insered ...");                                  
  84.                                                 }
  85.                                                 else
  86.                                                             JOptionPane.showMessageDialog(null,"Record NOT inserted ");
  87.                                     }
  88.                                     catch(Exception ex)
  89.                                     {
  90.                                                 System.out.println(ex);          
  91.                                                 
  92.                                     }
  93.                         }
  94.                         else if((JButton)b2==e.getSource())
  95.                         {
  96.                                     try
  97.                                     {
  98.                                                 Class.forName("org.postgresql.Driver");
  99.                                                 con=DriverManager.getConnection("jdbc:postgresql:DATABASE_NAME","postgres","TYPE_PASSWORD_OF_DATABASE");   
  100.                                                 System.out.println("Connected");
  101.                                                 stmt=con.createStatement();
  102.                                                 rs = stmt.executeQuery("select * from project");
  103.                                                 rsmd = rs.getMetaData();
  104.                                                 columns = rsmd.getColumnCount();

  105.                                                 for(int i = 1; i <= columns; i++)
  106.                                                 {
  107.                                                             columnNames.addElement(rsmd.getColumnName(i));
  108.                                                 }

  109.                                                 while(rs.next())
  110.                                                 {
  111.                                                             Vector row = new Vector(columns);
  112.                                                             for(int i = 1; i <= columns; i++)
  113.                                                             {
  114.                                                                         row.addElement(rs.getObject(i));
  115.                                                             }
  116.                                                             data.addElement(row);
  117.                                                 }
  118.                                                 t = new JTable(data, columnNames);
  119.                                                 js = new JScrollPane(t);

  120.                                                 p1.add(js);
  121.                                                 add(p1);

  122.                                                 setSize(600, 600);
  123.                                                 setVisible(true);
  124.                                     }
  125.                                     catch(Exception e1)
  126.                                     {
  127.                                                 System.out.println(e1);
  128.                                     }
  129.                         }
  130.                         else
  131.                         {
  132.                                     t1.setText(" ");
  133.                                     t2.setText(" ");
  134.                                     t3.setText(" ");
  135.                                     t4.setText("");
  136.                        }
  137.             }

  138.             public static void main(String a[])
  139.             {
  140.                         Project p = new Project();
  141.             }
  142. }

b) Write a program to display information about the database and list all the tables in the database. (Use DatabaseMetaData). 

CODE

👇

  1. package database1;

  2. /**
  3.  *
  4.  * @author ThE ProFessoR
  5.  */
  6. import java.sql.*;
  7. public class Database1 
  8. {
  9.     public static void main(String[] args) 
  10.     {
  11.         try
  12.         {
  13.             Class.forName("org.postgresql.Driver");
  14.             Connection conn=DriverManager.getConnection("jdbc:postgresql:DATABASE_NAME","postgres","TYPE_PASSWORD_OF_DATABASE");  
  15.             DatabaseMetaData dbmd = conn.getMetaData();
  16.             System.out.println("\t-----------------------------------------------------");
  17.             System.out.println("\t\tDriver Name"+dbmd.getDriverName());
  18.             System.out.println("\t\tDriver Version"+dbmd.getDriverVersion());
  19.             System.out.println("\t\tUser Name"+dbmd.getUserName());
  20.             System.out.println("\t\tDatabase Product Name"+dbmd.getDatabaseProductName());
  21.             System.out.println("\t\tDatabase Product Version"+dbmd.getDatabaseProductVersion());
  22.             System.out.println("\t-------------------------------------------------------");
  23.             
  24.             String table[] = {"TABLE"};
  25.             ResultSet rs = dbmd.getTables(null, null, null, table);
  26.             System.out.println("\t\tTables Names:");
  27.             while(rs.next())
  28.             {
  29.                 System.out.println(rs.getString("TABLE_NAME"));
  30.             } 
  31.             rs.close();
  32.             conn.close();
  33.          }
  34.          catch(Exception e)
  35.          {
  36.              System.out.println(e);
  37.          }     
  38.     }
  39.     
  40. }

c) Write a program to display information about all columns in the DONAR table using ResultSetMetaData. 

CODE

👇

  1. package donar;

  2. /**
  3.  *
  4.  * @author ThE ProFessoR
  5.  */
  6. import java.sql.*;
  7. public class Donar 
  8. {
  9.     public static void main(String[] args) 
  10.     {
  11.         try
  12.         {
  13.             Class.forName("org.postgresql.Driver");
  14.             Connection con = DriverManager.getConnection("jdbc:postgresql:DATABASE_NAME","postgres","TYPE_PASSWORD_OF_DATABASE");     
  15.             
  16.             Statement stmt = null;
  17.             stmt = con.createStatement();
  18.             ResultSet rs = stmt.executeQuery("Select * From Donar");
  19.             
  20.             ResultSetMetaData rsmd = rs.getMetaData();
  21.             System.out.println("----------------------------------------");
  22.             
  23.             int count = rsmd.getColumnCount();
  24.             System.out.println("\tNo. of Cloumns"+rsmd.getColumnCount());
  25.             System.out.println("----------------------------------------");
  26.             
  27.             for(int i =1; i < count; i++)
  28.             {
  29.                System.out.println("\t\tColumn No "+i);
  30.                System.out.println("\t\tColumn Name "+rsmd.getColumnName(i));
  31.                System.out.println("\t\tColumn Type "+rsmd.getColumnTypeName(i));
  32.                System.out.println("\t\tColumn Display Size"+rsmd.getColumnDisplaySize(i));
  33.                System.out.println();               
  34.             }
  35.             System.out.println("----------------------------------------");
  36.             rs.close();
  37.             con.close();
  38.         }
  39.         catch(Exception e)
  40.         {
  41.             System.out.println(e);
  42.         }
  43.     }
  44.     
  45. }

Set B
 a) Create a MOBILE table with fields Model_Number, Model_Name, Model_Color, Sim_Type, NetworkType, BatteryCapacity, InternalStorage, RAM and ProcessorType. Insert values in the table. Write a menu driven program to pass the input using Command line argument to perform the following operations on MOBILE table. 
 1. Insert 2. Modify 3. Delete 4. Search 5. View All 6. Exit

CODE
👇
  1. package mobile;

  2. import java.sql.*;
  3. import java.util.*;

  4. public class Mobile
  5. {
  6.     public static void main(String[] args) {
  7.         {
  8.           Connection con;
  9.             con = null;
  10.           PreparedStatement pstmt ;
  11.           pstmt = null ;
  12.           Statement stmt ;
  13.                   stmt = null;
  14.           ResultSet rs = null;
  15.           
  16.           try 
  17.           {
  18.               Class.forName("org.postgresql.Driver");
  19.               con = DriverManager.getConnection("jdbc:postgresql:DATABASE_NAME","postgres","TYPE_PASSWORD");
  20.               Scanner sc = new Scanner(System.in);
  21.               System.out.println("\n Mobile Information ");
  22.                do {
  23.                  System.out.println("\n \t 1.Insert \n \t 2. Modify \n\t 3.delete \n\t 4.search \n\t 5.view all 6.Exit \n");
  24.                  System.out.println("\n Enter Your Choice");
  25.                  int ch = sc.nextInt();
  26.                  switch(ch) 
  27.                  {
  28.                      case 1 : 
  29.                          pstmt = con.prepareStatement("insert into Mobile values (?,?,?,?,?,?,?,?)");
  30.                          System.out.println("\n Enter Model Number ");
  31.                          int mno = sc.nextInt();
  32.                          pstmt.setInt(1,mno);
  33.                          
  34.                          sc.nextLine();
  35.                          System.out.println("\n Enter Model Name ");
  36.                          String name = sc.next();
  37.                          sc.nextLine();
  38.                          pstmt.setString(2,name);
  39.                          System.out.println("\n Enter Model_color ");
  40.                          String color = sc.next();
  41.                          sc.nextLine();
  42.                          pstmt.setString(3, color);
  43.                          System.out.println("\n Enter sim type ");
  44.                          String type = sc.next();
  45.                          sc.nextLine();
  46.                          pstmt.setString(4, type);
  47.                          System.out.println("\n Enter Battery Capacity");
  48.                          int battery = sc.nextInt();
  49.                          pstmt.setInt(5, battery);
  50.                          System.out.println("\n Enter Internal Storage in GB:");
  51.                          int internal = sc.nextInt();
  52.                          pstmt.setInt(6, internal);
  53.                          System.out.println("\n Enter RAM in GB ");
  54.                          int ram = sc.nextInt();
  55.                          pstmt.setInt(7, ram);
  56.                          sc.nextLine();
  57.                          System.out.println("\n Enter Procsser Type ");
  58.                          String pr = sc.next();
  59.                          sc.nextLine();
  60.                          pstmt.setString(8, pr);
  61.                          
  62.                          int result = pstmt.executeUpdate();
  63.                          System.out.println(result+" Recort Inserted \n ");
  64.                          break;
  65.                          
  66.                      case 2 : 
  67.                          String SQL = "update mobile set name=? where mno=?";
  68.                          pstmt = con.prepareStatement(SQL);
  69.                          
  70.                          System.out.println("\n Enter Model No for Update Record ");
  71.                          int no = sc.nextInt();
  72.                          pstmt.setInt(2, no);
  73.                          
  74.                          sc.nextLine();
  75.                          System.out.println("\n Enter Upadated Model Name ");
  76.                          String mname = sc.next();
  77.                          sc.nextLine();
  78.                          pstmt.setString(1, mname);
  79.                          
  80.                          int result2 = pstmt.executeUpdate();
  81.                          System.out.println(result2+"\n Record updated");
  82.                          break;
  83.                          
  84.                      case 3 :
  85.                          pstmt = con.prepareStatement("delete from Mobile where mno=?");
  86.                          System.out.println("\n Enter Model no for delete the record");
  87.                          int model = sc.nextInt();
  88.                          pstmt.setInt(1, model);
  89.                          int result3 = pstmt.executeUpdate();
  90.                          System.out.println(result3+"\n Record deleted");
  91.                          break;
  92.                          
  93.                      case 4 :
  94.                          pstmt = con.prepareStatement("select * from Mobile where mno=?");
  95.                          System.out.println("\n Enter Model No for search record ");
  96.                          int m = sc.nextInt();
  97.                          pstmt.setInt(1, m);
  98.                          
  99.                          rs = pstmt.executeQuery();
  100.                          System.out.println("\n ---------------- ");
  101.                       while(rs.next())
  102.                       {
  103.                        System.out.println("\n" +rs.getInt(1) + "\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getInt(5)+"\t"+rs.getInt(6)+"\t"+rs.getInt(7)+"\t"+rs.getString(8));
  104.                       }
  105.                       System.out.println("\n ---------------- ");
  106.                       break;
  107.                       
  108.                      case 5:
  109.                          pstmt = con.prepareStatement("select * from Mobile");
  110.                          System.out.println("\n ---------------- ");
  111.                       while(rs.next())
  112.                       {
  113.                        System.out.println("\n" +rs.getInt(1) + "\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getInt(5)+"\t"+rs.getInt(6)+"\t"+rs.getInt(7)+"\t"+rs.getString(8));
  114.                       }
  115.                       System.out.println("\n ---------------- ");
  116.                       break;
  117.                       
  118.                      case 6:
  119.                          System.exit(1);
  120.                          rs.close();
  121.                          stmt.close();
  122.                          pstmt.close();
  123.                          con.close();
  124.                          sc.close();
  125.                  }
  126.                } while(true);
  127.           }
  128.            catch(ClassNotFoundException | SQLException e)
  129.            {
  130.             System.out.println(e);
  131.            }
  132.           }
  133.         }
  134.         
  135.     }

b) Design a following Registration form and raise an appropriate exception if invalid information is entered like Birth Year ‘0000’



CODE
👇