<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fabio Falcinelli &#187; JDBC</title>
	<atom:link href="http://www.fabiofalcinelli.it/tag/jdbc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fabiofalcinelli.it</link>
	<description>A mix of code and photos...</description>
	<lastBuildDate>Sat, 29 May 2010 12:16:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Shutdown HSQLDB</title>
		<link>http://www.fabiofalcinelli.it/2009/05/shutdown-hsqldb/</link>
		<comments>http://www.fabiofalcinelli.it/2009/05/shutdown-hsqldb/#comments</comments>
		<pubDate>Sat, 23 May 2009 09:35:43 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Landscapes]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[Calabria]]></category>
		<category><![CDATA[Fiumefreddo Bruzio]]></category>
		<category><![CDATA[HSQLDB]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tramonto]]></category>

		<guid isPermaLink="false">http://www.fabiofalcinelli.it/?p=221</guid>
		<description><![CDATA[Ultimamente sto usando Hypersonic SQL DB in un software che sto scrivendo. Il driver utilizzato è &#8220;embedded&#8221; ovvero non è presente un server in attesa di ricevere comandi SQL, ma il database è un file gestito direttamente dal driver di hypersonic. Con questo tipo di accesso però, avevo notato perdite di dati sul database, non [...]]]></description>
			<content:encoded><![CDATA[<p>Ultimamente sto usando Hypersonic SQL DB in un software che sto scrivendo. Il driver utilizzato è &#8220;<em>embedded</em>&#8221; ovvero non è presente un server in attesa di ricevere comandi SQL, ma il database è un file gestito direttamente dal driver di hypersonic.</p>
<p>Con questo tipo di accesso però, avevo notato perdite di dati sul database, non tutto infatti diventava persistente e, certo di non aver scritto codice errato, iniziavo a pensare di non capirci più nulla.</p>
<p>Il fatto è che, a differenza di quanto ero solito fare con altri RDBMS, non è sufficiente chiudere una connessione con</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></div>
<p>al termine dell&#8217;esecuzione del programma, il driver effettua caching delle tabelle per ottimizzare gli accessi al file system e non tutte le modifiche vengono scritte sul file.</p>
<p>È necessario fornire al driver l&#8217;istruzione di &#8220;SHUTDOWN&#8221; di modo che tutti i dati eventualmente ancora in cache vengano correttamente resi persistenti:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">Statement</span> shutdown <span style="color: #339933;">=</span> connection.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SHUTDOWN&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
shutdown.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
shutdown.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
connection.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Esiste inoltre, una funzione di shutdown che effettua anche una riorganizzazione del database ed una compressione dei dati. Essendo un&#8217;operazione lenta è bene non usarla troppo di frequente, il criterio d&#8217;uso è lo stesso di &#8220;SHUTDOWN&#8221;, &#8220;SHUTDOWN COMPACT&#8221;.</p>
<p>Alla prossima :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/05/shutdown-hsqldb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Statement vs PreparedStatement</title>
		<link>http://www.fabiofalcinelli.it/2009/01/statement-vs-preparedstatement/</link>
		<comments>http://www.fabiofalcinelli.it/2009/01/statement-vs-preparedstatement/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 12:50:49 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://fabiofalcinelli.netsons.org/?p=114</guid>
		<description><![CDATA[Quando frequentavo l&#8217;università mi chiedevo quale fosse effettivamente la differenza tra uno Statement e un PreparedStatement Java. Fondamentalmente mi permettevano di raggiungere lo stesso risultato in maniera lievemente diversa. In realtà i due costrutti sono molto più diversi di quanto non si creda. Ogni comando SQL ricevuto dal DBMS viene elaborato seguendo 3 fasi: Compilazione [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Quando frequentavo l&#8217;università mi chiedevo quale fosse effettivamente la differenza tra uno Statement e un PreparedStatement Java. Fondamentalmente mi permettevano di raggiungere lo stesso risultato in maniera lievemente diversa.</p>
<p style="text-align: justify;">In realtà i due costrutti sono molto più diversi di quanto non si creda. Ogni comando SQL ricevuto dal DBMS viene elaborato seguendo 3 fasi:</p>
<ol style="text-align: justify;">
<li>Compilazione (parsing del comando SQL);</li>
<li>Esecuzione;</li>
<li>Fetching  (restituzione del risultato al client).</li>
</ol>
<p style="text-align: justify;">Supponendo che vengano eseguite molte operazioni, la classe PreparedStatement permette di eseguire il passo di compilazione una sola volta, la prima, e poi di aggiornarne i valori in seguito:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">PreparedStatement</span> pstmt <span style="color: #339933;">=</span> conn.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;SELECT * FROM TABELLA WHERE CAMPO = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003399;">Iterator</span> ricerca <span style="color: #339933;">=</span> lista.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>ricerca.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp;pstmt.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>ricerca.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> pstmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Get results</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">//...</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p style="text-align: justify;">Questo si concretizza in un miglioramento delle performance, generalmente. Ciò non è sempre vero, ci sono altri fattori che entrano in gioco che possono rendere uno Statement più veloce di un PreparedStatement. A mio avviso anche se fosse, una leggera diminuzione di performance (che dipende moltissimo dal database utilizzato) per il PreparedStatement è sicuramente ben accetta data la sua grande flessibilità. Non solo, dalle <a title="Oracle - PreparedStatement vs Statment" href="http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.html#35_02" target="_blank">FAQ Oracle</a> ho appreso anche che ci sono dei benefici in termini di sicurezza nell&#8217;utilizzo di PreparedStatement: il binding dei dati, infatti, eviterebbe un considerevole numero di attacchi di tipo SQL Injection. La FAQ infatti riporta:</p>
<blockquote>
<p style="text-align: justify;"><em>&#8220;Statements may be slightly faster if you are only going to execute the SQL once. PreparedStatements are much faster when the SQL will be executed more than once. If you are using the statement cache, which you should, getting a statement from the cache is the same as executing the same statement.</em></p>
<p><em>In general we strongly recommend that you use PreparedStatements. This is especially true if you are sending user provided data in the SQL. By binding the data to a PreparedStatement parameter you can prevent most SQL injection attacks. Any performance advantage of using Statements is negligible.&#8221;</em></p></blockquote>
<p style="text-align: justify;">Insomma, per i miei gusti, non c&#8217;è motivo di usare Statement e PreparedStatement è la classe vincente.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/01/statement-vs-preparedstatement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Oracle 10g &#8211; Upsert</title>
		<link>http://www.fabiofalcinelli.it/2009/01/oracle-10g-upsert/</link>
		<comments>http://www.fabiofalcinelli.it/2009/01/oracle-10g-upsert/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 12:03:07 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Nature]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://fabiofalcinelli.netsons.org/?p=17</guid>
		<description><![CDATA[Recentemente mi è capitato di dover eseguire una insert or update, anche detta upsert, su Oracle 10g. Su MySQL ero solito usare la forma &#8220;INSERT &#8230; ON DUPLICATE KEY UPDATE &#8230;&#8221; che funziona piuttosto bene, ma che non era supportata dal database che stavo utilizzando. La versione 10g di Oracle, però, supporta l&#8217;operazione &#8220;MERGE&#8221;, entrata [...]]]></description>
			<content:encoded><![CDATA[<p>Recentemente mi è capitato di dover eseguire una <strong>insert or update</strong>, anche detta <strong>upsert</strong>, su Oracle 10g. Su MySQL ero solito usare la forma &#8220;INSERT &#8230; ON DUPLICATE KEY UPDATE &#8230;&#8221; che funziona piuttosto bene, ma che non era supportata dal database che stavo utilizzando.</p>
<p>La versione 10g di Oracle, però, supporta l&#8217;operazione &#8220;MERGE&#8221;, entrata a far parte dello standard SQL nel 2003, e si è rivelata molto comoda utilizzandola tramite JDBC.</p>
<p>Supponiamo di dover memorizzare in una tabella l&#8217;ultima pagina visitata da un utente in un dato giorno, possiamo utilizzare il seguente prepared statement java:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">PreparedStatement</span> pstmt <span style="color: #339933;">=</span> conn.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span><br />
<span style="color: #0000ff;">&quot;MERGE INTO last_page_table dst &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;USING ( &quot;</span><span style="color: #339933;">+</span><br />
<span style="color: #0000ff;">&quot;SELECT <span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #339933;">+</span>username<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\'</span> USER_ID, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;TO_DATE(<span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">sql</span>.<span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\'</span>YYYY-MM-DD') &nbsp;DATE_FIELD, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #339933;">+</span>lastPage<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\'</span> LAST_PAGE &quot;</span><span style="color: #339933;">+</span><br />
<span style="color: #0000ff;">&quot;FROM DUAL) src &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;ON (dst.USER_ID = src.USER_ID AND &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;dst.DATE_FIELD = src.DATE_FIELD) &quot;</span><span style="color: #339933;">+</span><br />
<span style="color: #0000ff;">&quot;WHEN MATCHED THEN &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;UPDATE SET dst.LAST_PAGE = src.LAST_PAGE &quot;</span><span style="color: #339933;">+</span><br />
<span style="color: #0000ff;">&quot;WHEN NOT MATCHED THEN &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;INSERT (dst.USER_ID, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;dst.DATE_FIELD, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;dst.LAST_PAGE) &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; <span style="color: #0000ff;">&quot;VALUES (src.USER_ID, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;src.DATE_FIELD, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;src.LAST_PAGE)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>In sostanza, l&#8217;operazione &#8220;MERGE&#8221; funziona tra tabelle, prende i valori di una e li inserisce o aggiorna nell&#8217;altra in base al matching della condizione nella clausola &#8220;ON&#8221;.<br />
Tuttavia, la necessità non era di utilizzare valori preesistenti di una tabella ed inserirli o aggiornarli nell&#8217;altra, ma l&#8217;effettivo popolamento della tabella. Esigenza risolta appoggiandosi alla tabella &#8220;DUAL&#8221;.<br />
Ovviamente, invece di inserire direttamente nella stringa i valori necessari, si poteva utilizzare anche il &#8216;?&#8217; e chiamare poi le funzioni della classe PreparedStatement.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">PreparedStatement</span> pstmt <span style="color: #339933;">=</span> conn.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span><br />
<span style="color: #0000ff;">&quot;MERGE INTO last_page_table dst &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;USING ( &quot;</span><span style="color: #339933;">+</span><br />
<span style="color: #0000ff;">&quot;SELECT ? USER_ID, ? &nbsp;DATE_FIELD, ? LAST_PAGE &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;FROM DUAL) src &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;ON (dst.USER_ID = src.USER_ID AND &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;dst.DATE_FIELD = src.DATE_FIELD) &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;WHEN MATCHED THEN &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;UPDATE SET dst.LAST_PAGE = src.LAST_PAGE &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;WHEN NOT MATCHED THEN &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;INSERT (dst.USER_ID, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;dst.DATE_FIELD, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;dst.LAST_PAGE) &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;VALUES (src.USER_ID, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;src.DATE_FIELD, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;src.LAST_PAGE)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Se non vi sono particolari necessità in termini di performance, sicurezza, flessibilità, in genere preferisco inserire tutto direttamente nella stringa in modo da poterla scrivere facilmente su un file di log prima di inviarla al driver:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">String</span> sqlMerge <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MERGE INTO ...&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #003399;">PreparedStatement</span> pstmt <span style="color: #339933;">=</span> conn.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span>sqlMerge<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Executing &quot;</span><span style="color: #339933;">+</span>sqlMerge<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
pstmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/01/oracle-10g-upsert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
