SQL Server connection string issue with multiple instances - java

I used the same connection string for obtaining direct connection. But it is not getting connected when used from java code. What could be the problem?
jdbc:sqlserver://DEVSQL\mssql2008:1433
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host DEVSQL, named instance \mssql2008 has failed.
There are two instances of SQL Server (2005 and 2008) running on the same port 1433. So what would be the connection string.
\mssql2008 is not an instance.
DEVSQL connects to the 2005 instance, while DEVSQL\mssql2008 connects to the 2008 instance

You have to escape the backslash
jdbc:sqlserver://DEVSQL\\mssql2008:1433

Based in the information provided in your example, I cannot differentiate:
Database server (maybe DEVSQL)
SQL Server instance name (maybe mssql2008)
Database name (for this, I'll just assume dbName)
With this info, you can construct your Connection String based in MSDN help
jdbc:sqlserver://DEVSQL:1433;databaseName=dbName;instance=mssql2008.
Also, check that your MSSQL server has the TCP/IP connection enabled. Check here for more info:
How to: Enable the TCP/IP Protocol for a Database Instance
Protocols for MSSQL 2008 (image)

You are missing part of the connection string (microsoft: before sqlserver:. This should work:
jdbc:microsoft:sqlserver://DEVSQL\\mssql2008:1433

Related

Can't connect to remote MySQL (MariaDB) database in .NET but I can with Java

I am trying to migrate some code from JavaFX to use C# for Windows Forms. In Java, I was using JDBC and could connect just fine programmatically. Nos that I am trying to use C# I am getting this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).
I am using the same server address and credentials. I'm using System.Data.SqlClient. I've tried a bunch of different combinations but can't get the connection string to work.
String connectionString = "server=[Server URL];database=[Database Name];persist security info=true;user id=[Username];pwd=[Password]";
Here is an example of the mysql connection string:
string connectionString = "datasource= <database ip or domain>; port=<port, usually 3306>;username=<database user>;password=<user's password>;SslMode=none<this is important if you don't use ssl>;database=<your database>";
I think the reason you can't connect is because you haven't specified the SslMode.

JDBC connection string for SQL Server 2014 using port number

I have installed SQL Server 2014 in Windows 10 with database port (1433) instead of a named instance with Java8. I am trying to use the below mentioned connection string format to connect.
jdbc:sqlserver://localhost:1433;databaseName=TEST_DATABASE;userName=user;password=user
But the connection fails. The SQL JDBC driver I am using is sqljdbc42.jar. I am able to connect successfully while trying to SQL server created using instance name instead of port. The format mentioned below works fine.
jdbc:sqlserver://localhost;instanceName=COMP_SYS;databaseName=NEW_DATABASE;userName=user;password=user
Only with DB port connection is getting failed. What am I missing.

Connecting to SQL Server results in error "The driver received an unexpected pre-login response"

I am unable to connect to sql server, getting following error.
exception com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
to the host SSALES, port 1433 has failed. Error: "The driver received an unexpe
cted pre-login response. Verify the connection properties and check that an inst
ance of SQL Server is running on the host and accepting TCP/IP connections at th
e port. This driver can be used only with SQL Server 2000 or later.".
my local Environment:-
java 5,
sql server enterprise manager version 8,
RunApp.java
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = java.sql.DriverManager.getConnection("jdbc:sqlserver://abc;d‌​atabaseName={db};use‌​r=name;password=pass‌​worf;");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM testdb.testtabel where active = 'a'");
running through cmd:
set path="C:\Program Files\Java\jdk5\bin";
javac -classpath "D:\Web Development\ABC\lib\mail-1.4.3.jar;D:\Web Development\ABC\lib\json-simple.jar;D:\Web Development\ABC\lib\sqljdbc.jar;" com\test\RunApp.java
java -classpath "D:\Web Development\ABC\lib\mail-1.4.3.jar;D:\Web Development\ABC\lib\json-simple.jar;D:\Web Development\ABC\lib\sqljdbc.jar;" com.test.RunApp
Your URL is wrong (see Building the Connection URL), it should be (note the use of an escaped backslash instead of forward slash):
"jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName={db};user=MyUserName;password=MyPassword;"
Also make sure that {db} is replaced with the actual name of the database.
You are trying to connecting by instance name, and some googling on the error message suggests this error can occur if the instance name is wrong or if the SQL Server Browser service is not running. So you need to have the SQL Server Browser service enabled (it is disabled by default).
Otherwise you will need to remove the instance name from the URL, and instead specify the correct port number in the JDBC url (usually 1433, but it can depend on the configuration: double check).

How to connect my web application to SQL Server / SQL Server Express

I am working with a web application that connects to a SQL Server database with this:
jdbc.url=jdbc:jtds:sqlserver://127.0.0.1/MyDatabase
jdbc.username=sa
jdbc.password=password
I am relatively new to SQL Server, however I have successfully accessed a SQLEXPRESS instance through the SQL Server Management Studio.
I can see from the Security->Login folder there is a "sa" entry
However when I run the web application I am getting this error:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property
'dataSource' threw exception; nested exception is
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (Network error IOException: Connection
refused: connect)
I seems that the user: "sa" is not authenticated to access the database?
If so, how can I use the SQL Server management studio to fix this?
Make sure authentication mode is set to Mixed. It's set to Windows authentication mode as default which disables sa account. See here: http://msdn.microsoft.com/en-us/library/ms143705%28v=sql.90%29.aspx
Edit: as Jacob suggested, make sure you can connect via TCP/IP (I assume it's used by the driver). To do so launch SQL Management Studio and in connection preferences set: Authentication - SQL Server authentication, login - sa, password - your password. Then click Options, connection properties and select tcp/ip as protocol. Check if you can login.
Also check these things:
http://msdn.microsoft.com/en-us/library/bb909712(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/bb909725(v=vs.90).aspx
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either.
e.g, c:>telnet servername 1433
to enable telnet client on windows
http://social.technet.microsoft.com/wiki/contents/articles/910.how-to-enable-telnet-client-in-windows-7.aspx

(Enterprise GlassFish v3 build 11) Communication link problem (MySQL DB)

I get a communication link failure while application tries to establish a connection with DB.
[#|2010-04-08T20:09:57.825+0300|SEVERE|glassfish3.0|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=24;_ThreadName=Thread-1;|Cannot connect to database server = com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.|#]
Precisely at this string:
Statement s = conn.createStatement();
where conn is defined as follows:
private static java.sql.Connection conn;
For this app I have set a connection pool with default parameters and currently it (app) uses both JPA and direct JDBC queries. Recreation of connection pool gave nothing, connection pool ping gave next message:
Ping Connection Pool for pool is Failed. Ping failed Exce
ption - Connection could not be allocated because: Communications lin
k failure%%%EOL%%%%%%EOL%%%The last packet sent successfully to the s
erver was 0 milliseconds ago. The driver has not received any packets
from the server. Please check the server.log for more details.%%%EOL
%%%Ping failed Exception - Connection could not be allocated because:
Communications link failure
and flushing the connection pool gave:
com.sun.enterprise.admin.cli.CommandException: remote failure: Failed to flush connection pool ...
However I can connect to the database from a terminal. Besides I have the same app working on my local machine with identical connection pool settings.
Any one has an idea on whats going on or how to solve the trouble?
Such problem could be if you have mysql server & glassfish server on the same host, and in mysql configuration you have option bind to some public address (for example 192.168.0.1 of eth0 interface) that normally successfully working with simple jdbc/jpa using user#localhost, but they don`t in a case of glassfish JTA, instead to bind to some of local address you getting link failure. As rule you could not bind to any local (localhost/127.0.0.1) addresses of such mysql host if public address presented.
Example:
my.cnf
bind-address = 127.0.0.1
bind-address = 192.168.0.1
127.0.0.1 - assign to lo interface
192.168.0.1 - assign to eth0 interface
It is glassfish-mysql bug.
Currently in order to use JTA, you should not bind mysql to such address. (remove "bind-address=192.168.0.1" from my.cnf). Or use user#192.168.0.1 what is less secure.
Besides I have the same app working on my local machine with identical connection pool settings.
Are you connecting to the same database? If yes, maybe check that you're using the same JDBC driver.
In my case I set :
URL : jdbc:mysql://10.81.35.66:3306/testDB
and
url : jdbc:mysql://10.81.31.76:3306/vectordb
both When setting values while creating connection pool in additional property part
on glass fish admin console .

Categories