I am running this code snippet on localhost in windows authentication but getting following error but I have alreadily added sqljdbc4 jar in my class path and while running from eclipse also I have added jar in build path
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();
}
}
}
The Error:
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)
help me please.
This error does not say "authentication error", it says "connection error" due to a "connection refused". This means you need to specify the correct port number. You'll need to review your SQL Server configuration and update your connection string.
according to MSDN docs, the connection string should look like this:
jdbc:sqlserver://localhost:<insert_proper_port_here>\SQLEXPRESS;databasename=yatin
You are not supplying a username or password, which you might need to do once you get the port number figured out. See the referenced documentation for further details.
First of all whenever you are using SQL instance name in URL you have to specify
//[serverName[\\instanceName][:portNumber]]databaseName
for E.g: jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databasename=yatin
Once this is done next step:-
Then Apply and ok
Now go to SQL server services section under SQL Server Configuration Manager in left pane. click on it
Now rerun the code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With