Laboratorio di sistemi JDBC Jsp [Java] File index.jsp <%@page

Laboratorio di sistemi
JDBC
Jsp [Java]
File index.jsp
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@page import=&quot;java.sql.*&quot;%&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Tabella Books, database Shop&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h4&gt;Visualizza i dati della tabella Books, database Shop&lt;/h4&gt;
&lt;%
Connection conn = null;
Statement st = null;
try {
// carichiamo i driver di MySQL
Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
} catch(ClassNotFoundException e) {
out.println(&quot;Problemi nel caricamento dei driver&quot;);
return;
}
try {
// predisponiamo la connessione col database
conn = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/shop?
user=root&amp;password=coz123&quot;);
tabella_books.pdf
Pag. 1/5
Cozzetto &copy;
Laboratorio di sistemi
JDBC
Jsp [Java]
} catch(SQLException e) {
out.println(&quot;Problemi nello stabilire la connessione al database&quot;);
return;
}
try {
// usiamo le due costanti indicate di seguito per poter contare il numero di
record del ResultSet
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// Conto il numero di record della tabella books
String sqlStr = &quot;SELECT * FROM books&quot;;
ResultSet rs = st.executeQuery(sqlStr);
// ci posizioniamo sull'ultimo record
rs.last();
// mi faccio dare la posizione che &egrave; anche il numero di record del ResultSet
int rowCount = rs.getRow();
if (rowCount==0) {
out.println(&quot;La query non restituisce risultati&quot;);
return;
}
// sposto il cursore prima del primo record
rs.beforeFirst();
// formattiamo il risultato
out.println(&quot;&lt;table border='1'&gt;&lt;tr&gt;&quot;);
out.println(&quot;&lt;td&gt;Book ID&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;Title&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;Author&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;Price&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;Publisher&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;Category ID&lt;/td&gt;&quot;);
out.println(&quot;&lt;/tr&gt;&lt;tr&gt;&quot;);
// effettuiamo un ciclo all'interno del ResultSet
// la prima volta che viene eseguito rs.next(), il cursore
// viene posizionato correttamente sul primo record
while (rs.next()) {
// stampiamo i valori delle colonne
out.println(&quot;&lt;td&gt;&quot;+rs.getInt(1)+&quot;&lt;/td&gt;&quot;);
// possiamo scrivere anche in alternativa
// out.println(&quot;&lt;td&gt;&quot;+rs.getString(&quot;title&quot;)+&quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot;+rs.getString(2)+&quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot;+rs.getString(3)+&quot;&lt;/td&gt;&quot;);
tabella_books.pdf
Pag. 2/5
Cozzetto &copy;
Laboratorio di sistemi
JDBC
Jsp [Java]
out.println(&quot;&lt;td&gt;&quot;+rs.getDouble(4)+&quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot;+rs.getString(5)+&quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot;+rs.getInt(6)+&quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;/tr&gt;&quot;);
} // fine while
out.println(&quot;&lt;/table&gt;&quot;);
out.println(&quot;&lt;br/&gt;La query restituisce &quot;+rowCount+&quot; record&quot;);
rs.close();
} catch(SQLException e) {
out.println(&quot;Errore nell'esecuzione della query&quot;);
return;
}
try {
st.close();
conn.close();
} catch(SQLException e) {
out.println(&quot;Impossibile effettuare la chiusura della connessione&quot;);
return;
}
%&gt;
&lt;/body&gt;
&lt;/html&gt;
tabella_books.pdf
Pag. 3/5
Cozzetto &copy;
Laboratorio di sistemi
JDBC
Jsp [Java]
File indexJSTL.jsp
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;
&lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/sql&quot; prefix=&quot;sql&quot;%&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Visualizza i dati della tabella Books, database Shop mediante
JSTL&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h4&gt;Visualizza i dati della tabella Books, database Shop mediante JSTL&lt;/h4&gt;
&lt;sql:setDataSource driver=&quot;org.gjt.mm.mysql.Driver&quot;
url=&quot;jdbc:mysql://localhost:3306/shop&quot; user=&quot;root&quot; password=&quot;coz123&quot;/&gt;
&lt;!-- mi faccio dare i tutti i generi senza ripetizioni --&gt;
&lt;sql:query var=&quot;result&quot;&gt;
SELECT * FROM books
&lt;/sql:query&gt;
&lt;c:choose&gt;
&lt;c:when test=&quot;${result.rowCount==0}&quot;&gt;
La query non restituisce risultati
&lt;/c:when&gt;
&lt;c:otherwise&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;td&gt;Book ID&lt;/td&gt;
&lt;td&gt;Title&lt;/td&gt;
&lt;td&gt;Author&lt;/td&gt;
&lt;td&gt;Price&lt;/td&gt;
tabella_books.pdf
Pag. 4/5
Cozzetto &copy;
Laboratorio di sistemi
JDBC
Jsp [Java]
&lt;td&gt;Publisher&lt;/td&gt;
&lt;td&gt;Category ID&lt;/td&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${result.rows}&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.book_id}&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.title}&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.author}&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.price}&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.publisher}&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.category_id}&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
&lt;p&gt;La query restituisce &lt;c:out value=&quot;${result.rowCount}&quot;/&gt; record&lt;/p&gt;
&lt;/c:otherwise&gt;
&lt;/c:choose&gt;
&lt;/body&gt;
&lt;/html&gt;
tabella_books.pdf
Pag. 5/5
Cozzetto &copy;