Wednesday, October 29, 2014

Design and Develop SQL DDL statements which demonstrate the use of SQL objects such as Table, View , Index using Client-Data sever(two tier).


Problem Statement: 
Design and Develop SQL DDL statements which demonstrate the use of SQL objects such as Table, View , Index using Client-Data sever(two tier).

Relation Schema:
Create following tables in oracle
Prof (id,pname,addr,city,age,DOJ,sal)
Works(id,dno)

Dept(dno,dname,addr,city)


Required Softwares:

1. mysql-connector-java-5.x.x
2. Eclipse

PROGRAM:

import java.io.*;
import java.sql.*;

public class Obiwan {

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException
{
BufferedReader buf= new BufferedReader(new InputStreamReader(System.in));
String url="jdbc:mysql://localhost/*Database Name*;
String driver="com.mysql.jdbc.Driver";
String user="*Username*", password="*Password*";
Connection conn=null;
Statement stmt=null;
int ch=0;
    
           do
           {
           System.out.println("1. CREATE A VIEW FOR PROF TABLE");
           System.out.println("2. DISPLAYING THE VIEW");
           System.out.println("3. UPDATING A VIEW");
           System.out.println("4. DELETING USING VIEW"); 
           System.out.println("5. DROPPING THE VIEW");
           System.out.println("6. CREATING THE INDEX");
          
           System.out.println("7. DISPLAYING THE INDEX");
           System.out.println("8. DELETING THE INDEX");
           System.out.println("9. EXIT");
           System.out.println("Enter choice: ");
           ch=Integer.parseInt(buf.readLine());
           switch(ch)
           {
           case 1:
            System.out.println("Creating view in given database...");
           
            String sql = "CREATE VIEW record_prof AS SELECT id_prof,first_name,last_name,doj,dob,age,salary FROM prof";
            stmt.executeUpdate(sql);
            System.out.print("\n");
           System.out.println("view created in given database...");
           
          break;
           case 2: System.out.println("DISPLAYING THE VIEW");
           String sql1= "select * from record_prof";
           ResultSet rs1 = stmt.executeQuery(sql1);
              while(rs1.next())
              {
                  int id = rs1.getInt("id_prof");
                  String First_Name = rs1.getString("first_name");
                  String Last_Name = rs1.getString("last_name");

                  int jid = rs1.getInt("age");

                  Date hih = rs1.getDate("dob");
                  Date gih = rs1.getDate("doj");
                  int kid = rs1.getInt("salary");
                  System.out.print("prof first name: " + First_Name+"  ,");
                  System.out.print("prof last name: " + Last_Name+"  ,");
                  System.out.print("prof ID: " + id +"  ,");
                  System.out.print("date of joining: " + gih +"\n");
                  System.out.print("date of birth: " + hih +"\n");
                  System.out.print("age: " + jid +"\n");
                  System.out.print("salary: " + kid +"\n");
                }
              System.out.print("\n");
              rs1.close();
           break;
           case 3: System.out.println("UPDATING A VIEW");
           String sql2 = " UPDATE record_prof SET age = 31 WHERE first_name='Jayti'";
           stmt.executeUpdate(sql2);
           String sql3 =" update record_prof set salary=55000 where first_name='Jayti'";
           stmt.executeUpdate(sql3);
           String sql4 = "SELECT * from record_prof";
              ResultSet rs2 = stmt.executeQuery(sql4);
              while(rs2.next())
              {

                  int id = rs2.getInt("id_prof");

                  String First_Name = rs2.getString("first_name");
                  String Last_Name = rs2.getString("last_name");
                
                  int jid = rs2.getInt("age");
                  Date hih = rs2.getDate("dob");
                  Date gih = rs2.getDate("doj");
                  int kid = rs2.getInt("salary");
                  System.out.print("prof first name: " + First_Name+"  ,");
                  System.out.print("prof last name: " + Last_Name+"  ,");
                  System.out.print("prof ID: " + id +"  ,");
                  System.out.print("date of joining: " + gih +"\n");
                  System.out.print("date of birth: " + hih +"\n");
                  System.out.print("age: " + jid +"\n");
                  System.out.print("salary: " + kid +"\n");
                }
              System.out.print("\n\n");
              rs2.close();
             break;
           case 4: System.out.println("DELETING USING VIEW");
           System.out.println("\n");
           String sql5 = "delete from record_prof where first_name='Zara'";
           stmt.executeUpdate(sql5);
           String sql6 = "SELECT * from record_prof";
              ResultSet rs3 = stmt.executeQuery(sql6);
              while(rs3.next())
              {
              int id = rs3.getInt("id_prof");
                  String First_Name = rs3.getString("first_name");
                  String Last_Name = rs3.getString("last_name");
           
                  int jid = rs3.getInt("age");
                  Date hih = rs3.getDate("dob");
                  Date gih = rs3.getDate("doj");
                  int kid = rs3.getInt("salary");
                  System.out.print("prof first name: " + First_Name+"  ,");
                  System.out.print("prof last name: " + Last_Name+"  ,");
                  System.out.print("prof ID: " + id +"  ,");
                  System.out.print("date of joining: " + gih +"\n");
                  System.out.print("date of birth: " + hih +"\n");
                  System.out.print("age: " + jid +"\n");
                  System.out.print("salary: " + kid +"\n");
                }
              System.out.print("\n");
              rs3.close();
           break;
           case 5 : System.out.println("DROPPING THE VIEW");
           System.out.println("\n");
           String sqqqql= "drop view record_prof";
           stmt.executeUpdate(sqqqql);
           System.out.println("VIEW IS DROPPED");
           break;
           case 6: System.out.println("CREATING THE INDEX");
           System.out.println("\n");
           String sqlo = "CREATE UNIQUE INDEX index_first ON prof ( id_prof, first_name)";
           stmt.executeUpdate(sqlo);
           System.out.println("INDEX CREATED SUCCESSfULLY");
           break;
           case 7 : System.out.println("DISPLAYING THE INDEX");
           System.out.println("\n");
           ResultSet indexInformation = null;
           DatabaseMetaData meta = conn.getMetaData();
           indexInformation = meta.getIndexInfo(conn.getCatalog(), null, "prof", true, true);
           while (indexInformation.next()) {
             String dbCatalog = indexInformation.getString("TABLE_NAME");
        
             boolean dbNoneUnique = indexInformation.getBoolean("NON_UNIQUE");
             
             String dbIndexName = indexInformation.getString("INDEX_NAME");
             int dbPages = indexInformation.getInt("PAGES");
             String dbColumnName = indexInformation.getString("COLUMN_NAME");
             int dbCardinality = indexInformation.getInt("CARDINALITY");
             short dbType = indexInformation.getShort("TYPE");

             System.out.println("Table Name=" + dbCatalog);

             System.out.println("nonUnique=" + dbNoneUnique);
             System.out.println("pages=" + dbPages);
             System.out.println("Index Name=" + dbIndexName);
             System.out.println("cardinality=" + dbCardinality);
             System.out.println("COlumn_name=" + dbColumnName);
             System.out.println("TYPE=" + dbType);
             System.out.println("\n");
           }

           

           break;
           case 8: System.out.println("Deleting a index");
           System.out.println("/n");
           String sqqql="alter table prof drop index index_first";
           stmt.executeUpdate(sqqql);
           System.out.println(" INDEX DELETED SUCCESSFULLY");
           
           
           case 9: System.out.println("EXIT");
           break;
           
           }
          }while(ch!=9);
         
          //STEP 6: Clean-up environment
               stmt.close();
               conn.close();
       }
       catch(SQLException se)
       {
                   //Handle errors for JDBC
                   se.printStackTrace();
                   }
               catch(Exception e)
               {
                   //Handle errors for Class.forName
                   e.printStackTrace();
                   }
               finally
               {
                   //finally block used to close resources
                   try
                   {
                   if(stmt!=null)
                   stmt.close();
                   }
                   catch(SQLException se2){
                   }// nothing we can do
                   try{
                   if(conn!=null)
                   conn.close();
                   }
                   catch(SQLException se){
                   se.printStackTrace();
                   }//end finally try
                   }//end try


           }


