java中以windows集成办法衔接SQL Server[Java编程]
本文“java中以windows集成办法衔接SQL Server[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
本日有人问起,若何故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) {
}
}
}
}
}
以上是“java中以windows集成办法衔接SQL Server[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |