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

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

 2009-09-26 00:00:00 来源:WEB开发网   
核心提示: 结果提示:找不到sqljdbc_auth.dll,到下载的压缩包里看了下:auth\x86,auth\x64\,auth\IA64下都有该文件,直接复制auth\x86\sqljdbc_auth.dll到E:\Java\jdkUpdate\jre\lib\ext\下,java中如何以window

结果提示:找不到sqljdbc_auth.dll,到下载的压缩包里看了下:auth\x86,auth\x64\,auth\IA64下都有该文件,直接复制auth\x86\sqljdbc_auth.dll到

E:\Java\jdkUpdate\jre\lib\ext\下,这是本机的jre路径。

然后运行。成功!

后来再试了下,发现直接用URL方式也可以实现:

代码如下:

Java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testsqlconn;
import java.sql.*;
/** *//**
 *
 * @author: Administrator:downmoon(3w@live.cn)
 * @date:2009-9-23 18:42:32
 * @Encoding:UTF-8
 * @File:TestSqlByURL/TestSqlByURL.java
 * @Package:testsqlconn
 */
public class TestSqlByURL {
    public TestSqlByURL() {
    }
    public void GetResults() {
        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://ap4\\agronet08:1433;databaseName=AdventureWorksLT2008;integratedSecurity=true;";
        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(connectionUrl);
            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT TOP 10 * FROM [SalesLT].[Product]";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                System.out.println(rs.getString(2) + " " + rs.getString(3));
            }
        } // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

Tags:java 如何 windows

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