building an sql connection url in java - java

so I am attempting to connect to a database on a network through java and I am having difficulty getting a connection to the server. I believe my problem is with the connection url. I have tried a few different things to test it out, but nothing has been working yet, so hopefully someone can help me. The error I get is:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.16, port 1433 has failed. Error: "connect timed out. 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 dbTest.db.dbConnect(db.java:23)
at dbTest.testConnection.main(testConnection.java:11)
Though depending on which connection url I use sometimes i get a java.net.sockettimeoutexception error. This is the code I am currently using to try to connect:
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Properties props = new Properties();
props.setProperty("user","sa");
props.setProperty("password","");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://192.168.1.16;databaseName=ECCS",props);
//MSSQL01\\ECLAIMS_DATA
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
The commented out MSSQL01\ECLAIMS_DATA is the name of the server and database I am attempting to connect to, I have tried using that as well. I'm just a little lost as to where everything needs to be. I have tried including the username and password in the connection string, I have tried using getConnection(url,username,password). I'm also not sure if the databaseName is where that needs to go. Can anyone help me out?

Related

How do I fix port 1433 failing error in my SQL server with JDBC, I tried everything [duplicate]

This question already has answers here:
The TCP/IP connection to the host localhost, port 1433 has failed error, need assistance
(4 answers)
Closed 5 years ago.
So I'm trying to do UDT using in Java with my eclipse and SQL server 2014:
This is the part of the code where I connect my SQL server to my eclipse, I already installed the JDBC jar to the path.
Connection con = null; PreparedStatement statement = null; //to take care of the sql statements to be run
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost‌​;databaseName=Company;integratedSecurity=true;");
//con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433‌​;databaseName=MASTER‌​;user=sa;password=Se‌​cret");
This is my error output, although I haven't created my tables in SQL server yet, I want try to see if eclipse is connected to SQL server 2014, so far this is error I have:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost‌​, port 1433 has failed. Error: "localhost‌​. 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:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at insertion.insert_values(insertion.java:12)
at PointType.main(PointType.java:65)
Don't worry about my insertion part, I just want to know why my port connection is failing?
I tried everything but so far still connection is not working, what could be causing it?
You've misspelled "localhost" in your URL

Not able to connect sql server 2008 r2 with java

I am trying to connect sql server 2008 r2 with my java code , But it is showing me this error .
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host DINESH-PC, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, 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(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:13)
my code is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String dbURL = "jdbc:sqlserver://localhost:1433;user=sa;password=123456";
Connection conn = DriverManager.getConnection(dbURL);
if (conn != null) {
System.out.println("Connected");
}
i am using jre7 and sqljdbc4-3.0.jar is included in library.
Help me with ur suggesstions.
You have quite a lot of information from exception message. Just follow the hints. Check if all connection string are right. Check if DB server run. Check if there is no firewall blocking that port on server where DB is situated.
also must check wheather tcp/ip is enabled ..
to check
open sql server configration manager
select sql server network configration
then, select protocol for MSSQLSERVER
them check wheather tcp/ip is enabled or not. If not enable it.

Connection String of MS-SQL to put in JAVA

What should be the connection string for MSSQL Database in JAVA? Something like this.
Like someother things I want to know that Is there any password in connection string like password of my computer? what should be the port number, url etc? If you need more information about the connection let me know.
P.S. The black line has the servername.
My Connection values
datasource.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.url=jdbc:sqlserver://MSSQLSERVERFULL;databaseName=xproctor;
datasource.user=JUNAID-VAIO\Shamim
datasource.password=
ERROR:
here is the ERROR I get while trying to connect database!!
Error querying database. Cause:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not
get JDBC Connection; nested exception is
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (The TCP/IP connection to the host
MSSQLSERVERFULL, port 1433 has failed. Error: "null. 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.".)
Use this connection string:
jdbc:sqlserver://;servername=server_name;integratedSecurity=true;authenticationScheme=JavaKerberos
The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect".
Enable Name pipes, TCP/IP, change Authentication mode, change localhost to 127.0.0.1 or .,
add the Instance Name to the url, change the port, enable port and Apps in the firewall... almost everything.
Its horrible! But the answer for your problem is: open SQL Server Configuration Manager -> Protocols for SQL SQLEXPRESS, select Properties of TCP/IP. In the tab IP Addresses, set the TCPPort in section IPAll to 1433.

The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect [duplicate]

This question already has answers here:
JDBC connection failed, error: TCP/IP connection to host failed
(9 answers)
Closed 5 years ago.
I want to connect Java class file with SQL server 2008. I have logged in with SQL server authentication use: sa, pass:123456. But i am receiving error in connectivity.
static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dburl = "jdbc:sqlserver://localhost\\SQL2008:1433;Database=Java";
static String user = "sa";
static String password = "123456";
public static void update() throws Exception{
String sql = "UPDATE Categories SET Id='COM' WHERE Id='LAP'";
Class.forName(driver);
Connection conn = DriverManager.getConnection(dburl, user, password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
}
public static void main(String[] args) throws Exception {
Basic.update();
}
Exception in thread "main" 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, 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(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1048)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:829)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:712)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Tutorials.jdbc.Basic.update(Basic.java:48)
at Tutorials.jdbc.Basic.main(Basic.java:72)
Probably a little late to answer this question, but doing it for my own benefit.
The actual TCP port to connect to through JDBC can be found in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp
Use this value to connect to SQL Server.
For example
"jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"
Also ensure that TCP connections are enabled in the server configuration tools.
I think that your dburl may be malformed.
Try this url instead.
String dburl= "jdbc:sqlserver://localhost\\SQL2008:1433;" +
"databaseName=Java;user=sa;password=123456";
Make sure you have Driver jar file and change your dburl to this as mentioned below
try this
String dburl ="jdbc:sqlserver://SQL2008:1433;DatabaseName=Java;user=sa;Password=123456";
Thanks you for your help, i'm fix my problem, it's not error on String dburl= "jdbc:sqlserver://localhost\SQL2008:1433;Database:Java"
it's error port 1433 no connect in SQL server 2008
Where i find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, i'm find its properties.
In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port and restart your SQL Server Services > SQL Server
And Its Done...
Thanks all for your help!
TCP/IP Connection Refused.
Run NETSTAT -A to see a list of ports that are open (they'll say LISTENING) If 1433 isn't among them, then SQL server isn't running or isn't listening on that port. Follow Michael Todd's advice from there.
If 1433 is on the list, check your local firewall to see if it allows connections to/from localhost and/or allows connections to 1433.
You can try TELNET 1433 and see if it connects (you should get a blank screen with a cursor blinking at the top left.) (You may have to enable the telnet client in Programs and Features.) If so then you have a problem with your code.

java to sql server connectivity exception

I have this error/exception-
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. Verify the connection properties, 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.".
and my code is-
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433";
Connection con = DriverManager.getConnection(connectionUrl,"","");
System.out.println("ok");
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
how can i sovle it,please help me.
This could mean almost anything - basically, it was impossible to connect to port 1433 on localhost. Could be a network problem, could be that the database is not started, could be that it is running but not bound to port 1433.
In a pinch, you can always open a shell and run
telnet localhost 1433
to see if it is possible to make a connection at all.
I had this error.
I fixed it by going into the SQL Server Configuration Manager.
Under SQL Server 2005 Configuration -> Protocols for SQLEXPRESS. Look at the IP Address tab of the Properties of the TCP/IP Protocol.
At the very bottom of the protocol list (under IPALL), the TCP Port field was blank. I put 1433 there, saved, restarted the SQL service and it worked great :)
It's telling you it can't connect.
Check that sql server is available on your local machine on that port.
Check that sql server is accepting connections with the blank name and password you're providing in your getConnection() invocation.
Check that your jdbc url is in the correct form, they're sometimes manufacturer/driver specific.
The error just means that java was not able to connect to your database. Either database is not running or your firewall is blocking connections to it. If you are sure your DB server is up on your localhost, try after disabling your windows firewall.
Lets look at some of the possibilities:
The database server is not listening on localhost:1433 : you probably would get a "connection refused" rather than a "connection timed out"
The database server is catatonic (i.e. in a state where it is not responding to requests) : you could get a "connection timed out".
The database server is massively overloaded : you could get a "connection timed out", but you would probably notice that the machine was very slow.
The local software firewall is configured to block all requests to port 1433 : you could get either "connection refused" or "connection timed out" ... or some other response. (It depends exactly what the firewall does with unwanted traffic.)
The loopback interface (for IP 127.0.0.x) is not configured : you are more likely to get "no route to host" or "no route to network".
The DNS entry for localhost is misconfigured (i.e. localhost doesn't resolve to 127.0.0.x) - lots of things could happen ... depending on how it is misconfigured.
Here are some things you could do to diagnose the problem ... depending on your system:
Look at the configured networks using ipconfig or ifconfig.
Look at "/etc/hosts" to check the entry for localhost
Try to connect to the service using telnet -p 1433
Use "ps" or the task manager to see what processes are running.
Check the local firewall configs to ensure that 1433 is open for (at least) TCP from the configured localhost IP address (probably 127.0.0.1).
I had a problem similar to this, and solved it by adding the line:
System.setProperty("java.net.preferIPv4Stack", "true");
The problem was it used IPv6 instead of IPv4 to access the db host (I used it for a DB on a remote host, not localhost).
Hope it helps.

Categories

Resources