Advertisement
Guest User

Untitled

a guest
May 18th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.94 KB | None | 0 0
  1. ---- index.jsp ----
  2.  
  3. <html>
  4.     <body>
  5.         <form action ="databbeis.jsp" method="post">
  6.             Inserisci il voto minimo:<input type="text" name="voto">
  7.             <input type="submit" value="Invia">
  8.         </form>
  9.     </body>
  10. </html>
  11.  
  12. ---- databbeis.jsp ----
  13.  
  14. <HTML>
  15. <BODY><TITLE>JSP e database MySQL</TITLE></BODY>
  16. <H2>JSP e database MySQL</H2>
  17.  
  18.  
  19. <%@ page import="java.sql.*"    %>                     
  20.  
  21. <TABLE BORDER="1">
  22. <%                              // 0. IMPORT api jdbc
  23.  try{  
  24.    Class.forName("com.mysql.jdbc.Driver");          // 1. carico il driver per la connessione al DB MySQL
  25. }  catch (Exception e)
  26.  {
  27.  out.println("Ciao" + e);
  28. }
  29.  String URL = "jdbc:mysql://localhost/classe?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";  
  30.        //So che questa roba ^ ti spaventa, ma non devi impararla a memoria dato che a scuola funziona normalmente, a me
  31.        // dava un problema che la time zone non era definita in jdbc, quindi ho dovuto fare a modo mio
  32.  Connection connessione = null;                 // 2. apro la connessione verso il database
  33.  try{                                  
  34.     connessione = DriverManager.getConnection(URL,"root","");
  35.  
  36.  
  37. }  catch (Exception e) { out.println("Errore" + e); }
  38.  
  39.                int ct = 0;
  40.                float voto = 0;
  41. try{
  42.            Statement statement = connessione.createStatement();
  43.            String input = request.getParameter("voto");
  44.            String query = ("SELECT voto FROM alunni WHERE voto >= " + input);
  45.             ResultSet result = statement.executeQuery(query);
  46.             if(result != null){
  47.                 ResultSetMetaData rsmd = result.getMetaData();
  48.                 while(result.next()){
  49.                     ct++;
  50.                     voto+= result.getInt(1);
  51.                 }
  52.                 voto = voto/ct;
  53.             }
  54.         out.println("Il voto medio e': " + voto);
  55.         }
  56.         catch(Exception e)
  57.         {
  58.             out.println("Questo" +e);
  59.         }
  60.  
  61.  try{                                            // 7. chiusura connessione  
  62.      connessione.close();
  63.     }  catch (Exception e) { out.println("Hah" + e); }
  64.  
  65.  out.close();                                   // 8. chiusura oggetto di output
  66. %>
  67. </TABLE>
  68. </BODY>
  69. </HTML>
  70.  
  71. ---- db_2.jsp ----
  72.  
  73. <HTML>
  74. <BODY><TITLE>JSP e database MySQL</TITLE></BODY>
  75. <H2>JSP e database MySQL</H2>
  76. <%@ page import="java.sql.*" %>
  77.  
  78. <TABLE BORDER="1">
  79. <%
  80. // carico il driver per la connessione al DB MySQL
  81. try{
  82.  Class.forName("com.mysql.jdbc.Driver");
  83. }
  84. catch (ClassNotFoundException e) {
  85.  System.err.println("Driver non trovato" + e);
  86. }
  87.  
  88. // riferimento al database: connessione Mysql
  89. String URL_mioDB = "jdbc:mysql://localhost:3306/classe";     //3306 di default, classe è il nome del DB
  90.  
  91. // apro la connesione verso il database
  92. Connection connessione = null;
  93. try{                            
  94.  connessione = DriverManager.getConnection(URL_mioDB,"root","");
  95. }
  96. catch (Exception e){
  97.  System.err.println("Errore nella connessione col database: " + e);
  98. }
  99.  
  100.  
  101. // attivo Statement per interagire con il database
  102. try{
  103.  Statement statement = connessione.createStatement();
  104.  
  105. // interrogo il DBMS mediante una query SQL
  106.  String query = "SELECT cognome, nome FROM alunni";
  107.  ResultSet resultSet = statement.executeQuery(query);
  108.  
  109. // Scorro e mostro i risultati.
  110.  out.println("<PRE>");
  111.   out.println("<B>cognome              nome</B><BR>");
  112.  
  113.   while (resultSet.next()) {
  114.    String cognome = resultSet.getString(1);
  115.     // getInt() getFloat() getBoolean() getDouble() getString() getDate()
  116.    String nome = resultSet.getString(2);
  117.        out.println( cognome+" - "+nome);
  118.   }
  119.   out.println("</PRE>");
  120.  }
  121.  catch (Exception e){ System.err.println(e); }
  122.  
  123. // chiusura connessione
  124.  try{                            
  125.   connessione.close();
  126.  }
  127.  catch (Exception e) {  System.err.println(e); }
  128.  
  129.  out.close();                  
  130. %>
  131. </TABLE>
  132. </BODY>
  133. </HTML>
  134. <%--
  135.  
  136. //QUERY DI MANIPOLAZIONE
  137. /* esito = statement.executeUpdate("INSERT INTO tab1 (dati) values ("+valore+")"))
  138.    if (esito == 1)
  139.        out.println("inserimento eseguito correttamente");
  140.     else
  141.        out.println("inserimento non eseguito");
  142. */
  143.  
  144. //QUERY DI INTERROGAZIONE
  145. // interrogo il DBMS mediante una query SQL
  146.  String query = "SELECT cognome, nome FROM alunni";
  147.  ResultSet resultSet = statement.executeQuery(query);
  148.  
  149.  
  150.  
  151. --%>
  152.  
  153. ---- db_5bis.jsp ----
  154.  
  155. <HTML>
  156. <BODY><TITLE>JSP e database MySQL</TITLE></BODY>
  157. <H2>JSP e database MySQL</H2>
  158.  
  159.  
  160. <%@ page import="java.sql.*"    %>                     
  161.  
  162. <TABLE BORDER="1">
  163. <%                              // 0. IMPORT api jdbc
  164.  try{  
  165.    Class.forName("com.mysql.jdbc.Driver");          // 1. carico il driver per la connessione al DB MySQL
  166. }  catch (Exception e)
  167.  {
  168.  out.println(e);
  169. }
  170.  String URL = "jdbc:mysql://localhost/classe?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  171.  Connection connessione = null;                 // 2. apro la connessione verso il database
  172.  try{                                  
  173.     connessione = DriverManager.getConnection(URL,"root","");
  174.  
  175.  
  176. }  catch (Exception e) { out.println(e); }
  177.  
  178.  
  179. try{
  180.     Statement statement = connessione.createStatement();    // 3. ottengo lo Statement per interagire con il database
  181.  
  182.    String query = request.getParameter("query");
  183.     ResultSet resultSet = statement.executeQuery(query);        // 4. interrogo il DBMS mediante query SQL
  184.  
  185.   if (resultSet  != null) {
  186.  
  187.   ResultSetMetaData rsmd = null;
  188. rsmd = resultSet.getMetaData();     // 5. recupero metadata dal ResulSet
  189.        
  190.    out.println("<PRE>");                    // 6. Scorro e mostro i risultati (colonne dinamiche)
  191.  
  192. //public int getColumnCount()throws SQLException    Ritorna il numero totale di colonne nell'oggetto ResutlSet 
  193. //public String getColumnName(int index)throws SQLException   Ritorna il nome della colonna dell'indice specificato (tipo nome,cognome e cagate varie)
  194. //public String getColumnTypeName(int index)throws SQLException   Ritorna il tipo di colonna dell'indice (int,string...)
  195. //public String getTableName(int index)throws SQLException    Ritorna il nome della tabella per l'indice speicificato (a cosa serve boh)
  196.  
  197.  
  198.    int numCols = rsmd.getColumnCount();
  199.  
  200.    out.println("sono presenti n. "+ numCols + "\t");
  201.  
  202.    for (int i = 1; i <= numCols; i++)
  203.     { out.println(rsmd.getColumnName(i) + "\t"+ rsmd.getColumnTypeName(i));  }
  204.  
  205.     out.println("<BR><BR>");  
  206.  
  207.  
  208.   for (int i = 1; i <= numCols; i++)
  209.     { out.print(rsmd.getColumnName(i) + "\t");  }
  210.  
  211.  while (resultSet.next())
  212.  {
  213.     out.println();  
  214.     for (int i = 1; i <= numCols; i++)     
  215.        out.print(resultSet.getString(i) + "\t");
  216.    
  217.  }
  218.  
  219. }
  220.  out.println("</PRE>");
  221.  }  catch (Exception e) { out.println(e); }
  222.  
  223.  try{                                            // 7. chiusura connessione  
  224.      connessione.close();
  225.     }  catch (Exception e) { out.println(e); }
  226.  
  227.  out.close();                                   // 8. chiusura oggetto di output
  228. %>
  229. </TABLE>
  230. </BODY>
  231. </HTML>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement