Java串連SqlServer2000

來源:互聯網
上載者:User

http://www.blogjava.net/fl1429/archive/2009/05/20/271740.html

順便把jar包也加進來,如果 socket錯誤 ,no  ... socket 需要sp4補丁

注意DriverName,有的就不一樣

//=====================================================================<br />//<br />// File: connectURL.java<br />// Summary: This Microsoft SQL Server JDBC Driver sample application<br />// demonstrates how to connect to a SQL Server database by using<br />// a connection URL. It also demonstrates how to retrieve data<br />// from a SQL Server database by using an SQL statement.<br />//<br />//---------------------------------------------------------------------<br />//<br />// This file is part of the Microsoft SQL Server JDBC Driver Code Samples.<br />// Copyright (C) Microsoft Corporation. All rights reserved.<br />//<br />// This source code is intended only as a supplement to Microsoft<br />// Development Tools and/or on-line documentation. See these other<br />// materials for detailed information regarding Microsoft code samples.<br />//<br />// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF<br />// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO<br />// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A<br />// PARTICULAR PURPOSE.<br />//<br />//=====================================================================<br />package cn.isvi.util;<br />import java.sql.*; </p><p>public class connectURL { </p><p> public static void main(String[] args) { </p><p> // Create a variable for the connection string.<br /> String connectionUrl = "jdbc:sqlserver://10.100.100.246:1433;databaseName=gdgtest;"; </p><p> // Declare the JDBC objects.<br /> Connection con = null;<br /> Statement stmt = null;<br /> ResultSet rs = null; </p><p> try {<br /> // Establish the connection.<br /> Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); </p><p> con = DriverManager.getConnection(connectionUrl,"sa","1q2w3e");<br /> System.out.println("coonn"+con);<br /> // Create and execute an SQL statement that returns some data.<br />// String SQL = "SELECT TOP 10 * FROM Person.Contact";<br />// stmt = con.createStatement();<br />// rs = stmt.executeQuery(SQL);<br />//<br />// // Iterate through the data in the result set and display it.<br />// while (rs.next()) {<br />// System.out.println(rs.getString(4) + " " + rs.getString(6));<br />// }<br /> } </p><p> // Handle any errors that may have occurred.<br /> catch (Exception e) {<br /> e.printStackTrace();<br /> } </p><p> finally {<br /> if (rs != null) try { rs.close(); } catch(Exception e) {}<br /> if (stmt != null) try { stmt.close(); } catch(Exception e) {}<br /> if (con != null) try { con.close(); } catch(Exception e) {}<br /> }<br /> }<br />}<br />  

使用DataSource,好像需要dll

//=====================================================================<br />//<br />// File: connectDS.java<br />// Summary: This Microsoft SQL Server JDBC Driver sample application<br />// demonstrates how to connect to a SQL Server database by<br />// using a data source object. It also demonstrates how to<br />// retrieve data from a SQL Server database by using a stored<br />// procedure.<br />//<br />//---------------------------------------------------------------------<br />//<br />// This file is part of the Microsoft SQL Server JDBC Driver Code Samples.<br />// Copyright (C) Microsoft Corporation. All rights reserved.<br />//<br />// This source code is intended only as a supplement to Microsoft<br />// Development Tools and/or on-line documentation. See these other<br />// materials for detailed information regarding Microsoft code samples.<br />//<br />// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF<br />// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO<br />// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A<br />// PARTICULAR PURPOSE.<br />//<br />//=====================================================================<br />package cn.isvi.util;<br />import java.sql.*;<br />import com.microsoft.sqlserver.jdbc.*; </p><p>public class connectDS { </p><p> public static void main(String[] args) { </p><p> // Declare the JDBC objects.<br /> Connection con = null;<br /> CallableStatement cstmt = null;<br /> ResultSet rs = null; </p><p> try {<br /> // Establish the connection.<br /> SQLServerDataSource ds = new SQLServerDataSource();<br /> ds.setIntegratedSecurity(true);<br /> ds.setServerName("10.100.100.246");<br /> ds.setPortNumber(1433);<br /> ds.setDatabaseName("gdgtest");<br /> con = ds.getConnection(); </p><p> // Execute a stored procedure that returns some data.<br /> cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");<br /> cstmt.setInt(1, 50);<br /> rs = cstmt.executeQuery(); </p><p> // Iterate through the data in the result set and display it.<br /> while (rs.next()) {<br /> System.out.println("EMPLOYEE: " + rs.getString("LastName") +<br /> ", " + rs.getString("FirstName"));<br /> System.out.println("MANAGER: " + rs.getString("ManagerLastName") +<br /> ", " + rs.getString("ManagerFirstName"));<br /> System.out.println();<br /> }<br /> } </p><p> // Handle any errors that may have occurred.<br /> catch (Exception e) {<br /> e.printStackTrace();<br /> } </p><p> finally {<br /> if (rs != null) try { rs.close(); } catch(Exception e) {}<br /> if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}<br /> if (con != null) try { con.close(); } catch(Exception e) {}<br /> }<br /> }<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.