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
please elp me wat to do
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)
כל התגובות
-
יום חמישי 23 פברואר 2012 07:28
Hi,
I think there is a typo in your code.
Pease replace "localost" with "localhost" and re-run the application to see if the error is gone.
Related threads:
http://social.msdn.microsoft.com/Forums/en/sqlexpress/thread/bac0ed58-741f-413f-9308-86d867810d67
http://social.msdn.microsoft.com/Forums/en/sqldataaccess/thread/d0edfa96-ca2a-4acf-8ee8-00bf4cccd5e7Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Get or Request Code Sample from Microsoft
If you have any feedback, please tell us.- הוצע כתשובה על-ידי Deepak_Goyal יום חמישי 23 פברואר 2012 09:16
- סומן כתשובה על-ידי Papy NormandModerator יום שני 27 פברואר 2012 19:15