<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jeff's thoughts on Software Architecture, Large Scale Services and the Technical world at large : JDBC</title><link>http://blogs.msdn.com/jcurrier/archive/tags/JDBC/default.aspx</link><description>Tags: JDBC</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>SDS Java JDBC examples</title><link>http://blogs.msdn.com/jcurrier/archive/2009/03/29/sds-java-jdbc-examples.aspx</link><pubDate>Sun, 29 Mar 2009 23:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9517505</guid><dc:creator>jcurrier</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jcurrier/comments/9517505.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jcurrier/commentrss.aspx?PostID=9517505</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jcurrier/rsscomments.aspx?PostID=9517505</wfw:comment><description>&lt;P&gt;&lt;FONT size=2&gt;In my &lt;A href="http://blogs.msdn.com/jcurrier/archive/2009/03/29/sds-coding-examples-part-1-c-ado-net.aspx" mce_href="http://blogs.msdn.com/jcurrier/archive/2009/03/29/sds-coding-examples-part-1-c-ado-net.aspx"&gt;previous&lt;/A&gt; blog entry I mentioned that I would post some Java &amp;amp; JDBC examples for connecting to and interacting with the SDS service.&amp;nbsp; Since that post (roughly an hour ago) I’ve been called a, “slacker” by &lt;A href="http://blogs.msdn.com/drobinson/default.aspx" mce_href="http://blogs.msdn.com/drobinson/default.aspx"&gt;David Robinson&lt;/A&gt; one of the PM’s on the team.&amp;nbsp; Therefore, to show that I am not a, “slacker” I’ve decided to just post the code today.&amp;nbsp; Happy Dave :-)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;So, in this example I’m using Java 1.6, NetBeans as the editor and the latest JDBC driver for SQL Server which you can download from &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=F914793A-6FB4-475F-9537-B8FCB776BEFD&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyId=F914793A-6FB4-475F-9537-B8FCB776BEFD&amp;amp;displaylang=en"&gt;here&lt;/A&gt;.&amp;nbsp; The only jar file I’m currently using to compile (and run with) other than the expected JRE jar files is the, “sqljdbc4.jar” jar file which comes along with the download I’ve linked to above.&amp;nbsp; NOTE: That if you choose to use an earlier version of JDBC it *must* support SSL.&amp;nbsp; This is a requirement for the service.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;Now, I’ve purposely made this example to be the exact same as the C# example I did previously so you could focus more on the differences with the client technologies and less on the actual data I’ve created.&amp;nbsp; As you can see the only significant difference relative to SDS is the format of a couple of connection string values (namely the database, user id format and the initial url for the server).&amp;nbsp; Other than that it’s the same code that we would use to connect to locally.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;Now, with no further delay.&amp;nbsp; The Java code for working with SDS.&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; main(String[] args)
    {
        String createTableSql = 
                "&lt;SPAN style="COLOR: #8b0000"&gt;CREATE TABLE [dbo].[tbl_Person]    &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;(                                                    &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;     [FirstName] NVARCHAR(64) NOT NULL,              &lt;/SPAN&gt;" +
	            "&lt;SPAN style="COLOR: #8b0000"&gt;     [LastName] NVARCHAR(64) NOT NULL                &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;    CONSTRAINT [personName_PK] PRIMARY KEY CLUSTERED &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;    (                                                &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;        [FirstName] ASC,                             &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;        [LastName] ASC                               &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;    )                                                &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;)&lt;/SPAN&gt;";     
        
        String insertSql = 
                "&lt;SPAN style="COLOR: #8b0000"&gt;INSERT INTO dbo.tbl_Person(FirstName, LastName) VALUES ('Jeff', 'Currier'); &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;INSERT INTO dbo.tbl_Person(FirstName, LastName) VALUES ('Nigel', 'Ellis'); &lt;/SPAN&gt;" +
                "&lt;SPAN style="COLOR: #8b0000"&gt;INSERT INTO dbo.tbl_Person(FirstName, LastName) VALUES ('David', 'Robinson');&lt;/SPAN&gt;"+ 
                "&lt;SPAN style="COLOR: #8b0000"&gt;INSERT INTO dbo.tbl_Person(FirstName, LastName) VALUES ('Jeff', 'Smith');&lt;/SPAN&gt;";
                
        &lt;SPAN style="COLOR: #0000ff"&gt;try&lt;/SPAN&gt;
        {
            &lt;SPAN style="COLOR: #008000"&gt;// First, format the connection string note that the server name here again is, “myserver”.  The database name (my user database name) is mydatabase.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;  &lt;/SPAN&gt;
            String connectionUrl = "&lt;SPAN style="COLOR: #8b0000"&gt;jdbc:sqlserver://myserver.data.dev.mscds.com;&lt;/SPAN&gt;" +
                    "&lt;SPAN style="COLOR: #8b0000"&gt;database=mydatabase;encrypt=true;user=jeff@myserver;password=*****&lt;/SPAN&gt;";

            &lt;SPAN style="COLOR: #008000"&gt;// Next, make the sure the SQL Server Driver is loaded.&lt;/SPAN&gt;
            Class.forName("&lt;SPAN style="COLOR: #8b0000"&gt;com.microsoft.sqlserver.jdbc.SQLServerDriver&lt;/SPAN&gt;");
            
            &lt;SPAN style="COLOR: #008000"&gt;// Then attempt to get a connection.  This will null or throw if we can't&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: #008000"&gt;// get a connection.&lt;/SPAN&gt;
            Connection sqlConn = DriverManager.getConnection(connectionUrl);
            &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (sqlConn == &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)
            {
                System.out.println("&lt;SPAN style="COLOR: #8b0000"&gt;Unable to obtain connection.  exiting&lt;/SPAN&gt;");
                System.exit(1);
            }

            &lt;SPAN style="COLOR: #008000"&gt;// Begin by creating the table we'll use.&lt;/SPAN&gt;
            Statement sqlStmt = sqlConn.createStatement();
            sqlStmt.execute(createTableSql);

            &lt;SPAN style="COLOR: #008000"&gt;// Then, insert some rows into the table that we just created.&lt;/SPAN&gt;
            sqlStmt.execute(insertSql);

            &lt;SPAN style="COLOR: #008000"&gt;// Now, select all of the, "Jeff' data from the table and print them out.&lt;/SPAN&gt;
            ResultSet results = sqlStmt.executeQuery("&lt;SPAN style="COLOR: #8b0000"&gt;select FirstName, LastName from tbl_Person where FirstName='Jeff'&lt;/SPAN&gt;");
            &lt;SPAN style="COLOR: #0000ff"&gt;while&lt;/SPAN&gt; (results.next())
            {
                System.out.println("&lt;SPAN style="COLOR: #8b0000"&gt;FirstName: &lt;/SPAN&gt;" + results.getString("&lt;SPAN style="COLOR: #8b0000"&gt;FirstName&lt;/SPAN&gt;") + "&lt;SPAN style="COLOR: #8b0000"&gt; LastName: &lt;/SPAN&gt;" + results.getString("&lt;SPAN style="COLOR: #8b0000"&gt;LastName&lt;/SPAN&gt;"));
            }
            &lt;SPAN style="COLOR: #008000"&gt;// Close the ResultSet up.&lt;/SPAN&gt;
            results.close();

            &lt;SPAN style="COLOR: #008000"&gt;// Finally drop the table and close the conneciton.&lt;/SPAN&gt;
            sqlStmt.execute("&lt;SPAN style="COLOR: #8b0000"&gt;drop table tbl_Person&lt;/SPAN&gt;");
            sqlConn.close();
        } &lt;SPAN style="COLOR: #0000ff"&gt;catch&lt;/SPAN&gt; (SQLException ex)
        {
            System.out.println("&lt;SPAN style="COLOR: #8b0000"&gt;Error: Unable to execute query &lt;/SPAN&gt;");
            ex.printStackTrace();
        } &lt;SPAN style="COLOR: #0000ff"&gt;catch&lt;/SPAN&gt;(ClassNotFoundException ex)
        {
            System.out.println("&lt;SPAN style="COLOR: #8b0000"&gt;Error: Unable to load JDBC Driver!&lt;/SPAN&gt;");
        }

    }&lt;/PRE&gt;&lt;PRE&gt;&lt;FONT size=2&gt;Enjoy,&lt;/FONT&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;FONT size=2&gt;--Jeff--&lt;/FONT&gt;&lt;/PRE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9517505" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jcurrier/archive/tags/Java/default.aspx">Java</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/SQL+Data+Services/default.aspx">SQL Data Services</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/SDS/default.aspx">SDS</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/SQL/default.aspx">SQL</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/JDBC/default.aspx">JDBC</category></item></channel></rss>