home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / MesitiM / teach / sol8.txt < prev    next >
Text File  |  2003-05-26  |  9KB  |  387 lines

  1. ESERCIZIO 1
  2. ===========
  3.  
  4. import java.sql.*; 
  5. import java.io.*; 
  6.  
  7. class JdbcConn
  8.   
  9. // subname deve essere sostituito con l'identificatore ODBC creato
  10.  
  11.  static String Con_URL = "jdbc:odbc:subname";
  12.    
  13.  public static void main (String args []) 
  14.  { 
  15.      try{
  16.  
  17.         Connection con;
  18.  
  19.     // caricamento driver
  20.  
  21.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  22.     
  23.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  24.     
  25.         con =DriverManager.getConnection(Con_URL,"gruppoN", "gruppoN");
  26.  
  27.     // chiusura connessione
  28.     
  29.         con.close();
  30.  
  31.       } 
  32.     catch(java.lang.ClassNotFoundException e) {
  33.             System.err.print("ClassNotFoundException: "); 
  34.             System.err.println(e.getMessage());
  35.         }
  36.     catch (SQLException e) {
  37.          while( e!=null){ 
  38.             System.out.println("SQLState: " + e.getSQLState());
  39.             System.out.println("    Code: " + e.getErrorCode());
  40.             System.out.println(" Message: " + e.getMessage());
  41.             e = e.getNextException();
  42.                 }
  43.                 }
  44.    } 
  45. }
  46.  
  47. =====================================================================
  48. ESERCIZIO 2
  49. =====================================================================
  50.  
  51.  
  52. import java.sql.*; 
  53. import java.io.*; 
  54.  
  55. class JdbcQueryNonPrep1
  56.   
  57. // subname deve essere sostituito con l'identificatore ODBC creato
  58.  
  59.  static String Con_URL = "jdbc:odbc:subname";
  60.    
  61.  public static void main (String args []) 
  62.  { 
  63.      try{
  64.  
  65.         Connection con;
  66.  
  67.     // caricamento driver
  68.  
  69.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  70.     
  71.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  72.     
  73.         con =DriverManager.getConnection(Con_URL,"gruppoN", "gruppoN");
  74.  
  75.     // inserire chiamate per creare lo statement
  76.  
  77.     Statement st = con.createStatement();
  78.  
  79.     // inserire chiamate per eseguire lo statement
  80.  
  81.     ResultSet rs = st.executeQuery("select count(*) from Studenti");
  82.  
  83.     rs.next();
  84.  
  85.     System.out.println("Numero totale studenti: " + rs.getInt(1));
  86.  
  87.     // chiusura connessione
  88.     
  89.         con.close();
  90.  
  91.       } 
  92.     catch(java.lang.ClassNotFoundException e) {
  93.             System.err.print("ClassNotFoundException: "); 
  94.             System.err.println(e.getMessage());
  95.         }
  96.     catch (SQLException e) {
  97.          while( e!=null){ 
  98.             System.out.println("SQLState: " + e.getSQLState());
  99.             System.out.println("    Code: " + e.getErrorCode());
  100.             System.out.println(" Message: " + e.getMessage());
  101.             e = e.getNextException();
  102.                 }
  103.                 }
  104.     
  105.  
  106.  
  107.    } 
  108. }
  109.  
  110.  
  111. =====================================================================
  112. ESERCIZIO 3
  113. =====================================================================
  114.  
  115. import java.sql.*; 
  116. import java.io.*; 
  117.  
  118. class JdbcQueryNonPrep2
  119.   
  120. // subname deve essere sostituito con l'identificatore ODBC creato
  121.  
  122.  static String Con_URL = "jdbc:odbc:subname";
  123.    
  124.  public static void main (String args []) 
  125.  { 
  126.      try{
  127.  
  128.         Connection con;
  129.  
  130.     // caricamento driver
  131.  
  132.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  133.     
  134.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  135.     
  136.         con =DriverManager.getConnection(Con_URL,"gruppoN", "gruppoN");
  137.  
  138.     // inserire chiamate per creare lo statement
  139.  
  140.     Statement st = con.createStatement();
  141.  
  142.     // inserire chiamate per eseguire lo statement
  143.  
  144.     ResultSet rs = st.executeQuery("select cognome, nome from Studenti where relatore is not null");
  145.  
  146.     // analisi risultato con cursore
  147.  
  148.     while (rs.next())
  149.         {
  150.             System.out.println("Cognome: " + rs.getString(1) + " Nome: " + rs.getString(2));
  151.         }
  152.  
  153.     // chiusura connessione
  154.     
  155.         con.close();
  156.  
  157.       } 
  158.     catch(java.lang.ClassNotFoundException e) {
  159.             System.err.print("ClassNotFoundException: "); 
  160.             System.err.println(e.getMessage());
  161.         }
  162.     catch (SQLException e) {
  163.          while( e!=null){ 
  164.             System.out.println("SQLState: " + e.getSQLState());
  165.             System.out.println("    Code: " + e.getErrorCode());
  166.             System.out.println(" Message: " + e.getMessage());
  167.             e = e.getNextException();
  168.                 }
  169.                 }
  170.     
  171.  
  172.  
  173.    } 
  174. }
  175.  
  176.  
  177. =====================================================================
  178. ESERCIZIO 4
  179. =====================================================================
  180.  
  181. import java.sql.*; 
  182. import java.io.*; 
  183.  
  184. class JdbcQueryPrep
  185.   
  186. // subname deve essere sostituito con l'identificatore ODBC creato
  187.  
  188.  static String Con_URL = "jdbc:odbc:subname";
  189.    
  190.  public static void main (String args []) 
  191.  { 
  192.      try{
  193.  
  194.         Connection con;
  195.  
  196.     // caricamento driver
  197.  
  198.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  199.     
  200.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  201.     
  202.         con =DriverManager.getConnection(Con_URL,"gruppoN", "gruppoN");
  203.  
  204.     // inserire chiamate per creare lo statement
  205.  
  206.     PreparedStatement pst = con.prepareStatement("select * from Esami where Studente = ?");
  207.     
  208.     // inserire chiamate per eventualmente assegnare parametri
  209.  
  210.     pst.setString(1,args[0]);
  211.  
  212.     // inserire chiamate per eseguire lo statement
  213.  
  214.     ResultSet rs = pst.executeQuery();
  215.  
  216.     // analisi risultato con cursore
  217.  
  218.     while (rs.next())
  219.         {
  220.             System.out.println("Corso: " + rs.getString(2));            
  221.         }
  222.  
  223.  
  224.     // inserire chiamate per eventualmente assegnare parametri
  225.  
  226.     pst.setString(1,args[1]);
  227.  
  228.     // inserire chiamate per eseguire lo statement
  229.  
  230.     ResultSet rs = pst.executeQuery();
  231.  
  232.     // analisi risultato con cursore
  233.  
  234.     while (rs.next())
  235.         {
  236.             System.out.println("Corso: " + rs.getString(2));            
  237.         }
  238.  
  239.     // chiusura connessione
  240.     
  241.         con.close();
  242.  
  243.       } 
  244.     catch(java.lang.ClassNotFoundException e) {
  245.             System.err.print("ClassNotFoundException: "); 
  246.             System.err.println(e.getMessage());
  247.         }
  248.     catch (SQLException e) {
  249.          while( e!=null){ 
  250.             System.out.println("SQLState: " + e.getSQLState());
  251.             System.out.println("    Code: " + e.getErrorCode());
  252.             System.out.println(" Message: " + e.getMessage());
  253.             e = e.getNextException();
  254.                 }
  255.                 }
  256.     
  257.  
  258.  
  259.    } 
  260. }
  261.  
  262. =====================================================================
  263. ESERCIZIO 5
  264. =====================================================================
  265.  
  266. import java.sql.*; 
  267. import java.io.*; 
  268.  
  269. class JdbcUpdate
  270.   
  271. // subname deve essere sostituito con l'identificatore ODBC creato
  272.  
  273.  static String Con_URL = "jdbc:odbc:subname";
  274.    
  275.  public static void main (String args []) 
  276.  { 
  277.      try{
  278.  
  279.         Connection con;
  280.  
  281.     // caricamento driver
  282.  
  283.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  284.     
  285.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  286.     
  287.         con =DriverManager.getConnection(Con_URL,"gruppoN", "gruppoN");
  288.  
  289.     // inserire chiamate per creare lo statement
  290.  
  291.     Statement st= con.createStatement();
  292.     
  293.  
  294.     // inserire chiamate per eseguire lo statement
  295.  
  296.     st.executeUpdate("update professori set Stipendio = 2500");    
  297.  
  298.     // chiusura connessione
  299.     
  300.         con.close();
  301.  
  302.       } 
  303.     catch(java.lang.ClassNotFoundException e) {
  304.             System.err.print("ClassNotFoundException: "); 
  305.             System.err.println(e.getMessage());
  306.         }
  307.     catch (SQLException e) {
  308.          while( e!=null){ 
  309.             System.out.println("SQLState: " + e.getSQLState());
  310.             System.out.println("    Code: " + e.getErrorCode());
  311.             System.out.println(" Message: " + e.getMessage());
  312.             e = e.getNextException();
  313.                 }
  314.                 }
  315.     
  316.  
  317.  
  318.    } 
  319. }
  320.  
  321. =====================================================================
  322. ESERCIZIO 6
  323. =====================================================================
  324.  
  325.  
  326. import java.sql.*; 
  327. import java.io.*; 
  328.  
  329. class JdbcProc
  330.   
  331. // subname deve essere sostituito con l'identificatore ODBC creato
  332.  
  333.  static String Con_URL = "jdbc:odbc:subname";
  334.    
  335.  public static void main (String args []) 
  336.  { 
  337.      try{
  338.  
  339.         Connection con;
  340.  
  341.     // caricamento driver
  342.  
  343.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  344.     
  345.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  346.     
  347.         con =DriverManager.getConnection(Con_URL,"gruppoN", "gruppoN");
  348.  
  349.     // inserire chiamate per creare lo statement
  350.  
  351.     Statement st = con.createStatement();
  352.     
  353.     String call = "exec CambiaDenCorso '" +  args[0] + " " + args[1] + "'";
  354.  
  355.     // inserire chiamate per eseguire lo statement
  356.  
  357.     st.executeUpdate(call);
  358.  
  359.     // chiusura connessione
  360.     
  361.         con.close();
  362.  
  363.       } 
  364.     catch(java.lang.ClassNotFoundException e) {
  365.             System.err.print("ClassNotFoundException: "); 
  366.             System.err.println(e.getMessage());
  367.         }
  368.     catch (SQLException e) {
  369.          while( e!=null){ 
  370.             System.out.println("SQLState: " + e.getSQLState());
  371.             System.out.println("    Code: " + e.getErrorCode());
  372.             System.out.println(" Message: " + e.getMessage());
  373.             e = e.getNextException();
  374.                 }
  375.                 }
  376.     
  377.  
  378.  
  379.    } 
  380. }
  381.