连接SQL2005的字符串怎么写啊import com.microsoft.*;
import java.sql.*;
import java.net.*;public class Demo
{
        //驱动程序常量
        private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public static void main(String args[])
    {
        String url1="jdbc:sqlserver://localhost\\SQLEXPRESS;user=sa;password=zj65252353;Database=pubs";
        //String url="jdbc:microsoft:sqlserver://HotJava:1433;User=sa;Password=zj65252353;Database=pubs";
        String qu="select * from titles";
        try {
                Class.forName(DRIVER_CLASS);
            }
            catch (ClassNotFoundException ex1)
            {
                ex1.printStackTrace();
            }        try {
            Connection con=DriverManager.getConnection(url1);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(qu);
            while(rs.next())
            {
                System.out.println(rs.getString(1));
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException ex)
        {
                System.out.println(ex.getMessage());
                System.out.println(ex.getNextException());
        }
    }}抱异常
到指定实例  的连接失败。错误: java.net.SocketTimeoutException: Receive timed out。null

解决方案 »

  1.   


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException; 
    public class TestSQL2005
    {
        private Connection con=null;
        private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        //1. 2000的方式为:Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        private static final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=master";
        String USER = "sa"; 
        String PASSWORD = "rocy520";      public void testConnection()
        {
            try
            {
                Class.forName(DRIVER);
                con=DriverManager.getConnection(URL,USER,PASSWORD);
                if(con!=null)
                {
                    System.out.println("OK!");
                    con.close();
                }
           }
           catch (ClassNotFoundException e)
           {
               e.printStackTrace();
           }
           catch(SQLException e)
           {
               e.printStackTrace();
           }
        }    public static void main(String[] args)
        {
            new TestSQL2005().testConnection();
        }
    }