private static void setDriver(String string) {

// TODO Auto-generated method stub

}

       }

OUTPUT:








DBMS using connections(Client-application server-Data sever, three tier) Oracle/MySQL (ODBC/JDBC), SQL Joints, prompt.

Problem Statement: 
DBMS using connections(Client-application server-Data sever, three tier) Oracle/MySQL (ODBC/JDBC), SQL Joints, prompt.

Required Softwares:
1. Apache Tomcat Server
2. Eclipse

PROGRAM:

Html page

asgb1.html

<body>
<head align=center>MUSIC LIBRARY</head>
<hr>

<form name="form1" action="q11.jsp" method="post">
1. Display all album names and corresponding artist names and artist ID.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>

<form name="form2" action="q22.jsp" method="post">
2. Display details of all tracks in a particular album.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>

<form name="form3" action="q33.jsp" method="post">
3. Show the longest and shortest track of the album.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>

<form name="form4" action="q44.jsp" method="post">
4. Display the total number of tracks in the album by.
Artist= <input type="text" name="user">
<INPUT TYPE=SUBMIT VALUE="submit" />


</form>
<hr>


<form name="form4" action="q55.jsp" method="post">
5. List all Tracks and show when it was last played.If a track has never been played, still show those tracks.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>



<form name="form4" action="q66.jsp" method="post">
6. List all Tracks and show when it was last played.If a track has never been played, DO NOT show those tracks.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>



