<?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>Last Stop &#187; sql</title>
	<atom:link href="http://laststop.spaceislimited.org/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://laststop.spaceislimited.org</link>
	<description>it&#039;s going to be a long ride</description>
	<lastBuildDate>Wed, 28 Jan 2009 13:21:30 +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>Connecting to MS SQL Server from Java Recipe</title>
		<link>http://laststop.spaceislimited.org/2008/11/28/connecting-to-ms-sql-server-from-java-recipe/</link>
		<comments>http://laststop.spaceislimited.org/2008/11/28/connecting-to-ms-sql-server-from-java-recipe/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 01:26:43 +0000</pubDate>
		<dc:creator>Timothy M. Rodriguez</dc:creator>
				<category><![CDATA[Technical Nonsense]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[ms sql server]]></category>
		<category><![CDATA[recipe]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://laststop.spaceislimited.org/?p=44</guid>
		<description><![CDATA[I thought I&#8217;d provide a recipe for how to connect to SQL Server from Java.  The following is a quick, relatively clean way to connect to Microsoft&#8217;s SQL Server from Java without going through too much voodoo&#8211;as is often the case when trying to connect to MS SQL Server through a non-microsoft language.  Luckily, Microsoft [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I&#8217;d provide a recipe for how to connect to SQL Server from Java.  The following is a quick, relatively clean way to connect to Microsoft&#8217;s SQL Server from Java without going through too much voodoo&#8211;as is often the case when trying to connect to MS SQL Server through a non-microsoft language.  Luckily, Microsoft provides a pure Java JDBC driver for connecting to it&#8217;s server!  You can find it <a title="MS SQL JDBC 2005" href="http://msdn.microsoft.com/en-us/data/aa937724.aspx" target="_blank">here</a>.  Don&#8217;t forget to properly include in your Java project!  Also, even though it&#8217;s the MS SQL 2k3 driver, it works perfectly fine with MS SQL 2000 (so you might as well just use this driver).</p>
<p><span id="more-44"></span></p>
<pre class="brush: java">

/*
* Main.java
*
* Created on November 27, 2008, 3:56 PM
*
*/

package sqlserversample;

import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

/**
*
* @author Timothy M. Rodriguez
*/

public class Main {

/* jdbc driver name and database url
* ms sql uses port 1433
* replace &quot;mysqlserver&quot; with the ip and host of your database
* if connecting from a *nix box, don&#039;t forget to use the FQDN and not just the NetBIOS host
* if not, the ip is a safe bet
*/
private static final String DATABASE_URL = &quot;jdbc:sqlserver://mysqlserver:1433&quot;;
private static final String DRIVER = &quot;com.microsoft.sqlserver.jdbc.sqlserverdriver&quot;;

//used to connect to the database
private static Connection connection = null;
//used to execute queries on the database
private static Statement statement = null;
//this object is used to store your result sets
private static ResultSet resultSet = null;
//this object is used to stored data about the table
private static ResultSetMetaData metaData = null;

/** Creates a new instance of Main */
public Main() {
//default constructor
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

try {
// load the driver class

//this should work, but doesn&#039;t, not sure why, let me know in the comments if you do
// Class.forName(DRIVER);
// instead i created it this way
SQLServerDriver sqlServerDriver = new SQLServerDriver();
/*
* I manually created a SQLServerDriver instance, this is due to
* complications with the classpath establish connection to db
*/
DriverManager.registerDriver(sqlServerDriver);
connection = DriverManager.getConnection(DATABASE_URL,
&quot;user&quot;, &quot;password&quot;);

// create the statement for querying
statement = connection.createStatement();

//put your query string in the executeQuery method argument
resultSet = statement.executeQuery(&quot;SELECT TOP 10 * FROM Table&quot;);
metaData = resultSet.getMetaData();
//I didn&#039;t use the metadata object, but showed you how to create it
//the metaData object can be used to get column names and indices, column types, etc.

while(resultSet.next()) {
//the get object method is useful if you don&#039;t want to bother with the column type
System.out.println(resultSet.getObject(3).toString());
}

} catch (SQLException sqlException) {
//couldn&#039;t connect to the db or execute a query
sqlException.printStackTrace();
/* in a real webapp, you don&#039;t want to be printing stack traces
* instead print a nice error page that doesn&#039;t include internal information
*  For example, &quot;oops, it blew up&quot;
*/
}
}
}
</pre>
<p>That&#8217;s it, I hope that helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://laststop.spaceislimited.org/2008/11/28/connecting-to-ms-sql-server-from-java-recipe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

