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) {
}
}
}
}
}