<form name="form4" action="q77.jsp" method="post">
7. Create index on Track_Name and find the Album Name for particular track.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>



<form name="form4" action="q88.jsp" method="post">
8. Delete the index created in (7)
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>



<form name="form4" action="q99.jsp" method="post">
9. CREATE and DISPLAY a BRIEF VIEW of all the tracks.
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>


<form name="form4" action="q10.jsp" method="post">
10.DELETE the Track VIEW created in (9)
<INPUT TYPE=SUBMIT VALUE="submit" />
</form>
<hr>


</body>

JSP Pages 

q11.jsp

<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root"); 
//Connection con = DriverManager.getConnection("jdbc:mysql://192.168.5.101:3306/TE3205db","TE3205","TE3205"); 
Statement st= con.createStatement(); 
ResultSet rs=st.executeQuery("select Artist_ID,Artist_Name,Album_Name from Artist natural join Album"); 
%>

<table border="1">
<tr>
   <th>Artist ID</th>
   <th>Artist Name</th>
   <th>Album Name</th>
</tr>


<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getInt(1) %></td>
                <TD> <%= rs.getString(2) %></TD>
                <TD> <%= rs.getString(3) %></TD>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>

</html>

q22.jsp

<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root"); 
//Connection con = DriverManager.getConnection("jdbc:mysql://192.168.5.101:3306/TE3205db","TE3205","TE3205"); 
Statement st= con.createStatement(); 
ResultSet rs=st.executeQuery("select Artist_ID,Artist_Name,Album_Name from Artist natural join Album"); 
%>

<table border="1">
<tr>
   <th>Artist ID</th>
   <th>Artist Name</th>
   <th>Album Name</th>
</tr>


<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getInt(1) %></td>
                <TD> <%= rs.getString(2) %></TD>
                <TD> <%= rs.getString(3) %></TD>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q33.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root"); 
Statement st= con.createStatement(); 
ResultSet rs=st.executeQuery("select Track_ID ,Track_Name,Duration from Track where Album_ID=1 AND Duration=(select MAX(Duration) FROM Track where Album_ID=1) OR  Duration=(select MIN(Duration) FROM Track where Album_ID=1)"); 
%>

