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

<channel>
	<title>NULL.in &#187; jdbc</title>
	<atom:link href="http://www.nalinmakar.com/tag/jdbc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nalinmakar.com</link>
	<description>Nalin's Labyrinth</description>
	<lastBuildDate>Thu, 29 Jul 2010 04:07:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Connect to MS SQL Server using JDBC</title>
		<link>http://www.nalinmakar.com/2008/12/13/connect-to-ms-sql-server-using-jdbc/</link>
		<comments>http://www.nalinmakar.com/2008/12/13/connect-to-ms-sql-server-using-jdbc/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 00:51:56 +0000</pubDate>
		<dc:creator>Nalin</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.nalinmakar.com/?p=537</guid>
		<description><![CDATA[The following example shows a java class that can be used to verify if you connection to MS SQL Server using JDBC is working properly. The following code establishes a connection to MS SQL server and then executes a query to print system tables. You need to get Microsoft&#8217;s JDBC drivers or jTDS JDBC drivers. [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows a java class that can be used to verify if you connection to MS SQL Server using JDBC is working properly. The following code establishes a connection to MS SQL server and then executes a query to print system tables. You need to get <a  href="http://msdn.microsoft.com/en-us/data/aa937724.aspx">Microsoft&#8217;s JDBC drivers</a> or <a  href="http://jtds.sourceforge.net/">jTDS JDBC drivers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.DriverManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Statement</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> ConnectMSSQLServer
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dbConnect<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> db_connect_string,
            <span style="color: #003399;">String</span> db_userid,
            <span style="color: #003399;">String</span> db_password<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">Connection</span> conn <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>db_connect_string,
                  db_userid, db_password<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>
         <span style="color: #003399;">Statement</span> statement <span style="color: #339933;">=</span> conn.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from sysobjects where type='u'&quot;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> statement.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>queryString<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: #cc66cc;">1</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: #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>
         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>
&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><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      ConnectMSSQLServer connServer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ConnectMSSQLServer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      connServer.<span style="color: #006633;">dbConnect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdbc:sqlserver://&amp;lt;hostname&amp;gt;&quot;</span>, <span style="color: #0000ff;">&quot;&amp;lt;user&amp;gt;&quot;</span>,
               <span style="color: #0000ff;">&quot;&amp;lt;password&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Change <code>&lt;hostname&gt;</code>, <code>&lt;user&gt;</code> and <code>&lt;password&gt;</code> to the correct values. Also, if you use the jTDS drivers, the driver class should be set to <code>net.sourceforge.jtds.jdbc.Driver</code>.</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a  href="http://www.nalinmakar.com/2010/07/28/logging-tests-to-separate-files/" title="Logging tests to separate files">Logging tests to separate files</a></li><li><a  href="http://www.nalinmakar.com/2010/03/26/hudson-ci-for-testing/" title="Hudson CI for Testing">Hudson CI for Testing</a></li><li><a  href="http://www.nalinmakar.com/2009/11/28/what-is-wrong-with-me/" title="What is wrong with me?">What is wrong with me?</a></li><li><a  href="http://www.nalinmakar.com/2008/12/06/removing-tags-from-posts-contents-in-wordpress/" title="Removing tags from Wordpress posts content">Removing tags from Wordpress posts content</a></li><li><a  href="http://www.nalinmakar.com/2006/08/01/mass-string-replace-in-mysql/" title="Mass string replace in MySQL">Mass string replace in MySQL</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.nalinmakar.com/2008/12/13/connect-to-ms-sql-server-using-jdbc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
