maneanirudha

DMSA_CUDIV

Sep 23rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. package ass1;
  2. import java.sql.*;
  3. public class pp
  4. {
  5.    
  6.    
  7.      
  8.            public static void main(String[] args)
  9.            {
  10.            try
  11.            {
  12.            
  13.               Class.forName("com.mysql.jdbc.Driver");        
  14.               Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/tecomp","root", "tecomp");
  15.               System.out.println("Connected database successfully...");
  16.               Statement stmt = conn.createStatement();
  17.                    
  18.              
  19.             //**********create Table********************************
  20.               String sql = "CREATE TABLE Registration " +
  21.                            "(id INTEGER not NULL, " +
  22.                            " first VARCHAR(255), " +
  23.                            " last VARCHAR(255), " +
  24.                            " age INTEGER, " +
  25.                            " PRIMARY KEY ( id ))";
  26.              
  27.                      
  28.              
  29.               stmt.executeUpdate(sql);
  30.              
  31.            
  32.              
  33.              
  34.        
  35.         //**********Inserting records into the table********************************  
  36.               System.out.println("Inserting records into the table...");
  37.               sql = "INSERT INTO Registration " +
  38.                            "VALUES (200, 'Anirudha', 'Mane', 21)";
  39.               stmt.executeUpdate(sql);
  40.              
  41.               sql = "INSERT INTO Registration " +
  42.                            "VALUES (201, 'Anosh', 'Magar', 22)";
  43.               stmt.executeUpdate(sql);
  44.              
  45.               sql = "INSERT INTO Registration " +
  46.                            "VALUES (202, 'Shraddha', 'Naik', 21)";
  47.               stmt.executeUpdate(sql);
  48.              
  49.               sql = "INSERT INTO Registration " +
  50.                            "VALUES(203, 'Omkar', 'Mankar', 21)";
  51.               stmt.executeUpdate(sql);
  52.              
  53.            
  54.               sql = "SELECT id, first, last, age FROM Registration";
  55.              
  56.              
  57.               ResultSet rs = stmt.executeQuery(sql);
  58.              
  59.              
  60.              
  61.             //**********Display Table********************************
  62.              
  63.               System.out.println("Registration Table After Insertion: ");
  64.              while(rs.next())
  65.              {
  66.                  System.out.print(" "+rs.getInt("id"));
  67.                  System.out.print(" "+rs.getInt("age"));
  68.                  System.out.print(" "+rs.getString("first"));
  69.                  System.out.println(" "+rs.getString("last"));
  70.              };
  71.              
  72.              
  73.            //**********Updating  records from the table*******************************
  74.                 sql = "UPDATE Registration " +"SET age = 35 WHERE id in (200, 202)";
  75.                 stmt.executeUpdate(sql);
  76.  
  77.                 sql = "SELECT id, first, last, age FROM Registration";
  78.                 rs = stmt.executeQuery(sql);
  79.        
  80.           //**********Display Table********************************
  81.                      
  82.                 System.out.println("Registration Table After Updation : ");
  83.                   while(rs.next())
  84.                      {
  85.                          System.out.print(" "+rs.getInt("id"));
  86.                          System.out.print(" "+rs.getInt("age"));
  87.                          System.out.print(" "+rs.getString("first"));
  88.                          System.out.println(" "+rs.getString("last"));
  89.                      };
  90.         /*Index*/
  91.            stmt.executeUpdate("create index Reg on Registration(id)");
  92.            System.out.println("Index Has Been Created");
  93.         /*View*/
  94.         stmt.executeUpdate("create view Regi as select first,age from Registration");
  95.         stmt.executeUpdate("select * from Regi");
  96.            
  97.         //**********Drop Table if already exist***************
  98.                            sql = "DROP TABLE Registration ";
  99.                              stmt.executeUpdate(sql);
  100.                              System.out.println("Table  deleted in given database...");
  101.                              
  102.            
  103.            }catch(Exception e)
  104.            {
  105.              
  106.            }
  107.     }
  108.     }
Add Comment
Please, Sign In to add comment