Http://www.blogjava.net/fl1429/archive/2009/05/20/271740.html
By the way, add the jar package. If the socket is incorrect, no .. socket requires SP4 patch.
Pay attention to drivername. Some are different.
// ================================================ =================================< Br/> // <br/> // file: connecturl. java <br/> // summary: this Microsoft SQL Server JDBC driver sample application <br/> // demonstrates how to connect to a SQL Server database by using <br/> // a connection URL. it also demonstrates how to retrieve data <br/> // from a SQL Server database by using an SQL statement. <br/> //-- ----------------------------------------------------------------- <Br/> // This file is part of the Microsoft SQL Server JDBC driver code samples. <br/> // copyright (c) Microsoft Corporation. all rights reserved. <br/> // This source code is intended only as a supplement to Microsoft <br/> // development tools and/or on-line documentation. see these other <br/> // materials for detai Led information regarding Microsoft code samples. <br/> // This code and information are provided "as is" without warranty of <br/> // any kind, either expressed or implied, including but not limited to <br/> // The implied warranties of merchantability and/or fitness for a <br/> // particle purpose. <br/> // ============================ ========================================================== = <br/> Pa Ckage CN. isvi. util; <br/> Import Java. SQL. *; </P> <p> public class connecturl {</P> <p> Public static void main (string [] ARGs) {</P> <p> // create a variable for the connection string. <br/> string connectionurl = "JDBC: sqlserver: // 10.100.100.246: 1433; databasename = gdgtest;"; </P> <p> // declare the JDBC objects. <br/> connection con = NULL; <br/> statement stmt = NULL; <br/> resultset rs = NULL; </P> <P> try {<br/> // establish the connection. <br/> class. forname ("com. microsoft. sqlserver. JDBC. sqlserverdriver "); </P> <p> con = drivermanager. getconnection (connectionurl, "sa", "1q2w3e"); <br/> system. out. println ("coonn" + con); <br/> // create and execute an SQL statement that returns some data. <br/> // string SQL = "select top 10 * From person. contact "; <br/> // stmt = con. createstatement (); <br/> // R S = stmt.exe cutequery (SQL); <br/> // iterate through the data in the result set and display it. <br/> // while (RS. next () {<br/> // system. out. println (RS. getstring (4) + "" + Rs. getstring (6); <br/>/}< br/>}</P> <p> // handle any errors that may have occurred. <br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> finally {<br/> If (RS! = NULL) Try {Rs. Close ();} catch (exception e) {}< br/> If (stmt! = NULL) Try {stmt. Close () ;}catch (exception e) {}< br/> If (con! = NULL) Try {con. Close () ;}catch (exception e) {}< br/>}< br/>
Using datasource, it seems that DLL is required
// ================================================ =================================< Br/> // <br/> // file: connectds. java <br/> // summary: this Microsoft SQL Server JDBC driver sample application <br/> // demonstrates how to connect to a SQL Server database by <br/> // using a data source object. it also demonstrates how to <br/> // retrieve data from a SQL Server database by using a stored <br/> // procedure. <br/> // <Br/> // ----------------------------------------------------------------------- <br/> // This file is part of the Microsoft SQL Server JDBC driver code samples. <br/> // copyright (c) Microsoft Corporation. all rights reserved. <br/> // This source code is intended only as a supplement to Microsoft <br/> // development tools and/or on-line documentation. see these other <br/> // materi ALS for detailed information regarding Microsoft code samples. <br/> // This code and information are provided "as is" without warranty of <br/> // any kind, either expressed or implied, including but not limited to <br/> // The implied warranties of merchantability and/or fitness for a <br/> // particle purpose. <br/> // ============================ ========================================== =====< Br/> package CN. isvi. util; <br/> Import Java. SQL. *; <br/> Import COM. microsoft. sqlserver. JDBC. *; </P> <p> public class connectds {</P> <p> Public static void main (string [] ARGs) {</P> <p> // declare the JDBC objects. <br/> connection con = NULL; <br/> callablestatement cstmt = NULL; <br/> resultset rs = NULL; </P> <p> try {<br/> // establish the connection. <br/> sqlserverdatasource DS = new SQL Serverdatasource (); <br/> DS. setintegratedsecurity (true); <br/> DS. setservername ("10.100.100.246"); <br/> DS. setportnumber (1433); <br/> DS. setdatabasename ("gdgtest"); <br/> con = Ds. getconnection (); </P> <p> // execute a stored procedure that returns some data. <br/> cstmt = con. preparecall ("{call DBO. uspgetemployeemanagers (?)} "); <Br/> cstmt. setint (1, 50); <br/> rs = cstmt.exe cutequery (); </P> <p> // iterate through the data in the result set and display it. <br/> while (RS. next () {<br/> system. out. println ("Employee:" + Rs. getstring ("lastname") + <br/> "," + Rs. getstring ("firstname"); <br/> system. out. println ("Manager:" + Rs. getstring ("managerlastname") + <br/> "," + Rs. getstring ("managerfirstname"); <br/> System. Out. println (); <br/>}</P> <p> // handle any errors that may have occurred. <br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> finally {<br/> If (RS! = NULL) Try {Rs. Close ();} catch (exception e) {}< br/> If (cstmt! = NULL) Try {cstmt. Close () ;}catch (exception e) {}< br/> If (con! = NULL) Try {con. Close () ;}catch (exception e) {}< br/>}< br/>