<table border="1">
<tr>
   <th>Track ID</th>
   <th>Track Name</th>
   <th>Duration</th>
</tr>

<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getInt(1) %></td>
                <TD> <%= rs.getString(2) %></TD>
                <TD> <%= rs.getString(3) %></TD>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q44.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root");
PreparedStatement pstmt = null;
Statement st= con.createStatement();
String artist=request.getParameter("user");

String sqlString ="Select Artist_Name,Album_Name,count(*) AS No_Of_Tracks from Artist inner join Album using(Artist_ID) inner join Track using(Artist_ID,Album_ID) where Artist_Name =?"; 
pstmt = con.prepareStatement(sqlString); 
pstmt.setString(1,artist); 
ResultSet rs ;
rs= pstmt.executeQuery();
 
%>

<table border="1">
<tr>
   <th>Artist Name</th>
   <th>Album Name</th>
   <th>No. of tracks</th>
</tr>

<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getString(1) %></TD>
                <TD> <%= rs.getString(2) %></TD>
             <TD> <%= rs.getInt(3) %></td>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q55.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root");  
Statement st= con.createStatement(); 

ResultSet rs=st.executeQuery("select Track_Name , Last_Played from Track left join Played USING(Album_ID,Track_ID)ORDER BY Last_Played DESC"); 
%>

<table border="1">
<tr>
   <th>Track Name</th>
   <th>Last Played</th>
</tr>

<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getString(1) %></TD>
                <TD> <%= rs.getString(2) %></TD>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q66.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root");  
Statement st= con.createStatement(); 

ResultSet rs=st.executeQuery("select Track_Name , Last_Played from Track right join Played USING(Album_ID,Track_ID)ORDER BY Last_Played DESC"); 
%>

<table border="1">
<tr>
   <th>Track Name</th>
   <th>Last Played</th>
</tr>

<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getString(1) %></TD>
                <TD> <%= rs.getString(2) %></TD>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q77.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root"); 
Statement st= con.createStatement(); 
st.executeUpdate("CREATE INDEX Name_Index on Track(Track_Name)");
ResultSet rs=st.executeQuery("select Album_Name,Duration from Album INNER JOIN Track USING(Album_ID)WHERE Track_Name='Hey Brother'"); 
%>

<table border="1">
<tr>
   <th>Artist Name</th>
   <th>Duration</th>
</tr>

<% while(rs.next()){ %>
            <TR>
             <TD> <%= rs.getString(1) %></TD>
             <TD> <%= rs.getString(2) %></td>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q88.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root"); 
Statement st= con.createStatement(); 

st.executeUpdate("alter table Track drop index Name_Index");


out.println("<BR>");

out.println("<BR>");
out.println("INDEX on Name column DELETED");


st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q99.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root");  
Statement st= con.createStatement(); 
st.executeUpdate("CREATE VIEW Track_View AS SELECT Track_Name,Album_ID,Artist_ID FROM Track ORDER BY Album_ID,Track_Name");
ResultSet rs=st.executeQuery("select * from Track_View"); 
%>

<table border="1">
<tr>
   <th>Track Name</th>
   <th>Album ID</th>
   <th>Artist ID</th>
</tr>

<% while(rs.next()){ %>
            <TR>
                <TD> <%= rs.getString(1) %></TD>
                <TD> <%= rs.getInt(2) %></TD>
             <TD> <%= rs.getInt(3) %></td>
            </TR>
            <% } %>
        </table>
<%
rs.close();
st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

q10.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<html>
<%
Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/b1","root","root"); 
Statement st= con.createStatement(); 
st.executeUpdate("drop view Track_View");


out.println("<BR>");

out.println("<BR>");
out.println("Track View has been DELETED");


st.close();
con.close();
%>

<body>
<form name="prev" action="asgb1.html" method="post">
<hr>
<INPUT TYPE=SUBMIT VALUE="PREVIOUS" />
</body>
</html>

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...