java嶄參windows鹿撹圭塀銭俊SQL Server
扮寂:2011-01-19 BlogJava 剱埖
書爺嗤繁諒軟?泌採參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) {
}
|