已答复 ms sql 2005 connecting to java

  • יום רביעי 22 פברואר 2012 05:32
     
     

    to connect ms sql 2005 in java i used sqljdbc3.0 driver in that i used sqljdbc4.jar file in the classpath of environmental variables.I write the following code

    import java.sql.*;

    public class TestJdbc
    {
    public static void main(String[] args)
    {

      String connectionUrl = "jdbc:sqlserver://localost:1433;"+
           "databaseName=SPI;integratedSecurity=true;";

         Connection con = null;
         Statement stmt = null;
         ResultSet rs= null;

       try

          {

            System.out.println("Haiiiii");
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Hai22222222");
            con = DriverManager.getConnection(connectionUrl);
            System.out.println("hai333333");
        String SQL = "SELECT symbol,bid,ask FROM DATA";         
        stmt = con.createStatement();
        rs = stmt.executeQuery(SQL);

        while(rs.next())
          {
            System.out.println(rs.getString(4)+"  "+ rs.getString(6));
          }

            System.out.println("connection successful");
          }


        catch(Exception e)
        {
             e.printStackTrace();
        }
        System.out.print("connected");
        /*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) {}
            }
        }
    }*/
    }

    I get the following errors

    com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the ho
    st localost, port 1433 has failed. Error: "null. Verify the connection propertie
    s, check that an instance of SQL Server is running on the host and accepting TCP
    /IP connections at the port, and that no firewall is blocking TCP connections to
     the port.".
            at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(S
    QLServerException.java:171)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
    verConnection.java:1033)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
    ction.java:817)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
    nection.java:700)
            at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
    java:842)
            at java.sql.DriverManager.getConnection(DriverManager.java:582)
            at java.sql.DriverManager.getConnection(DriverManager.java:207)
            at TestJdbc.main(TestJdbc.java:22)

                            please elp me wat to do

כל התגובות