// Cognome e Nome: Cognome, Nome // Numero di matricola: // Data di Nascita: // login su SQL server: // Password su SQL server: import java.sql.*; import java.io.*; class punto3_4 { // subname deve essere sostituito con l'identificatore ODBC creato static String Con_URL = "jdbc:odbc:subname"; public static void main (String args []) //args[0]=C1, args[1]=C2 { try{ Connection con; // caricamento driver Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); // connessione: N deve essere sostituito con il numero del vostro gruppo con =DriverManager.getConnection(Con_URL,"login","password"); // inserire chiamate per creare lo statement PreparedStatement pquery = con.prepareStatement("select cid, nome, costoGiornaliero" + "from spazioPubblicitario" + " where citta = ? and cid not in" + " (select spazioPubblicario " + " from allocazione" + " where '06/12/2003' not between" + " dataAllocazione and dataRimozione"); pquery.setString(1,args[0]); ResultSet rs1= pquery.executeQuery(); // analisi risultato con cursore while (rs1.next()) { System.out.println("Id spazio: " + rs1.getString(1) + "Nome spazio: " + rs1.getString(2) + " costo giornaliero: " + rs1.getFloat(3)); } pquery.setString(1,args[1]); ResultSet rs2= pquery.executeQuery(); // stampa del risultato while (rs2.next()) { System.out.println("Id spazio: " + rs1.getString(1) + "Nome spazio: " + rs1.getString(2) + " costo giornaliero: " + rs1.getFloat(3)); } // volendo si poteva usare il createStatement. Anche se la soluzione con il preparedStatement e' migliore // Statement st = con.createStatement(); // String query_C1 = "select cid, nome, costoGiornaliero from spazioPubblicitario where citta = '" + args[1]+ "' and cid not in (select spazioPubblicario from allocazione where '06/12/2003' not between dataAllocazione and dataRimozione"; // String query_C2 = "select cid, nome, costoGiornaliero from spazioPubblicitario where citta = '" + args[1]+ "' and cid not in (select spazioPubblicario from allocazione where '06/12/2003' not between dataAllocazione and dataRimozione"; // ResultSet rs = st.executeQuery(query_C1); // stampa del risultato // while (rs.next()) // { // System.out.println("Id spazio: " + rs1.getString(1) + "Nome spazio: " + rs1.getString(2) + " costo giornaliero: " + rs1.getFloat(3)); // } // ResultSet rs2 = st.executeQuery(query_C2); // stampa del risultato // while (rs2.next()) // { // System.out.println("Id spazio: " + rs1.getString(1) + "Nome spazio: " + rs1.getString(2) + " costo giornaliero: " + rs1.getFloat(3)); // } Statement pquery2 = con.createStatement(); pquery2.executeUpdate("update Inserzionisti " + "set budget = budget * 1.3 " + "where citta = '" +args[1] + "'"); pquery2.executeUpdate("update SpazioPubblicitario " + "set costoGiornaliero = costoGiornaliero * .95 " + "where citta = '" +args[2] + "'"); // chiusura connessione con.close(); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } catch (SQLException e) { while( e!=null){ System.out.println("SQLState: " + e.getSQLState()); System.out.println(" Code: " + e.getErrorCode()); System.out.println(" Message: " + e.getMessage()); e = e.getNextException(); } } } }