我现在有条SQL语句select count(1) from changjie.dbo.Sm_deliver_backup a 
where a.itemid=998806 
and a.[time] >= 20070616000000 and a.[time] < 20070616240000
and substring(CONVERT (varchar(50),usernumber),0,8) in 
(select number from shike.dbo.[sm_分省市号码段]  where 省='福建')我想利用JDBC连接changjie和Shike这两个数据库,JDBC该如何编写。
我现在JDBC是这样的
public ConnectJdbc()//利用构造方法设置数据库驱动程序和连接好数据库
{
try
{
    Class.forName(DriverName); 
}catch(ClassNotFoundException e)
{
    e.printStackTrace();
    System.out.println("数据库驱动程序加载异常!"+e);
} try
{     conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.2.139:1433;DatabaseName=changjie;User=sa;Password=63586500");
}catch(SQLException e)
{
  e.printStackTrace();
  System.out.println("数据库连接程序加载异常!"+e);
}
    }
这只能连上changjie数据库,想要同时连上changjie和Shike两个数据库该怎么写这个JDBC?

解决方案 »

  1.   

    conn1=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.2.139:1433;DatabaseName=changjie;User=sa;Password=63586500");conn2=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.2.139:1433;DatabaseName=changjie;User=sa;Password=63586500");conn1和conn2 用的时候分开用;我没用过,你试以下吧
      

  2.   

    conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;User=sa;Password=sa");
      

  3.   

    //=====================================================================
    //
    //  File:    connectDS.java      
    //  Summary: This Microsoft SQL Server 2005 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.
    //  Date:    April 2006      
    //
    //---------------------------------------------------------------------
    //
    //  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.
    //
    //===================================================================== 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.setUser("UserName");
    ds.setPassword("*****");
    ds.setServerName("localhost");
    ds.setPortNumber(1433); 
    ds.setDatabaseName("AdventureWorks");
    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) {}
         }
    }
    }
    //楼主不妨参考一下上述代码.