WEB开发网
开发学院软件开发Java java中如何以windows集成方式连接SQL Server 阅读

java中如何以windows集成方式连接SQL Server

 2009-09-26 00:00:00 来源:WEB开发网   
核心提示:今天有人问起,如何以windows集成方式连接SQL Server,这个以前真没试过,java中如何以windows集成方式连接SQL Server,于是,打开netBeans测试了一下

今天有人问起,如何以windows集成方式连接SQL Server,这个以前真没试过。

于是,打开netBeans测试了一下,代码如下:

/*
 * 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");//数据库实例名
            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 10 * from [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 that 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) {
                }
            }
        }
    }
}

1 2 3  下一页

Tags:java 如何 windows

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接