<?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>Der Blog von Benny Neugebauer &#187; MySQL</title>
	<atom:link href="http://www.bennyn.de/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bennyn.de</link>
	<description>Alles über die Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 21 May 2012 16:07:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>MySQL NOW() equivalent in Java for prepared statements</title>
		<link>http://www.bennyn.de/programmierung/java/mysql-now-equivalent-in-java-for-prepared-statements.html</link>
		<comments>http://www.bennyn.de/programmierung/java/mysql-now-equivalent-in-java-for-prepared-statements.html#comments</comments>
		<pubDate>Sun, 01 Apr 2012 20:30:59 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[GregorianCalendar]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[JSE]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[NOW()]]></category>
		<category><![CDATA[Prepared Statements]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=3344</guid>
		<description><![CDATA[If you are used to MySQL then you probably know the MySQL function NOW() which inserts the current date and time in a MySQL query. But if you use JDBC and prepared statements, then you can reconstruct this function with a GregorianCalendar (which is the successor of Date): 1 2 3 4 5 6 7 [...]]]></description>
			<content:encoded><![CDATA[<p>If you are used to MySQL then you probably know the MySQL function <code>NOW()</code> which inserts the current date and time in a MySQL query. But if you use JDBC and prepared statements, then you can reconstruct this function with a <code>GregorianCalendar</code> (which is the successor of <code>Date</code>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> insertArticle<span style="color: #009900;">&#40;</span>Article article<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//query = &quot;INSERT INTO articles(title,content,date) VALUES (?,?,NOW())&quot;;</span>
  query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO articles(title,content,date) VALUES (?,?,?)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  statement <span style="color: #339933;">=</span> connection.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  statement.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, article.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  statement.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, article.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  statement.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span>statement<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  statement.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/java/mysql-now-equivalent-in-java-for-prepared-statements.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Insert only in specific columns with MySQL</title>
		<link>http://www.bennyn.de/programmierung/mysql/insert-only-in-specific-columns-with-mysql.html</link>
		<comments>http://www.bennyn.de/programmierung/mysql/insert-only-in-specific-columns-with-mysql.html#comments</comments>
		<pubDate>Sun, 04 Mar 2012 22:08:45 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[columns]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[into]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=3230</guid>
		<description><![CDATA[If you have a big MySQL table and you want to insert data only in specific columns, then you can do this with naming the columns in parentheses after the table name: INSERT INTO `wp_posts`&#40;`post_author`,`post_content`,`post_title`,`post_date`&#41; VALUES &#40;0,'Hello World','Text...',NOW&#40;&#41;&#41;; This works even without apostrophes: INSERT INTO wp_posts&#40;post_author,post_content,post_title,post_date&#41; VALUES &#40;0,'Hello World','Text...',NOW&#40;&#41;&#41;;]]></description>
			<content:encoded><![CDATA[<p>If you have a big MySQL table and you want to insert data only in specific columns, then you can do this with naming the columns in parentheses after the table name:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`wp_posts`</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`post_author`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`post_content`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`post_title`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`post_date`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Hello World'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Text...'</span><span style="color: #66cc66;">,</span>NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This works even without apostrophes:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> wp_posts<span style="color: #66cc66;">&#40;</span>post_author<span style="color: #66cc66;">,</span>post_content<span style="color: #66cc66;">,</span>post_title<span style="color: #66cc66;">,</span>post_date<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Hello World'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Text...'</span><span style="color: #66cc66;">,</span>NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/mysql/insert-only-in-specific-columns-with-mysql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>java.sql.SQLException &#8211; Value can not be represented as java.sql.Date</title>
		<link>http://www.bennyn.de/programmierung/java/java-sql-sqlexception-value-can-not-be-represented-as-java-sql-date.html</link>
		<comments>http://www.bennyn.de/programmierung/java/java-sql-sqlexception-value-can-not-be-represented-as-java-sql-date.html#comments</comments>
		<pubDate>Sun, 04 Mar 2012 21:20:54 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[DATETIME]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=3224</guid>
		<description><![CDATA[If you receive an error that is similar to this: java.sql.SQLException: Value &#8217;0000-00-00 00:00:00&#8242; can not be represented as java.sql.Date Then you should check your database connection. If your connection url looks like jdbc:mysql://localhost/my_database then you should try jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=convertToNull. Other options are: jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=round jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=exception The explanation can be found in &#8220;handling DATETIME values&#8220;.]]></description>
			<content:encoded><![CDATA[<p>If you receive an error that is similar to this:</p>
<blockquote><p>
java.sql.SQLException: Value &#8217;0000-00-00 00:00:00&#8242; can not be represented as java.sql.Date
</p></blockquote>
<p>Then you should check your database connection. If your connection url looks like <code>jdbc:mysql://localhost/my_database</code> then you should try <code>jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=convertToNull</code>. </p>
<p>Other options are:<br />
<code>jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=round</code><br />
<code>jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=exception</code></p>
<p>The explanation can be found in &#8220;<a href="http://stackoverflow.com/a/2629701/451634">handling DATETIME values</a>&#8220;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/java/java-sql-sqlexception-value-can-not-be-represented-as-java-sql-date.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connecting to a MySQL Database with Java (JDBC)</title>
		<link>http://www.bennyn.de/programmierung/java/connecting-to-a-mysql-database-with-java-jdbc.html</link>
		<comments>http://www.bennyn.de/programmierung/java/connecting-to-a-mysql-database-with-java-jdbc.html#comments</comments>
		<pubDate>Sun, 04 Mar 2012 21:12:49 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Connection]]></category>
		<category><![CDATA[Connectivity]]></category>
		<category><![CDATA[Connector]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[JSE]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=3219</guid>
		<description><![CDATA[In the beginning of my study time I&#8217;ve programmed a MySQL connection in Java for a music collection. After 2 years now I have made a remake of to show how to use the Java Database Connectivity (JDBC). All you need is mysql-connector-java-5.0.8-bin.jar. I&#8217;ve written a small sample for the connection with a WordPress database. [...]]]></description>
			<content:encoded><![CDATA[<p>In the beginning of my study time I&#8217;ve programmed a MySQL connection in Java for a music collection. After 2 years now I have made a remake of <a href="http://www.bennyn.de/programmierung/java/datenbankabfragen-mit-jdbc.html" title="JDBC Datenbankanbindung mit MySQL">this code</a> to show how to use the Java Database Connectivity (JDBC). All you need is <a href="http://dev.mysql.com/downloads/connector/j/5.0.html">mysql-connector-java-5.0.8-bin.jar</a>.</p>
<p>I&#8217;ve written a small sample for the connection with a WordPress database.<br />
<span id="more-3219"></span><br />
<strong>DatabaseController.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.mysql.jdbc.PreparedStatement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.mysql.jdbc.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.DriverManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.SimpleDateFormat</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Level</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DatabaseController
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> host <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> password <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>DatabaseController.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> DatabaseController<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> host, <span style="color: #003399;">String</span> name, <span style="color: #003399;">String</span> user, <span style="color: #003399;">String</span> password<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">host</span> <span style="color: #339933;">=</span> host<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">user</span> <span style="color: #339933;">=</span> user<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">password</span> <span style="color: #339933;">=</span> password<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Connection</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Connection</span> connection <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      connection <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdbc:mysql://&quot;</span> <span style="color: #339933;">+</span> host <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;?zeroDateTimeBehavior=convertToNull&quot;</span>, user, password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> <span style="color: #339933;">|</span> <span style="color: #003399;">ClassNotFoundException</span> ex<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      logger.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">SEVERE</span>, ex.<span style="color: #006633;">getLocalizedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> connection<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printWordPressPosts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Connection</span> connection <span style="color: #339933;">=</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM wp_posts&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">PreparedStatement</span> statement <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">PreparedStatement</span><span style="color: #009900;">&#41;</span> connection.<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">ResultSet</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ResultSet</span><span style="color: #009900;">&#41;</span> statement.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
    <span style="color: #003399;">SimpleDateFormat</span> dateFormat <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH:mm:ss.S&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>result.<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>
      <span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> result.<span style="color: #006633;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;post_date&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">String</span> title <span style="color: #339933;">=</span> result.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;post_title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">String</span> content <span style="color: #339933;">=</span> result.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;post_content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>date <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Posted on: &quot;</span> <span style="color: #339933;">+</span> dateFormat.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, Title: &quot;</span> <span style="color: #339933;">+</span> title<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Execution:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
<span style="color: #009900;">&#123;</span>
  DatabaseController controller <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DatabaseController<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span>, <span style="color: #0000ff;">&quot;database&quot;</span>, <span style="color: #0000ff;">&quot;db_user&quot;</span>, <span style="color: #0000ff;">&quot;db_password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  controller.<span style="color: #006633;">printWordPressPosts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/java/connecting-to-a-mysql-database-with-java-jdbc.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Datenbank-Zugriff mit PHP und PDO</title>
		<link>http://www.bennyn.de/programmierung/php/datenbank-zugriff-mit-php-und-pdo.html</link>
		<comments>http://www.bennyn.de/programmierung/php/datenbank-zugriff-mit-php-und-pdo.html#comments</comments>
		<pubDate>Thu, 12 May 2011 19:33:07 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Abfrage]]></category>
		<category><![CDATA[Datenbanken]]></category>
		<category><![CDATA[Eintrag]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PDO]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=2529</guid>
		<description><![CDATA[Beim Zugriff auf eine Datenbank sollte man mit PHP immer auf PDO (PHP Data Objects) zurückgreifen. Durch diese Abstraktionsstufe ist das Datenbank-System später einfacher austauschbar und Prepared Statements lassen sich auch ganz leicht realisieren. Hierzu ein exemplarischer Beispielcode. Code: database.sql 1 2 3 4 5 6 7 8 9 10 11 12 13 DROP DATABASE [...]]]></description>
			<content:encoded><![CDATA[<p>Beim <strong>Zugriff auf eine Datenbank</strong> sollte man mit PHP immer auf <strong>PDO</strong> (PHP Data Objects) zurückgreifen. Durch diese Abstraktionsstufe ist das Datenbank-System später einfacher austauschbar und Prepared Statements lassen sich auch ganz leicht realisieren. Hierzu ein exemplarischer Beispielcode.<br />
<span id="more-2529"></span></p>
<h2>Code:</h2>
<p><b>database.sql</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`dailydjango`</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #ff0000;">`dailydjango`</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;
<span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #ff0000;">`dailydjango`</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`feed_entries`</span> 
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #ff0000;">`id`</span>		<span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>	<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>
	<span style="color: #66cc66;">,</span><span style="color: #ff0000;">`pubdate`</span>	DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>	
	<span style="color: #66cc66;">,</span><span style="color: #ff0000;">`title`</span>	<span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
	<span style="color: #66cc66;">,</span><span style="color: #ff0000;">`link`</span>		<span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
	<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`link`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> InnoDB <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;</pre></td></tr></table></div>

<p><b>code.php</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$HOST</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$DB</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'dailydjango'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$USER</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$PASS</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$TABLE</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'feed_entries'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Test-Daten</span>
<span style="color: #000088;">$test_pubdate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2011-05-01 21:30:45'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test_title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Ein Titel'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test_link</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.bennyn.de/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// MySQL-Eintrag</span>
	try 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$con</span>	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql:host=<span style="color: #006699; font-weight: bold;">$HOST</span>;dbname=<span style="color: #006699; font-weight: bold;">$DB</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$USER</span><span style="color: #339933;">,</span> <span style="color: #000088;">$PASS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$sql</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO <span style="color: #006699; font-weight: bold;">$TABLE</span>(id,pubdate,title,link) VALUES(NULL,:pubdate,:title,:link)&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stmt</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$con</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':pubdate'</span><span style="color: #339933;">,</span><span style="color: #000088;">$test_pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':title'</span><span style="color: #339933;">,</span><span style="color: #000088;">$test_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':link'</span><span style="color: #339933;">,</span><span style="color: #000088;">$test_link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stmt</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$con</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// MySQL-Abfrage</span>
	try 
	<span style="color: #009900;">&#123;</span>  		
		<span style="color: #000088;">$con</span>	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql:host=<span style="color: #006699; font-weight: bold;">$HOST</span>;dbname=<span style="color: #006699; font-weight: bold;">$DB</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$USER</span><span style="color: #339933;">,</span> <span style="color: #000088;">$PASS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$sql</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM <span style="color: #006699; font-weight: bold;">$TABLE</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pubdate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$con</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>  
	catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><u>Hinweis:</u> Die Datenbank-Verbindung sollte nicht immer bei jeder Datenbank-Anfrage neu aufgebaut und abgebaut werden.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/php/datenbank-zugriff-mit-php-und-pdo.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

