Connect SQL Server in Java in Windows Integrated mode

Source: Internet
Author: User

Today, someone asked how to connect SQL Server in Windows Integrated mode, which was never really tried before.

So, open NetBeans and test the code as follows:

/*


* To change this template, choose Tools | Templates


* and open the template in the editor.


  */


package testsqlconn;


import java.sql.*;


import com.microsoft.sqlserver.jdbc.*;


/** *//**


  *


* @author: Administrator:downmoon (3w@live.cn)


* @date: 2009-9-23 18:42:32


* @Encoding: UTF-8


* @File: Testsqlbyds/testsqlbyds.java


* @Package: Testsqlconn


  */


public class Testsqlbyds {


public testsqlbyds () {}


public void Getresutls ()


     {


//Declare the JDBC objects.


Connection con = null;


CallableStatement cstmt = null;


ResultSet rs = null;


try {


//Establish the connection.


sqlserverdatasource ds = new SQLServerDataSource ();


ds.setintegratedsecurity (TRUE);


ds.setservername ("ap4\\agronet08");//Database Instance name


Ds.setportnumber (1433);


ds.setdatabasename ("AdventureWorksLT2008");//database Name


con = ds.getconnection ();


//Execute a SQL that returns some data.


//cstmt = Con.preparecall ("{Call Dbo.uspgetemployeemanagers (?)}");


//cstmt.setint (1,50);


cstmt = Con.preparecall ("SELECT top *" [saleslt].[ Product] ");//sql


rs = Cstmt.executequery ();


//Iterate through the data in the result set and display it.


while (Rs.next ()) {


System.out.println ("Product:" + rs.getstring ("Name") + "," + rs.getstring ("ProductNumber"));


System.out.println ("ListPrice:" + rs.getstring ("ListPrice"));


System.out.println ();


             }


}//Handle Any errors this may have occurred.


catch (Exception e) {


E.printstacktrace ();


} finally {


if (rs!= null) {


try {


Rs.close ();


} catch (Exception e) {


                 }


             }


if (cstmt!= null) {


try {


Cstmt.close ();


catch (Exception e) {


                 }


             }


if (con!= null) {


try {


Con.close ();


} catch (Exception e) {


                 }


             }


         }


     }


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.