<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Linglom&#039;s blog &#187; Java</title>
	<atom:link href="http://www.linglom.com/category/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.linglom.com</link>
	<description>Just another IT weblog</description>
	<lastBuildDate>Thu, 27 May 2010 09:56:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</title>
		<link>http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/</link>
		<comments>http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 23:33:19 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Access 2007]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=1032</guid>
		<description><![CDATA[This article is one of the series of Accessing Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction Perform SQL Operations From Part 3: Create a Connection, I show how to create a connection from NetBeans 6.5 [...]


Related posts:<ol><li><a href='http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p><a name="top"></a><br />
This article is one of the series of Accessing Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at <a href="http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/">Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></p>
<p><span id="more-1032"></span></p>
<p><!-- Start AdLogger Wrapping Code -->
<?php @include_once("/usr/local/psa/home/vhosts/linglom.com/httpdocs/adlogger/ad_check_include.php"); if ($show_ads == 'y') { ?>

<script type="text/javascript"><!--
google_ad_client = "pub-7765165459812980";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
//2007-02-17: SquareonBlog
google_ad_channel = "6055254908";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "ce6531";
google_color_text = "000000";
google_color_url = "008000";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<?php } ?>
<!-- End AdLogger Wrapping Code --></p>
<h3>Perform SQL Operations</h3>
<p>From <a href="http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/">Part 3: Create a Connection</a>, I show how to create a connection from NetBeans 6.5 to the Northwind database of Microsoft Access 2007. On this post, you see how to perform basic SQL operations such as SELECT, INSERT, DELETE and UPDATE to the Customers table on Northwind database. </p>
<ul>
<li><a href="#1">Retrieve data from database</a></li>
<li><a href="#2">Update data on database</a></li>
</ul>
<p><a name="1"></a></p>
<h4>Retrieve data from database</h4>
<p>Copy and paste the code below on main method between these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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;Connected!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">con.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>The Code</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #003399;">Statement</span> stmt <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// SQL query command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM Customers&quot;</span><span style="color: #339933;">;</span>
            stmt <span style="color: #339933;">=</span> con.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <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>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Company&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; : &quot;</span>
                        <span style="color: #339933;">+</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;First Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; : &quot;</span>
                        <span style="color: #339933;">+</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Code Explanation:</strong></p>
<ul>
<li>Line 1-2: Declare some variables.</li>
<li>Line 5: Define SQL query string.</li>
<li>Line 6: Create a statement object for sending SQL statements to the database.</li>
<li>Line 7: Executes the SQL statement and returns a single ResultSet object.</li>
<li>Line 8-12: Iterative in the ResultSet object to show results on output window</li>
</ul>
<p><a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-4/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-4/_1.png" width="200" height="113" alt="Query Code" title="Query Code"  /></a></p>
<p>Next, run the project. You see the result on the output window. It displays records from Customers table.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-4/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-4/_2.png" width="200" height="313" alt="Query Result" title="Query Result"  /></a><br />
<a href="#top">Back to top</a></p>
<p><a name="2"></a></p>
<h4>Update data on database</h4>
<p>To insert, update and delete records on MS Access 2007 databases, you can re-use the code from the previous section. But you need to replace the SQL statement and code on this line:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<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>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Company&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; : &quot;</span>
                        <span style="color: #339933;">+</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;First Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; : &quot;</span>
                        <span style="color: #339933;">+</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span></pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Code Explanation: </strong><br />
Executes the given SQL statement which may be INSERT, UPDATE or DELETE statement and returns the row count for the update.</p>
<p><strong>INSERT command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL INSERT command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO Customers (Company,[First Name],&quot;</span> <span style="color: #339933;">+</span>
                    <span style="color: #0000ff;">&quot;[Last Name]) VALUES ('MyCompany','Linglom','My Last Name')&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>UPDATE command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL UPDATE command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE Customers SET [Last Name] = 'New Last Name'&quot;</span> <span style="color: #339933;">+</span>
                            <span style="color: #0000ff;">&quot;WHERE [First Name] = 'Linglom'&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>DELETE command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL DELETE command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM Customers WHERE [First Name] = 'Linglom'&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Summary</h3>
<p>You can download the source code of this series at <a href="http://forum.linglom.com/index.php?topic=685.0">SampleAccess2007.java</a>. The example code will create a connection to Northwind database on Microsoft Access 2007. Then, retrieve records, insert a new record, update and delete the record from Customer table. Here is output of the example code.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-4/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-4/_3.png" width="200" height="111" alt="Update Result" title="Update Result"  /></a><br />
<a href="#top">Back to top</a></p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</title>
		<link>http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/</link>
		<comments>http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 03:45:21 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Access 2007]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=1029</guid>
		<description><![CDATA[This article is one of the series of Accessing MS Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction Create a Connection From Part 2: Prepare Sample Database, I show how to setup Northwind database for Microsoft [...]


Related posts:<ol><li><a href='http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection'>Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection</a></li>
<li><a href='http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database</a></li>
<li><a href='http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p>This article is one of the series of Accessing MS Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at <a href="http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/">Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></p>
<p><span id="more-1029"></span></p>
<h3>Create a Connection</h3>
<p>From <a href="http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/">Part 2: Prepare Sample Database</a>, I show how to setup Northwind database for Microsoft Access 2007. Now let&#8217;s start programming. On this post, you see how to create a connection from NetBeans 6.5 to the Northwind database of Microsoft Access 2007 which located at &#8220;c:\database\Northwind 2007.accdb&#8221;. </p>
<p><!--adsense#Square--></p>
<h3>Step-by-step</h3>
<ol>
<li>Create a new Java Application project on NetBeans. Open NetBeans 6.5.1 and select <strong>File</strong> -> <strong>New Project</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_1.png" width="200" height="150" alt="New Project on NetBeans" title="New Project on NetBeans"  /></a></li>
<li>On <strong>New Project</strong>, select <strong>Java</strong> -> <strong>Java Application</strong>. Click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_2.png" width="200" height="138" alt="Select Java Application" title="Select Java Application"  /></a></li>
<li>On <strong>New Java Application</strong>, type the project name <strong>&#8220;NorthwindSample&#8221;</strong> and click <strong>Finish</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_3.png" width="200" height="129" alt="Type Project Name for New Java Application" title="Type Project Name for New Java Application"  /></a></li>
<li>The project <strong>&#8220;NorthwindSample&#8221;</strong> is created.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_4.png" width="200" height="150" alt="NorthwindSample's Project" title="NorthwindSample's Project"  /></a></li>
<li>Let&#8217;s begin the programming part. First, I need to import some libraries.

<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;">java.sql.*</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_5.png" width="200" height="117" alt="Import Libraries" title="Import Libraries"  /></a></li>
<li>Type the code below to the main method.

<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="java" style="font-family:monospace;"><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;sun.jdbc.odbc.JdbcOdbcDriver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:odbc:Driver={Microsoft Access Driver &quot;</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;(*.mdb, *.accdb)};DBQ=C:<span style="color: #000099; font-weight: bold;">\\</span>Database<span style="color: #000099; font-weight: bold;">\\</span>Northwind 2007.accdb&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Connection</span> con <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;Connected!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        con.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<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;SQL Exception: &quot;</span><span style="color: #339933;">+</span> e.<span style="color: #006633;">toString</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> cE<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;Class Not Found Exception: &quot;</span><span style="color: #339933;">+</span>
                cE.<span style="color: #006633;">toString</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></pre></td></tr></table></div>

<p><strong>Code Explanation:</strong></p>
<ul>
<li><em>Line 2:</em> Class.forName(&#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;); means load the JDBC-ODBC driver.</li>
<li><em>Line 3-4:</em> String url = &#8220;jdbc:odbc:Driver={Microsoft Access Driver &#8221; + &#8220;(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb&#8221;; declare a variable which store a string of the driver name for Microsoft Access 2007 and the location of the Northwind database.</li>
<li><em>Line 5:</em> Make a connection using information on the variable which was created before.</li>
<li><em>Line 6:</em> Display &#8220;Connected!&#8221; on console window.</li>
<li><em>Line 7:</em> Close the connection.</li>
</ul>
<p><a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/6.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_6.png" width="200" height="112" alt="Code to Connect To MS Access 2007" title="Code to Connect To MS Access 2007"  /></a></li>
<li>Compile and run the project. You see the text <strong>&#8220;Connected!&#8221;</strong> on the output window. Otherwise, you should see an exception message.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/7.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-3/_7.png" width="200" height="84" alt="Connected to MS Access 2007's Database" title="Connected to MS Access 2007's Database"  /></a></li>
</ol>
<h3>What&#8217;s Next?</h3>
<p>Now you have connected to the Northwind database of Microsoft Access 2007. Next, I will show how to perform some basic SQL operations such as SELECT, INSERT, UPDATE and DELETE to the Northwind database. See <a href="http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/">Part 4: Perform SQL Operations</a>.</p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection'>Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection</a></li>
<li><a href='http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database</a></li>
<li><a href='http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database</title>
		<link>http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/</link>
		<comments>http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 04:02:06 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Access 2007]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=1023</guid>
		<description><![CDATA[This article is one of the series of Accessing MS Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction Prepare Sample Database On this post, I show how to prepare a sample database for Microsoft Access 2007, [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
<li><a href='http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p>This article is one of the series of Accessing MS Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at <a href="http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/">Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></p>
<p><span id="more-1023"></span></p>
<h3>Prepare Sample Database</h3>
<p>On this post, I show how to prepare a sample database for Microsoft Access 2007, the <strong>&#8220;Northwind&#8221;</strong> database. </p>
<p><!--adsense#Square--></p>
<h3>Step-by-step</h3>
<ol>
<li>Open Microsoft Access 2007, click on <strong>Local Templates</strong>. If you have <strong>Northwind 2007</strong> on <strong>Local Templates</strong>, click on it and skip to step 4. Otherwise, follow the next step to download one.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_1.png" width="200" height="140" alt="Northwind 2007 on Local Templates" title="Northwind 2007 on Local Templates"  /></a></li>
<li>Download Northwind template for Access 2007 from Microsoft &#8211; <a href="http://office.microsoft.com/en-us/templates/TC012289971033.aspx?CategoryID=CT101428651033" target="_blank" rel="nofollow">Northwind 2007</a>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_2.png" width="200" height="261" alt="Download Northwind template for Access 2007" title="Download Northwind template for Access 2007"  /></a></li>
<li>Extract the downloaded file, 01228997.cab. Then, double-click on <strong>01228997.accdt</strong>. Microsoft Office Access 2007 will start.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_3.png" width="200" height="145" alt="Northwind 2007" title="Northwind 2007"  /></a></li>
<li>On <strong>Microsoft Access</strong>, choose the destination to create a sample Northwind 2007. On this example, I choose <strong>&#8220;c:\database\Northwind 2007.accdb&#8221;</strong>. Then, click <strong>Create</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_4.png" width="200" height="144" alt="Create sample Northwind database for Microsoft Access 2007" title="Create sample Northwind database for Microsoft Access 2007"  /></a></li>
<li>Now the sample Northwind database is created. Click on <strong>Navigation Pane</strong> on the left side menu.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_5.png" width="200" height="144" alt="Northwind Sample Database" title="Northwind Sample Database"  /></a></li>
<li>On <strong>Navigation Pane</strong>, click on <strong>Supporting Objects</strong> and double-click on <strong>Customers</strong>. You see data in the Customers table.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/6.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_6.png" width="200" height="144" alt="View Sample Data" title="View Sample Data"  /></a></li>
<li>You also see the sample database file on <strong>c:\database\Northwind 2007.accdb</strong> which was created on step 4. I will use this path in the later part.<br />
<a href="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/7.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/NetBeans-MSAccess2007/Part-2/_7.png" width="200" height="150" alt="Northwind 2007.accdb" title="Northwind 2007.accdb"  /></a></li>
</ol>
<h3>What&#8217;s Next?</h3>
<p>Now you have a sample database, Northwind. Next, I will show how to write code to create a connection from NetBeans 6.5 to the Northwind database. See <a href="http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/">Part 3: Create a Connection</a>.</p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
<li><a href='http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction</title>
		<link>http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/</link>
		<comments>http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 04:04:44 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Access 2007]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=1020</guid>
		<description><![CDATA[Introduction Here comes again, a tutorial about accessing database on NetBeans. In 2007, I wrote two tutorials which are accessing SQL Server on NetBeans using JDBC and accessing MySQL on NetBeans using JDBC. Both tutorials are useful to many people so I decide to write a new one, Accessing MS Microsoft Access 2007 on NetBeans. [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Introduction</h3>
<p>Here comes again, a tutorial about accessing database on NetBeans. In 2007, I wrote two tutorials which are <a href="http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/">accessing SQL Server on NetBeans using JDBC</a> and <a href="http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/">accessing MySQL on NetBeans using JDBC</a>. Both tutorials are useful to many people so I decide to write a new one, Accessing MS Microsoft Access 2007 on NetBeans. </p>
<p><span id="more-1020"></span></p>
<p><!--adsense#Square--></p>
<p>This series shows how to use NetBeans 6.5 to connect to a Microsoft Access 2007&#8242;s database using JDBC. After the connection is established, you see how to perform basic SQL operations such as SELECT, INSERT, UPDATE and DELETE on a table and get result (if any) to display on console window. </p>
<p>I divide this series into 4 parts:</p>
<ol>
<li>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction<br />
The post that you are reading.</li>
<li><a href="http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/">Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Northwind Database</a><br />
This post shows you how to prepare Northwind database of Microsoft Access 2007.</li>
<li><a href="http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/">Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a><br />
This post shows how to establish a connection to the Northwind database on NetBeans 6.5.</li>
<li><a href="http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/">Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a><br />
This post shows how to perform basic SQL operations such as SELECT, INSERT, UPDATE and DELETE on Customer table on Northwind database.</li>
</ol>
<h3>Requirements</h3>
<p>Here are the software requirements which I use on this series.</p>
<ul>
<li>NetBeans 6.5.1</li>
<li>Northwind sample database for Microsoft Access 2007</li>
</ul>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/08/27/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-2-prepare-sample-database/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/08/24/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-1-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing SQL Server on NetBeans using JDBC, Part III: Troubleshooting</title>
		<link>http://www.linglom.com/2008/02/23/accessing-sql-server-on-netbeans-using-jdbc-part-iii-troubleshooting/</link>
		<comments>http://www.linglom.com/2008/02/23/accessing-sql-server-on-netbeans-using-jdbc-part-iii-troubleshooting/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 04:09:13 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2008/02/23/accessing-sql-server-on-netbeans-using-jdbc-part-iii-troubleshooting/</guid>
		<description><![CDATA[This post is the last part which gathers common problems along with solutions about accessing SQL Server using JDBC on NetBeans. By defer posting this a few months, I found many problems that may occur from many people and also solutions. So this post will benefit people who want to develop application to connect a [...]


Related posts:<ol><li><a href='http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection'>Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection</a></li>
<li><a href='http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part I: Create a connection'>Accessing MySQL on NetBeans using JDBC, Part I: Create a connection</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p>This post is the last part which gathers common problems along with solutions about accessing SQL Server using JDBC on NetBeans. By defer posting this a few months, I found many problems that may occur from many people and also solutions. So this post will benefit people who want to develop application to connect a SQL Server using JDBC and have problem with it. </p>
<p><span id="more-76"></span></p>
<p><!--adsense#Square--></p>
<p>There are 3 parts:</p>
<ol>
<li><a href="http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/">Part I  : Create a connection</a><br />
This part which you&#8217;re reading shows about how to establish a connection between NetBeans and SQL Server. In this example, I use SQL Server 2000 SP4 and NetBeans IDE 5.5</li>
<li><a href="http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/">Part II : Perform SQL Operations</a><br />
This part show how to perform some basic operations from NetBeans with SQL Server. For instance, send querys as SELECT, INSERT, UPDATE to the database.</li>
<li>Part III: Troubleshooting<br />
The last part is about problems and how to fix them.</li>
</ol>
<p><a name="top"></a></p>
<h3>Troubleshooting index</h3>
<ol>
<li><a href="#1">Missing the JDBC library</a></li>
<li><a href="#2">Mistype the connection string</a></li>
<li><a href="#3">The TCP/IP connection to the host  has failed</a></li>
<li><a href="#4">Login failed for user &#8216;sa&#8217;</a></li>
<li><a href="#5">This driver is not configured for integrated authentication</a></li>
</ol>
<p><!--adsense#Square--></p>
<ol>
<p><a name="1"></a></p>
<li>
<h4>Missing the JDBC library</h4>
</li>
<p><strong>Problem</strong><br />
You received this error message while compile code in part I.</p>
<p><em>Class Not Found Exception: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver</em><br />
<a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/1.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_1.png" width="250" height="43" alt="Class Not Found Exception: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" title="Class Not Found Exception: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver"  /></a></p>
<p><strong>Cause</strong><br />
This problem may have many causes as the following:
</ol>
<ol>
<li>The JDBC library file is not loaded properly. You may not have add the library file <em>&#8220;sqljdbc.jar&#8221;</em> to the project.</li>
<li>You haven&#8217;t import the needed library to the project</li>
</ol>
<p><strong>Solution</strong></p>
<ol>
<li>Add the appropriate library to the project. Refer to part I: create a connection.<br />
<a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/2.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_2.png" width="250" height="193" alt="Add sqljdbc.jar to project's library" title="Add sqljdbc.jar to project's library"  /></a></li>
<li>You need to add following code at the top of thesource code:<br />
<em>import java.sql.Connection;<br />
import java.sql.DriverManager;<br />
import java.sql.SQLException;</em><br />
<a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/3.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_3.png" width="250" height="70" alt="Import necessity library" title="Import necessity library"  /></a><br />
Or you can<br />
<em>import java.sql.*;</em>
</li>
</ol>
<p><a href="#top">Back to top</a></p>
<p><a name="2"></a></p>
<li>
<h4>Mistype the connection string</h4>
</li>
<p><strong>Problem</strong><br />
You received this error message while compile code in part I.</p>
<p><em>SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:sqlserver:\\bkksql2005:1433;databaseName=AdventureWorks;user=sa;password=password;</em></p>
<p><a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/4.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_4.png" width="250" height="36" alt="SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:sqlserver:\\bkksql2005:1433;databaseName=AdventureWorks;user=sa;password=password;" title="SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:sqlserver:\\bkksql2005:1433;databaseName=AdventureWorks;user=sa;password=password;"  /></a></p>
<p><strong>Cause</strong><br />
You may have typed your connection string incorrectly so the JDBC driver couldn&#8217;t understand. </p>
<p><strong>Solution</strong><br />
Recheck your connection string format. The format for SQL authentication mode should be<br />
&#8220;jdbc:sqlserver://<em>serverName</em>:<em>portNumber</em>;databaseName=<em>DatabaseName</em>;user=<em>UserName</em>;password=<em>Password</em>;&#8221; where </p>
<ul>
<li><em>serverName</em> is the name of the SQL Server</li>
<li>(Optional) <em>portNumber</em> is the TCP port of SQL Server. Default port is 1433. </li>
<li>(Optional) <em>DatabaseName</em> is the database name on SQL Server.</li>
<li><em>UserName</em> and <em>Password</em> are username and password for login to SQL Server. This user must be SQL Server authentication mode.</li>
</ul>
<p><br \/><br />
The format for Windows authentication mode should be<br />
&#8220;jdbc:sqlserver://<em>serverName</em>:<em>portNumber</em>;databaseName=<em>DatabaseName</em>;integratedSecurity=true;&#8221; where </p>
<ul>
<li><em>serverName</em> is the name of the SQL Server</li>
<li>(Optional) <em>portNumber</em> is the TCP port of SQL Server. Default port is 1433. </li>
<li>(Optional) <em>DatabaseName</em> is the database name on SQL Server.</li>
</ul>
<p><strong><em>Note: </em></strong> Database name is an optional. For more complex connection string, refer to MSDN &#8211; <a href="http://msdn2.microsoft.com/en-us/library/ms378428.aspx" rel="nofollow" target="_blank">Building the Connection URL</a> at Microsoft.</p>
<p>In this example, I mistype &#8220;jdbc:sqlserver:<strong>//&#8230;</strong>&#8221; to &#8220;jdbc:sqlserver:<strong>\\&#8230;</strong>&#8220;.</p>
<p><a href="#top">Back to top</a></p>
<p><a name="3"></a></p>
<li>
<h4>The TCP/IP connection to the host  has failed</h4>
</li>
<p><strong>Problem</strong><br />
You received this error message while compile code in part I.</p>
<p><em>SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect</em></p>
<p><a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/5.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_5.png" width="250" height="34" alt="SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect" title="SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect"  /></a></p>
<p><strong>Cause</strong><br />
You try to connect to a SQL Server which has not SQL Server service running or the service refuse to accept remote connections.</p>
<p><strong>Solution</strong></p>
<ol>
<li>Recheck your connection string that you have type server name and port correctly or not. </li>
<li>Check that SQL Server is running.</li>
<li>If SQL Server is on remote host, try to check that you have allow remote connections on the server. To enable remote connections on SQL Server, try visit <a href="http://www.linglom.com/2007/08/31/enable-remote-connection-to-sql-server-2005-express">Enable remote connection to SQL Server 2005 Express</a>.</li>
<li>Check firewall which may block the connection from client to server.</li>
</ol>
<p><a href="#top">Back to top</a></p>
<p><a name="4"></a></p>
<li>
<h4>Login failed for user &#8216;sa&#8217;</h4>
</li>
<p><strong>Problem</strong><br />
You received this error message while compile code in part I.</p>
<p><em>SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user &#8216;sa&#8217;.</em></p>
<p><a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/6.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_6.png" width="250" height="57" alt="SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ’sa’." title="SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ’sa’."  /></a></p>
<p><strong>Cause</strong><br />
This error message states clearly that you have provide wrong username or password that connect to SQL Server.</p>
<p><strong>Solution</strong><br />
Recheck username and password again.</p>
<p><a href="#top">Back to top</a></p>
<p><a name="5"></a></p>
<li>
<h4>This driver is not configured for integrated authentication</h4>
</li>
<p><strong>Problem</strong><br />
You need to use Windows authentication mode by using &#8216;integratedSecurity=true&#8217; instead of specify username and password in the connection string but receive this error message:</p>
<p><em>SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.<br />
Feb 8, 2008 11:28:55 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit><br />
WARNING: Failed to load the sqljdbc_auth.dll</clinit></em></p>
<p><a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/7.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_7.png" width="250" height="48" alt="This driver is not configured for integrated authentication." title="This driver is not configured for integrated authentication."  /></a></p>
<p><strong>Cause</strong><br />
You need to add &#8216;sqljdbc_auth.dll&#8217; to the system path. This file can be found in Microsoft SQL Server JDBC Driver. It resides in sqljdbc_1.1\enu\auth\x86 depends on your JDBC driver and system platform (x86 or x64).</p>
<p><strong>Solution</strong><br />
Copy &#8216;sqljdbc_auth.dll&#8217; to C:\Windows\System32<br />
or<br />
You can set the java.library.path to the directory where &#8216;sqljdbc_auth.dll&#8217; is.<br />
<a href="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/8.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/part3/_8.png" width="250" height="177" alt="set java.library.path" title="set java.library.path"  /></a><br />
<strong><em>Note:</em></strong> If the path contains any spaces or special characters, you have to use double quotes &#8220;&#8221; around the value (ex. <em>&#8220;your directory&#8221;</em>).</p>
<p><a href="#top">Back to top</a></p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection'>Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection</a></li>
<li><a href='http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part I: Create a connection'>Accessing MySQL on NetBeans using JDBC, Part I: Create a connection</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2008/02/23/accessing-sql-server-on-netbeans-using-jdbc-part-iii-troubleshooting/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</title>
		<link>http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/</link>
		<comments>http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 14:14:35 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/</guid>
		<description><![CDATA[From Part I, I have only established a connection with local MySQL. Next I’ll show how to retrieve and modify data on remote MySQL. MySQL Connection using in this part Suppose that I have MySQL running remotely on IP:192.168.1.101 with default port (3306) and I want to connect to Northwind database with username is ‘root’ [...]


Related posts:<ol><li><a href='http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/03/18/accessing-mysql-on-vbnet-using-mysql-connectornet-part-vii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations'>Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p>From Part I, I have only established a connection with local MySQL. Next I’ll show how to retrieve and modify data on remote MySQL.</p>
<p><span id="more-67"></span></p>
<h3>MySQL Connection using in this part</h3>
<p>Suppose that I have MySQL running remotely on IP:192.168.1.101 with default port (3306) and I want to connect to Northwind database with username is ‘root’ and password is ‘123456’. The connection string will be</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> connectionUrl <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:mysql://192.168.1.101:3306/Northwind?&quot;</span> <span style="color: #339933;">+</span> 
            <span style="color: #0000ff;">&quot;user=root&amp;password=123456&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><!--adsense#Square--></p>
<h3>Retrieve data from a database</h3>
<p>To get some data, I need to execute query on the MySQL and get the result back to me. First, I create stmt (Statement object) and execute query in SQL language. Then I store the result on ResultSet object and iterative show the result on the output window.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #003399;">Statement</span> stmt <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">//SQL query command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM Products&quot;</span><span style="color: #339933;">;</span>
            stmt <span style="color: #339933;">=</span> con.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <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>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ProductName&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; : &quot;</span> <span style="color: #339933;">+</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UnitPrice&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Code Explanation:</strong><br />
- Statement objects allow you to execute basic SQL queries and retrieve the results through the ResultSet class.<br />
- In while-loop, iterative in the ResultSet object to show result in console (ProductName and UnitPrice columns in Products table) on output window.</p>
<p>The example result will be similar to below.<br />
<em><strong>Note:</strong> I have imported only 4 records from Products table in Northwind database.</em></p>
<p><img src="http://www.linglom.com/images/howto/netbeans/mysql/part2/1.png" width="297" height="172" alt="Select query result" title="Select query result"  /></p>
<h3>Update data on database</h3>
<p>To insert, update and delete records on SQL Server, you can use the code from retrieve data from database and simply change SQL command and also modify some code a little bit. On update, I must use executeUpdate(�?SQL�?) method on statement object instead executeQuery(“SQL�?) and the return value will be rows affected instead of a record set.</p>
<p><strong>Example</strong><br />
<strong>INSERT command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL insert command</span>
            <span style="color: #003399;">String</span> strSQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO Products (ProductName,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,&quot;</span> <span style="color: #339933;">+</span>
                            <span style="color: #0000ff;">&quot;ReOrderLevel,Discontinued) VALUES ('MyProduct','10 Kg.',1234.0000,100,50,30,0)&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>strSQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>UPDATE command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL update command</span>
            <span style="color: #003399;">String</span> strSQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE Products SET UnitPrice = 900, UnitsInStock = 55, UnitsOnOrder = 5 WHERE ProductName = 'MyProduct'&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>strSQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>DELETE command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL delete command</span>
            <span style="color: #003399;">String</span> strSQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM Products WHERE ProductName = 'MyProduct'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>strSQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Summary </h3>
<p>You can download source code example <a href="http://www.linglom.com/downloads/testMySQL.java">testMySQL.java</a> (Right-click on the link and select Save target As…).<br />
But you have to change connection string to match your environment. The example code will connect to Northwind database and try to retrieve records, insert a new record, update the record and delete the record from Products table. The result is below.</p>
<p><img src="http://www.linglom.com/images/howto/netbeans/mysql/part2/2.png" width="550" height="249" alt="Example query result from MySQL" title="Example query result from MySQL"  /></p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/03/18/accessing-mysql-on-vbnet-using-mysql-connectornet-part-vii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations'>Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Accessing MySQL on NetBeans using JDBC, Part I: Create a connection</title>
		<link>http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/</link>
		<comments>http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 10:14:18 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/</guid>
		<description><![CDATA[Introduction This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J, MySQL AB&#8217;s JDBC Driver for MySQL. I’ll divide into 2 parts: Part I : Create a connection This part which you’re reading shows about how to establish a connection between NetBeans and MySQL. Part II : Perform SQL Operations [...]


Related posts:<ol><li><a href='http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection'>Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection</a></li>
<li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
<li><a href='http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Introduction</h3>
<p>This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J, MySQL AB&#8217;s JDBC Driver for MySQL.</p>
<p><span id="more-58"></span></p>
<p>I’ll divide into 2 parts:</p>
<ol>
<li>Part I : Create a connection<br />
This part which you’re reading shows about how to establish a connection between NetBeans and MySQL.</li>
<li>Part II : Perform SQL Operations<br />
This part show how to perform some basic operations from NetBeans to MySQL. For instance, send querys as SELECT, INSERT, UPDATE to a database. </li>
</ol>
<p><!--adsense#Square--></p>
<h3>Requirements</h3>
<ul>
<li>MySQL Connector/J, licensed under the GPL or a commercial license<br />
from MySQL AB. </li>
<li>NetBeans with JRE (Java Runtime Environment).</li>
</ul>
<h3>Step-by-Step guide</h3>
<ol>
<li>Installation
<ul>
<li>Install NetBeans.</li>
<li>Download MySQL Connector/J, name &#8216;mysql-connector-java-5.0.6.zip&#8217;. (The file name may differs depends on the version if you’ve downloaded from the Official Site at <a href="http://dev.mysql.com/downloads/connector/j/5.1.html" rel="nofollow" target="_blank">here</a>.)</li>
<li>Extract the zip file to a folder, you&#8217;ll see file &#8216;mysql-connector-java-5.0.6-bin.jar&#8217; which is the library file that we want. Just copy the file to the library folder, for example to &#8220;C:\Program Files\Java\jdk1.6.0_02\lib&#8221; directory.<br />
<img src="http://www.linglom.com/images/howto/netbeans/mysql/1.png" width="323" height="296" alt="the library file" title="the library file" /></li>
</ul>
</li>
<li>Add JDBC Driver to the project on NetBeans (Add a library).<br />
Next, I create a new Java project on NetBeans named &#8216;TestMySQL&#8217; and add &#8216;mysql-connector-java-5.0.6-bin.jar&#8217; that I&#8217;ve just extracted from previous step to the project’s library. </p>
<ol>
<li>Create New Project called TestSQL.<br />
<a href="http://www.linglom.com/images/howto/netbeans/mysql/2.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/mysql/_2.png" width="550" height="475" alt="Create new NetBeans's project" title="Create new NetBeans's project"  /></a></li>
<li>In Projects window, right click the project name and select Properties.<br />
<img src="http://www.linglom.com/images/howto/netbeans/mysql/3.png" width="322" height="482" alt="Open project's properties" title="Open project's properties" /></li>
<li>Project Properties window appears. The Categories on left side, select Libraries. And on right side in Compile tab, click Add JAR/Folder.<br />
<a href="http://www.linglom.com/images/howto/netbeans/mysql/4.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/mysql/_4.png" width="550" height="381" alt="Add JAR file" title="Add JAR file"  /></a></li>
<li>New Window appears, browse to the file &#8216;mysql-connector-java-5.0.6-bin.jar&#8217; and click Open.<br />
<a href="http://www.linglom.com/images/howto/netbeans/mysql/5.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/mysql/_5.png" width="550" height="374" alt="Browse to the library file" title="Browse to the library file"  /></a></li>
<li>You’ll see the .jar file was added to the project. Click OK to finish.<br />
<a href="http://www.linglom.com/images/howto/netbeans/mysql/6.png" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/mysql/_6.png" width="550" height="381" alt="The library file was added" title="The library file was added"  /></a><br />
Note: You should keep mysql-connector-java-5.0.6-bin.jar in the directory that you won’t delete it (ex. not in temp folder). May be in the same directory that keep common library files. If you delete the file without delete a link from the project, the project will show error about missing library.
</li>
</ol>
</li>
<li>Connect to the database.<br />
Now I&#8217;m going to write some code to connect to MySQL database. I have configured MySQL service on localhost.</p>
<ol>
<li>I’m going to use Connection and DriverMapper Classes so I need to import libraries.<br />
import java.sql.*;<br />
<img src="http://www.linglom.com/images/howto/netbeans/mysql/7.png" width="363" height="386" alt="Import classes to the project" title="Import classes to the project" /></li>
<li>I&#8217;ll connect to MySQL Server on local machine, the mysql database(a default database in MySQL). In main method, add the following code.

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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>
            <span style="color: #003399;">String</span> connectionUrl <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:mysql://localhost/mysql?&quot;</span> <span style="color: #339933;">+</span> 
                                   <span style="color: #0000ff;">&quot;user=root&amp;password=123456&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Connection</span> con <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>connectionUrl<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> e<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;SQL Exception: &quot;</span><span style="color: #339933;">+</span> e.<span style="color: #006633;">toString</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> cE<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;Class Not Found Exception: &quot;</span><span style="color: #339933;">+</span> cE.<span style="color: #006633;">toString</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></pre></div></div>

<p><strong>The code explanation:</strong></p>
<ul>
<li>Class.forName(&#8220;com.mysql.jdbc.Driver&#8221;); means load the MySQL driver.</li>
<li>&#8220;jdbc:mysql://localhost/mysql?&#8221; + &#8220;user=root&#038;password=123456&#8243;; is a connection string that tells to connect MySQL on localhost, select database named &#8216;mysql&#8217; and user/password for MySQL server.<br />
If you would like to connecto to other database, simply change text &#8216;mysql&#8217; after &#8216;localhost/&#8217; to your database name.</li>
</ul>
<p><img src="http://www.linglom.com/images/howto/netbeans/mysql/8.png" width="543" height="208" alt="Connection code" title="Connection code" /></p>
</li>
<li>Compile and run the project. If no error occurs, it means that the connection has established successfully.<br />
<img src="http://www.linglom.com/images/howto/netbeans/mysql/9.png" width="309" height="122" alt="Connected MySQL successful" title="Connected MySQL successful" /></li>
</ol>
</li>
</ol>
<p>Next part, I&#8217;ll show to how to perform some basic SQL operations to MySQL.</p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection'>Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection</a></li>
<li><a href='http://www.linglom.com/2009/08/31/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-3-create-a-connection/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection</a></li>
<li><a href='http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/feed/</wfw:commentRss>
		<slash:comments>105</slash:comments>
		</item>
		<item>
		<title>Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations</title>
		<link>http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/</link>
		<comments>http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/#comments</comments>
		<pubDate>Sat, 14 Jul 2007 02:45:51 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/</guid>
		<description><![CDATA[From Part I, I have only established a connection with local SQL Server. Next I’ll show how to retrieve and modify data on remote SQL Server. There are 3 parts: Part I : Create a connection This part which you&#8217;re reading shows about how to establish a connection between NetBeans and SQL Server. In this [...]


Related posts:<ol><li><a href='http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/03/18/accessing-mysql-on-vbnet-using-mysql-connectornet-part-vii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations'>Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p>From Part I, I have only established a connection with local SQL Server. Next I’ll show how to retrieve and modify data on remote SQL Server.</p>
<p><span id="more-42"></span></p>
<p>There are 3 parts:</p>
<ol>
<li><a href="http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/">Part I  : Create a connection</a><br />
This part which you&#8217;re reading shows about how to establish a connection between NetBeans and SQL Server. In this example, I use SQL Server 2000 SP4 and NetBeans IDE 5.5</li>
<li>Part II : Perform SQL Operations<br />
This part show how to perform some basic operations from NetBeans with SQL Server. For instance, send querys as SELECT, INSERT, UPDATE to the database.</li>
<li><a href="http://www.linglom.com/2008/02/23/accessing-sql-server-on-netbeans-using-jdbc-part-iii-troubleshooting/">Part III: Troubleshooting</a><br />
The last part is about problems and how to fix them.</li>
</ol>
<p><!--adsense#Square--></p>
<h3>SQL Server Connection using in this part</h3>
<p>Assume that I have SQL Server running remotely on BKKPDC01 computer and I want to connect to Northwind database with username is ‘sa’ and password is ‘password’. The connection string will be</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> connectionUrl <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:sqlserver://bkkpdc01:1433;databaseName=Northwind;user=sa;password=password&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Retrieve data from database</h3>
<p>To get some data, I need to execute query on the SQL Server and get the result back to me. First, I create stmt (Statement object) and execute query in SQL language. Then I store the result on ResultSet object and iterative show the result on the output window.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #003399;">Statement</span> stmt <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// SQL query command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM Products&quot;</span><span style="color: #339933;">;</span>
            stmt <span style="color: #339933;">=</span> con.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <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>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ProductName&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; : &quot;</span> <span style="color: #339933;">+</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UnitPrice&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Code Explanation:</strong><br />
- Statement object is used for sending SQL Statement to the database.<br />
- ResultSet object is used to keep data from the executed query.<br />
- In while-loop, iterative in the ResultSet object to show result (All ProductName and UnitPrice in Products table) on output window.</p>
<p>The example result will be similar to below.</p>
<p> <a href="http://www.linglom.com/images/howto/netbeans/sqlserver/query1.gif" target="_blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/_query1.gif" width="181" height="250" alt="Example query from SQL Server" title=""  /></a></p>
<h3>Update data on database</h3>
<p>To insert, update and delete records on SQL Server, you can use the code from retrieve data from database and simply change SQL command and also modify some code a little bit. On update, I must use executeUpdate(�?SQL�?) method on statement object instead executeQuery(“SQL�?) and the return value will be rows affected instead of a record set.</p>
<p>Example<br />
<strong>INSERT command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL query command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO Products (ProductName,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReOrderLevel,Discontinued) VALUES ('MyProduct','10 Kg.',1234.0000,100,50,30,0)&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>UPDATE command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL query command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE Products SET UnitPrice = 900, UnitsInStock = 55, UnitsOnOrder = 5 WHERE ProductName = 'MyProduct'&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>DELETE command</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">            <span style="color: #666666; font-style: italic;">// SQL query command</span>
            <span style="color: #003399;">String</span> SQL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM Products WHERE ProductName = 'MyProduct'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> rowsEffected <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>SQL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>rowsEffected <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rows effected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Summary </h3>
<p>	You can download the source code example <a href="http://www.linglom.com/downloads/testsql.java">here</a> (Right-click on the link and select Save target As&#8230;).<br />
But you have to change connection string to match your environment. The example code will connect to Northwind database and try to retrieve records, insert a new record, update the record and delete the record from Products table. The result is below.</p>
<p><a href="http://www.linglom.com/images/howto/netbeans/sqlserver/query2.gif" target="blank"><img src="http://www.linglom.com/images/howto/netbeans/sqlserver/_query2.gif" width="330" height="266" alt="Result from the example source code" title=""  /></a></p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2008/01/16/accessing-mysql-on-netbeans-using-jdbc-part-ii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations'>Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/09/07/accessing-ms-access-2007-on-netbeans-6-5-using-jdbc-part-4-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations'>Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations</a></li>
<li><a href='http://www.linglom.com/2009/03/18/accessing-mysql-on-vbnet-using-mysql-connectornet-part-vii-perform-sql-operations/' rel='bookmark' title='Permanent Link: Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations'>Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2007/07/14/accessing-sql-server-on-netbeans-using-jdbc-part-ii-perform-sql-operations/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
		<item>
		<title>How to run command-line or execute external application from Java</title>
		<link>http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/</link>
		<comments>http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 15:24:06 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/</guid>
		<description><![CDATA[Have you ever confront a situation that you need to execute external programs while developing a Java application? For instance, you are developing a Java application and need to execute external application(another executable program) in the middle of the program or you may need to execute some commands such as listing directory command: dir (in [...]


Related posts:<ol><li><a href='http://www.linglom.com/2008/07/21/get-system-information-on-command-line-mode/' rel='bookmark' title='Permanent Link: Get System Information on command-line mode'>Get System Information on command-line mode</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p>Have you ever confront a situation that you need to execute external programs while developing a Java application? For instance, you are developing a Java application and need to execute external application(another executable program) in the middle of the program or you may need to execute some commands such as listing directory command: dir (in windows) or ls (in Unix) while developing the program. </p>
<p><span id="more-41"></span></p>
<p><!--adsense#Square--></p>
<p>The following will show you how to execute the external program from Java application.</p>
<h2>Example</h2>
<p><em><strong>Note</strong></em>: The example will use NetBeans as IDE.</p>
<p>Let see the example Java source code below:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</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> Main <span style="color: #009900;">&#123;</span>
&nbsp;
       <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> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">Runtime</span> rt <span style="color: #339933;">=</span> <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//Process pr = rt.exec(&quot;cmd /c dir&quot;);</span>
                <span style="color: #003399;">Process</span> pr <span style="color: #339933;">=</span> rt.<span style="color: #006633;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>helloworld.exe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #003399;">BufferedReader</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>pr.<span style="color: #006633;">getInputStream</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;
                <span style="color: #003399;">String</span> line<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line<span style="color: #339933;">=</span>input.<span style="color: #006633;">readLine</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: #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>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">int</span> exitVal <span style="color: #339933;">=</span> pr.<span style="color: #006633;">waitFor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;Exited with error code &quot;</span><span style="color: #339933;">+</span>exitVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <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;">Exception</span> e<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>e.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><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></pre></div></div>

<p>The above Java&#8217;s code will try to execute the external program (helloworld.exe) and show output in console as exit code of the external program.</p>
<p><strong>The sample external program, Helloworld.exe (Visual Basic)</strong><br />
<img src="http://www.linglom.com/images/howto/programming/3.gif" width="390" height="204" alt="Visual Basic - Hello World" title="Hello World" /></p>
<h3><strong>Code Explanation:</strong></h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Runtime</span> rt <span style="color: #339933;">=</span> <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Process</span> pr <span style="color: #339933;">=</span> rt.<span style="color: #006633;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>helloworld.exe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Create Runtime Object and attach to system process. In this example, execute the helloworld.exe.<br />
If you want to execute some commands, just modify the string in the rt.exec(“….�?) to command that you want.<br />
For instance, in the comment line; //Process pr = rt.exec(&#8220;cmd /c dir&#8221;);<br />
<em><strong>Note</strong></em>: that you need to enter “cmd /c …�? before type any command in Windows.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> exitVal <span style="color: #339933;">=</span> pr.<span style="color: #006633;">waitFor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;Exited with error code &quot;</span><span style="color: #339933;">+</span>exitVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Method waitFor() will make the current thread to wait until the external program finish and return the exit value to the waited thread. </p>
<h3><strong>Output example</strong></h3>
<p>Process pr = rt.exec(&#8220;c:\\helloworld.exe&#8221;);<br />
<img src="http://www.linglom.com/images/howto/programming/1.gif" width="366" height="157" alt="Execute external program with exit code" title="Execute external program with exit code" /></p>
<p>Process pr = rt.exec(&#8220;cmd /c dir&#8221;);<br />
<img src="http://www.linglom.com/images/howto/programming/2.gif" width="506" height="439" alt="Execute windows command" title="Execute windows command" /></p>
<h2>Summary</h2>
<p>	This is a simple code to execute external applications. If you want to call functions from other program (ex. C++, Visual Basic DLL), you need to use JNI (Java Native Interface) which you can find a nice tutorial at codeproject.com.<br />
	If you need more information, below are some sites that talk about executing external code.</p>
<p><strong>For Java users</strong></p>
<ul>
<li>Execute an external program &#8211; Real&#8217;s Java How-to.
<p>http://www.rgagnon.com/javadetails/java-0014.html</li>
<li>When Runtime.exec() won&#8217;t &#8211; Java World.
<p>http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html</li>
<li>Trouble with Runtime.getRuntime().exec(cmd) on Linux &#8211; Java.
<p>http://www.thescripts.com/forum/thread16788.html</li>
<li>Runtime.getRuntime().exec (Linux / UNIX forum at JavaRanch).
<p>http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&#038;f=13&#038;t=001755</li>
<li>Java&#8217;s Runtime.exec() and External Applications.
<p>http://www.ensta.fr/~diam/java/online/io/javazine.html</li>
</ul>
<p><strong>For C++ users</strong></p>
<ul>
<li>How can I start a process?
<p>http://www.codeguru.com/forum/showthread.php?t=302501</li>
<li>Executing programs with C(Linux).
<p>http://www.gidforums.com/t-3369.html</li>
</ul>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2008/07/21/get-system-information-on-command-line-mode/' rel='bookmark' title='Permanent Link: Get System Information on command-line mode'>Get System Information on command-line mode</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/feed/</wfw:commentRss>
		<slash:comments>63</slash:comments>
		</item>
		<item>
		<title>How to compile VTK for Java using Microsoft Visual Studio .NET 2003</title>
		<link>http://www.linglom.com/2007/03/11/how-to-compile-vtk-for-java-using-microsoft-visual-studio-net-2003/</link>
		<comments>http://www.linglom.com/2007/03/11/how-to-compile-vtk-for-java-using-microsoft-visual-studio-net-2003/#comments</comments>
		<pubDate>Sun, 11 Mar 2007 03:01:21 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[VTK]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2007/03/11/how-to-compile-vtk-for-java-using-microsoft-visual-studio-net-2003/</guid>
		<description><![CDATA[Introduction Currently, I&#8217;m doing a Java Project that need some 3D image processing so I come across with VTK which is an open source consists of a C++ class library, and several interpreted interface layers including Tcl/Tk, Java, and Python. To use VTK in Java, I need to compile VTK for using with Java. I [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><p><strong>Introduction</strong><br />
Currently, I&#8217;m doing a Java Project that need some 3D image processing so I come across with <a href="http://www.vtk.org/" rel="nofollow" target="_blank">VTK</a> which is an open source consists of a C++ class library, and several interpreted interface layers including Tcl/Tk, Java, and Python. To use VTK in Java, I need to compile VTK for using with Java. I have found only a few website that related with this topic and hardly to get troubleshoot problems so I decided to write the tutorial.</p>
<p><span id="more-35"></span></p>
<p><!--adsense#Square--></p>
<p><strong>What do you need?</strong></p>
<ol>
<li><a href="http://www.vtk.org/get-software.php" rel="nofollow" target="_blank">VTK Source and Data</a>.<br />
In the example, I used VTK Source 4.4.2 and Data 5.0.2. I&#8217;ve found that when compile Source 5.0.2 in VS .NET, I got some errors so I recommend as that I use.</li>
<li><a href="http://www.cmake.org/HTML/Download.html" rel="nofollow" target="_blank">CMake Cross Platform Make</a>.<br />
In the example, I used CMake 2.4.6</li>
<li><a href="http://msdn2.microsoft.com/en-us/vstudio/" rel="nofollow" target="_blank">Microsoft Visual Studio .NET 2003</a></li>
</ol>
<p><strong>Step-By-Step Guide</strong></p>
<ol>
<li>Extract VTK Source and Data. I extract VTK Source to C:\VTK and VTK Data to C:\VTK\VTKData.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/1.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_1.gif" width="330" height="254" alt="" title="Extract VTK Data and Source"  /></a></p>
<li>Run CMake to make project for VS .NET 2003</li>
<p><img src="http://www.linglom.com/images/howto/vtk/2.gif" width="223" height="56" alt="A CMake icon" title="" /></p>
<li>Specify source code path as C:\VTK and build binaries to C:\VTK\Bin as figure below.<br />
You can choose place to keep Binaries elsewhere if you want.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/3.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_3.gif" width="330" height="217" alt="CMake's Window" title=""  /></a></p>
<li>Click Configure. New Window appears to let you choose which compiler you want CMake to build for. Select Visual Studio 7 .NET 2003. Click OK.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/4.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_4.gif" width="330" height="217" alt="Choose Generator" title=""  /></a></p>
<li>New Window appears to confirm new directory creation. Click OK. Wait few minutes.</li>
<p><img src="http://www.linglom.com/images/howto/vtk/5.gif" width="258" height="134" alt="Create new directory confirmation" title="" /></p>
<li>When finished, there will be some attributes show in cache values in red. Change some attributes as figure below. Then Click Configure, again.<br />
BUILD_SHARED_LIBS = ON.<br />
VTK_DATA_ROOT = C:\VTK\VTKDAta (Where you extract VTKData).<br />
VTK_WRAP_JAVA = ON (To make library for Java)</li>
<p><a href="http://www.linglom.com/images/howto/vtk/6.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_6.gif" width="330" height="276" alt="CMake Configuration for Java" title=""  /></a></p>
<li>When configured done, the color on attributes will change to gray. Now Click OK.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/6.5.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_6.5.gif" width="330" height="274" alt="Compiled configuration" title=""  /></a></p>
<li>When finished, CMake will close itself automatically. Now let see C:\VTK\Bin, where you build Binaries to, you will see solution file (VTK.sln). Double-click it to run Visual Studio.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/7.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_7.gif" width="330" height="249" alt="Solution file was created" title=""  /></a></p>
<li>Change Solution Configuration to Release to make sure that the compiled libraries can be use on other machine.</li>
<p><img src="http://www.linglom.com/images/howto/vtk/8.gif" width="213" height="152" alt="Change to Release solution" title="" /></p>
<li>Then, Click Start (Debug -> Start) or press F5 to compile the project. This step may takes up to 1-2 hours depends on your CPU.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/9.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_9.gif" width="330" height="240" alt="Run VTK's project" title=""  /></a></p>
<li>There may be new window show about configurations are out of date, just ignore it by select Yes.</li>
<p><img src="http://www.linglom.com/images/howto/vtk/10.gif" width="346" height="689" alt="Notify out of date configurations" title="" /></p>
<li>The Compile will finish when the output window show as below.</li>
<p><img src="http://www.linglom.com/images/howto/vtk/11.gif" width="344" height="149" alt="Compiled Project" title="" /></p>
<li>If there is a window as below, you can ignore it by Click Cancel.</li>
<p><img src="http://www.linglom.com/images/howto/vtk/12.gif" width="403" height="179" alt="Specify Debugging" title="" /></p>
<li>Finish!. Now you get the compiled library files in C:\VTK\Bin\bin\release for DLL files and C:\VTK\Bin\java\vtk for Java Class files.</li>
<p><a href="http://www.linglom.com/images/howto/vtk/13.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_13.gif" width="330" height="259" alt="Libraries folder" title=""  /></a><br />
<a href="http://www.linglom.com/images/howto/vtk/14.gif" target="_blank"><img src="http://www.linglom.com/images/howto/vtk/_14.gif" width="330" height="258" alt="Libraries folder" title=""  /></a></p>
<li>For DLL libraries in Release folder, you can use by add System path to the folder. My Computer -> Properties -> Advanced -> Environment Variables -> Edit on Path in System Variables. Insert directory where DLL files are.<br />
<img src="http://www.linglom.com/images/howto/vtk/15.gif" width="417" height="485" alt="Add DLL libraries" title="" /></li>
<li>For Java libraries in java\vtk\, you can use by compile them to .class file.</li>
</ol>
</div>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2007/03/11/how-to-compile-vtk-for-java-using-microsoft-visual-studio-net-2003/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
