import java.io.*; import java.sql.*; import java.lang.*; class hw2{ static Connection DBCon=null; String s=""; ResultSet rs=null; Statement stmt=null; String upd=""; BufferedReader br; public hw2(){ /*We load the jdbc:odbc bridge driver and open the connection which we'll use for the program*/ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch (Exception E){System.err.println("DRIVER");} try{ DBCon=DriverManager.getConnection("jdbc:odbc:444","yana","password"); stmt = DBCon.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); }catch (Exception E){System.err.println("Connection failed");} br=new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args){ hw2 my_hwk=new hw2(); my_hwk.query1(); my_hwk.update1(); my_hwk.query2(); my_hwk.update2(); my_hwk.query3(); //clean up try{ if (!DBCon.isClosed()) DBCon.close(); }catch(Exception E){System.err.println("Could not close the connection");E.printStackTrace();} } void query1(){ String q0="drop view Q1"; String q1=" create view Q1 as select distinct F_Name,F_Salary from Faculty F,Department D,Offering O "; q1+="where O.F_SSN=F.F_SSN and D.F_SSN=F.F_SSN and F.F_SSN not in"; q1+=" (select distinct F.F_SSN from Course C,Faculty F,Department D,Offering O where D.Did=C.Did and O.CourseNo=C.CourseNo and D.F_SSN=F.F_SSN and O.F_SSN=F.F_SSN)"; /* this step is not mandatory, just ensures that the program doesn't crash if the view exists*/ try{ stmt.execute(q0); }catch(Exception e){} try{ stmt.execute(q1); rs=stmt.executeQuery("select * from Q1"); }catch (Exception E){System.err.println("Q1 failed");E.printStackTrace();} try{ System.out.println("for Question 1: "); while (rs.next()){ System.out.println(rs.getString("F_Name")+" "+rs.getInt("F_Salary")); } }catch (Exception E){System.err.println("result set query 1:");E.printStackTrace();} } void update1(){ upd="update y_Faculty set F_Salary=1.1*F_Salary where F_Name in (select F_Name from Q1)"; try{ stmt.executeUpdate(upd); }catch (Exception E){System.err.println("Update failed");E.printStackTrace();} try{ rs=stmt.executeQuery("select F.* from y_Faculty F,Q1 where F.F_Name=Q1. F_Name"); }catch (Exception E){System.err.println(" select from y_Faculty failed");E.printStackTrace();} try{ System.out.println("\nafer update Question 1: "); while (rs.next()){ System.out.println(rs.getString("F_Name")+" "+rs.getInt("F_Salary")); } }catch (Exception E){System.err.println("question 1");E.printStackTrace();} } void query2(){ String q0="drop view DeptStats"; String q1="create view DeptStats as "; q1+="select MAX(F.F_Salary)as max_Salary,AVG(F.F_Salary)as avg_Salary,D.Did,D.F_SSN,D.D_Budget"; q1+=" from Faculty F, Department D "; q1+="where F.Did=D.Did group by D.Did,D.F_SSN,D.D_Budget"; String q2=" create view SadChairs as select F.F_SSN,F.F_Name,F_Salary-avg_Salary as Diff,F.F_Salary,D.D_Budget from Faculty F,DeptStats D "; q2+=" where D.F_SSN=F.F_SSN and F.F_Salary>D.avg_Salary and F.F_Salary