<?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>JDBC | LogikDevelopment</title>
	<atom:link href="https://www.logikdev.com/tag/jdbc/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Tue, 25 Apr 2017 13:57:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>
	<item>
		<title>JDBC and the &#8220;zero&#8221; date</title>
		<link>https://www.logikdev.com/2011/02/06/jdbc-and-the-zero-date/</link>
					<comments>https://www.logikdev.com/2011/02/06/jdbc-and-the-zero-date/#respond</comments>
		
		<dc:creator><![CDATA[smoreau]]></dc:creator>
		<pubDate>Sun, 06 Feb 2011 18:25:51 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[zero date]]></category>
		<category><![CDATA[zeroDateTimeBehavior]]></category>
		<guid isPermaLink="false">https://www.logikdev.com/?p=1058</guid>

					<description><![CDATA[<p>A few weeks ago, I was in the process of writing a little script in Java to extract data from a MySQL database. Because this is what we can call a micro project, I decided to simply use JDBC to talk to the database. But when I tried to run my script, I got the [&#8230;]</p>
<p>The post <a href="https://www.logikdev.com/2011/02/06/jdbc-and-the-zero-date/">JDBC and the “zero” date</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>A few weeks ago, I was in the process of writing a little script in Java to extract data from a MySQL database. Because this is what we can call a <em>micro</em> project, I decided to simply use <strong>JDBC</strong> to talk to the database.</p>
<p>But when I tried to run my script, I got the following exception:</p>
<pre>
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Date
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
	at com.mysql.jdbc.ResultSet.getDateFromString(ResultSet.java:2032)
	at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:1956)
	at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:1924)
	at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:1972)
	...
</pre>
<p>After looking at the database, it seems that this exception has been thrown because the fields of type <code>datetime</code> contain the value &#8216;0000-00-00 00:00:00&#8217;. Don&#8217;t ask me why I have this kind of value in the database! Obviously, we would prefer to have a <code>NULL</code> value instead of a &#8220;zero&#8221; date.</p>
<p>There are two solutions to fix this problem:</p>
<ol>
<li>
Replace all the &#8220;zero&#8221; dates by <code>NULL</code> using the following SQL query:</p>
<pre class="brush: sql; light: true; title: ; notranslate">
UPDATE table SET datefield = NULL WHERE datefield = '0000-00-00 00:00:00';
</pre>
</li>
<li>
Add the parameter <code>zeroDateTimeBehavior=convertToNull</code> to the connection url which will automatically convert the &#8220;zero&#8221; date to <code>NULL</code>. Please find below an example of connection url with this parameter:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
jdbc:mysql://localhost:3306/dbname?zeroDateTimeBehavior=convertToNull
</pre>
</li>
</ol>
<p>Personally, I choose the second solution as I didn&#8217;t want to change the original data of the database.</p><p>The post <a href="https://www.logikdev.com/2011/02/06/jdbc-and-the-zero-date/">JDBC and the “zero” date</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.logikdev.com/2011/02/06/jdbc-and-the-zero-date/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
