当前位置: 代码迷 >> SQL >> java联接SQLServer2000
  详细解决方案

java联接SQLServer2000

热度:84   发布时间:2016-05-05 13:06:31.0
java连接SQLServer2000

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

顺便把jar包也加进来,如果 socket错误 ,no? ... socket 需要sp4补丁

注意DriverName,有的就不一样

//=====================================================================////  File:    connectURL.java      //  Summary: This Microsoft SQL Server JDBC Driver sample application//	     demonstrates how to connect to a SQL Server database by using//	     a connection URL. It also demonstrates how to retrieve data //	     from a SQL Server database by using an SQL statement.////---------------------------------////  This file is part of the Microsoft SQL Server JDBC Driver Code Samples.//  Copyright (C) Microsoft Corporation.  All rights reserved.////  This source code is intended only as a supplement to Microsoft//  Development Tools and/or on-line documentation.  See these other//  materials for detailed information regarding Microsoft code samples.////  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF //  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A//  PARTICULAR PURPOSE.////===================================================================== package cn.isvi.util;import java.sql.*;public class connectURL {	public static void main(String[] args) {				// Create a variable for the connection string.		String connectionUrl = "jdbc:sqlserver://10.100.100.246:1433;databaseName=gdgtest;";		// 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,"sa","1q2w3e");            		System.out.println("coonn"+con);            		// Create and execute an SQL statement that returns some data.//            		String SQL = "SELECT TOP 10 * FROM Person.Contact";//            		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(4) + " " + rs.getString(6));//            		}        	}        		// 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) {}		}	}}

?使用DataSource,好像需要dll

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

?

  相关解决方案