【发布时间】:2013-04-13 21:43:18
【问题描述】:
我在 Windows 身份验证中在 localhost 上运行此代码 sn-p 但出现以下错误,但我已经在我的类路径中添加了 sqljdbc4 jar 在从 Eclipse 运行时,我还在构建路径中添加了 jar
import java.io.*;
import java.sql.*;
import java.util.GregorianCalendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class Cms_truncate
{
public static void main(String[] args)
{
Calendar cal = new GregorianCalendar();
//String name="cmscim";
Connection conn = null;
String url = "jdbc:sqlserver://localhost\SQLEXPRESS;databasename=yatin";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String userName = "";
String password = "";
Statement stmt;
try
{
Class.forName(driver);//.newInstance();
conn = DriverManager.getConnection(url,userName,password);
String query = "truncate table cim";
stmt = conn.createStatement();
int flag = stmt.executeUpdate(query);
System.out.println("flag = "+flag);
conn.close();
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Cms_truncate.main(Cms_truncate.java:28)
请帮帮我。
【问题讨论】:
-
连接被拒绝:连接。验证连接属性。确保 SQL Server 实例正在主机上运行并在端口接受 TCP/IP 连接。确保到端口的 TCP 连接没有被防火墙阻止。 - 就是这样,检查实例是否在指定端口运行并且它正在接受连接请求,检查凭据是否正确!跨度>
-
别忘了检查防火墙!
标签: java sql-server-2005 jdbc