Advertisement
giorgioancuta

JSP & JDBC

May 18th, 2018
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 6.61 KB | None | 0 0
  1.  --/ index.html /--
  2.  
  3. <!doctype html>
  4. <html>
  5.     <head>
  6.         <title>Accesso</title>
  7.     </head>
  8.     <body>
  9.             <h1>Donazione</h1>
  10.             <form action="ciao.jsp" method="post">
  11.             Nome<input type="text" name="nome" required><br>
  12.             Cognome<input type="text" name="cognome" required><br>
  13.             Donazione<input type="text" name="donazione" required><br>
  14.             <input type="submit">
  15.             <br>
  16.             </form>
  17.     </body>
  18. </html>
  19.  
  20.  --/ ciao.jsp /--
  21.  
  22. <html>
  23.     <head>
  24.         <title>JSP e Database mySQL</title>
  25.     </head>
  26.     <body>
  27.     <!--Import API JDBC-->
  28.     <%@ page import="java.sql.*" %>
  29.     <table border="1">
  30.     <%
  31.     // Caricamento driver per connessione al database mySQL
  32.         try{
  33.             Class.forName("com.mysql.jdbc.Driver");
  34.         }
  35.         catch(Exception e)
  36.         {
  37.             out.println("Errore: " + e);
  38.         }
  39.     // Apertura connessione verso il database
  40.         Connection connessione = null;
  41.         String databaseURL = "jdbc:mysql://localhost:3306/esercitazione";
  42.         try{
  43.             connessione = DriverManager.getConnection(databaseURL, "root", "");
  44.         //  connessione = DriverManager.getConnection("jdbc:mysql://localhost:3306/esercitazione?user=root&password=");
  45.         }
  46.         catch(Exception e){
  47.             out.println("Errore: " + e);
  48.         }
  49.     // Ottenimento dello statement per interagire con il database
  50.         try{
  51.             // Creazione dello statement
  52.             Statement statement = connessione.createStatement();
  53.             String query = "SELECT * FROM persone";
  54.             // Creazione del result set
  55.             ResultSet rs = statement.executeQuery(query);
  56.             if(rs != null)
  57.             {
  58.                 ResultSetMetaData rsmd = null;
  59.                 rsmd = rs.getMetaData();
  60.  
  61.                 out.println("<pre>");
  62.  
  63.                 int numeroColonne = rsmd.getColumnCount();
  64.  
  65.                 out.println("Sono presenti " + numeroColonne + " colonne nel database" + "\t");
  66.  
  67.                 for (int i = 1; i <= numeroColonne; i++)
  68.                     {
  69.                     out.println(rsmd.getColumnName(i) + "\t tipo " + rsmd.getColumnTypeName(i));  
  70.                     }
  71.  
  72.                 out.println("<br><br>");
  73.  
  74.                 for (int i = 1; i <= numeroColonne; i++)
  75.                     {
  76.                     out.print("<b>"+rsmd.getColumnName(i)+"</b>" + "\t");  
  77.                     }
  78.  
  79.                 while (rs.next())
  80.                     {
  81.                     out.println();  
  82.                     for (int i = 1; i <= numeroColonne; i++)
  83.                         {  
  84.                         out.print(rs.getString(i) + "\t");
  85.                         }  
  86.                     }
  87.  
  88.                   out.println("</pre>");
  89.             }
  90.         }
  91.         catch(Exception e){
  92.             out.println("Errore: " + e);
  93.         }
  94.  
  95.         // Chiusura della connessione
  96.  
  97.         try{  
  98.         connessione.close();
  99.         }
  100.         catch (Exception e)
  101.         {
  102.             out.println("Errore: " + e);
  103.         }
  104.  
  105.         out.close();
  106.     %>
  107.     </table>
  108.     </body>
  109. </html>
  110.  
  111. // RICERCA
  112.  
  113. <%@ page import="java.sql.*" %>
  114. <%
  115. if(request.getParameter("btn")!=null)
  116. {
  117.    
  118.  
  119. // carico il driver per la connessione al DB MySQL
  120. String DRIVER = "com.mysql.jdbc.Driver";
  121.  
  122.  
  123. try{
  124.  Class.forName(DRIVER);
  125. }
  126. catch (ClassNotFoundException e) {
  127.  //System.err.println("Driver non trovato" + e);
  128.    out.println("ERRORONE");
  129. }
  130.  
  131.  
  132.  
  133.  
  134. // riferimento al database: connessione Mysql
  135. String URL_mioDB = "jdbc:mysql://localhost:3306/db_cali";     //3306 di default
  136. // definizione della query
  137. String w = request.getParameter("w");
  138. String query="SELECT * FROM users WHERE user LIKE '%"+w+"%' OR nome LIKE '%"+w+"%' OR cognome LIKE '%"+w+"%';";
  139.  
  140. Connection conn=null;
  141. try
  142. {
  143.     conn=DriverManager.getConnection(URL_mioDB,"root","");
  144.  
  145. }
  146. catch(Exception e){
  147. out.println(e);
  148. }
  149.  
  150.  
  151. try{
  152.     Statement st=conn.createStatement();
  153.     ResultSet rs=st.executeQuery(query);
  154.  
  155.     while(rs.next())
  156.     {
  157.         out.println(rs.getString(2)+ " "+rs.getString(2));
  158.     }
  159. }
  160. catch(Exception e)
  161. {
  162.     out.println(e);
  163. }
  164.  
  165.  
  166. /*finally
  167. {
  168.     conn.close();
  169. }*/
  170. }
  171. else
  172. {
  173.     %>
  174.     <form action="">
  175.         <input type="text" name="w">
  176.         <input type="submit" name="btn" value="Invia">
  177.     </form>
  178.    
  179.     <%
  180. }
  181. %>
  182.  
  183. // GESTIONE
  184.  
  185. <%@ page import="java.sql.*" %>
  186. <%
  187. if(request.getParameter("btn")!=null)
  188. {
  189.    
  190.  
  191. // carico il driver per la connessione al DB MySQL
  192. String DRIVER = "com.mysql.jdbc.Driver";
  193.  
  194.  
  195. try{
  196.  Class.forName(DRIVER);
  197. }
  198. catch (ClassNotFoundException e) {
  199.  //System.err.println("Driver non trovato" + e);
  200.    out.println("ERRORONE");
  201. }
  202.  
  203.  
  204.  
  205.  
  206. // riferimento al database: connessione Mysql
  207. String URL_mioDB = "jdbc:mysql://localhost:3306/db_cali";     //3306 di default
  208. // definizione della query
  209. String query = request.getParameter("query");
  210.  
  211. Connection conn=null;
  212. try
  213. {
  214.     conn=DriverManager.getConnection(URL_mioDB,"root","");
  215.  
  216. }
  217. catch(Exception e){
  218. out.println(e);
  219. }
  220.  
  221.  
  222. try{
  223.     Statement st=conn.createStatement();
  224.    int ris=st.executeUpdate(query);
  225.  
  226.    
  227.         out.println(ris);
  228.    
  229. }
  230. catch(Exception e)
  231. {
  232.     out.println(e);
  233. }
  234.  
  235.  
  236. finally
  237. {
  238.     conn.close();
  239. }
  240. }
  241. else
  242. {
  243.     %>
  244.     <form action="">
  245.         <input type="text" name="query">
  246.         <input type="submit" name="btn" value="Invia">
  247.     </form>
  248.    
  249.     <%
  250. }
  251. %>
  252.  
  253. // INTERROGA
  254.  
  255. <%@ page import="java.sql.*" %>
  256. <%
  257. if(request.getParameter("btn")!=null)
  258. {
  259.    
  260.  
  261. // carico il driver per la connessione al DB MySQL
  262. String DRIVER = "com.mysql.jdbc.Driver";
  263.  
  264.  
  265. try{
  266.  Class.forName(DRIVER);
  267. }
  268. catch (ClassNotFoundException e) {
  269.  //System.err.println("Driver non trovato" + e);
  270.    out.println("ERRORONE");
  271. }
  272.  
  273.  
  274.  
  275.  
  276. // riferimento al database: connessione Mysql
  277. String URL_mioDB = "jdbc:mysql://localhost:3306/db_cali";     //3306 di default
  278. // definizione della query
  279. String query = request.getParameter("query");
  280.  
  281. Connection conn=null;
  282. try
  283. {
  284.     conn=DriverManager.getConnection(URL_mioDB,"root","");
  285.  
  286. }
  287. catch(Exception e){
  288. out.println(e);
  289. }
  290.  
  291.  
  292. try{
  293.     Statement st=conn.createStatement();
  294.     ResultSet rs=st.executeQuery(query);
  295.  
  296.     while(rs.next())
  297.     {
  298.         out.println(rs.getString(1)+ " "+rs.getString(2));
  299.     }
  300. }
  301. catch(Exception e)
  302. {
  303.     out.println(e);
  304. }
  305.  
  306.  
  307. finally
  308. {
  309.     conn.close();
  310. }
  311. }
  312. else
  313. {
  314.     %>
  315.     <form action="">
  316.         <input type="text" name="query">
  317.         <input type="submit" name="btn" value="Invia">
  318.     </form>
  319.    
  320.     <%
  321. }
  322. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement