JDBC connection string for SQL Server 2014 using port number - java

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.

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.

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).

MS SQL Connection in java

How do we connect to MS SQL 2008 server win jdk 1.6?
When I connect it via netbeans
url = jdbc:sqlserver://192.168.0.4:1434;databaseName=pollbooth
or
url = jdbc:sqlserver://192.168.0.4:1434/pollbooth
when ever I already properly configure MS SQl driver, sqljdbc.jar & sqljdbc4.jar, it sends
can't establish a connection to given url using
com.microsoft.sqlserver.jdbc.SQLServerDriver(JRE).
The following link will help you in connecting to the MSSQL Server database.
http://msdn.microsoft.com/en-us/library/ms378428(v=sql.90).aspx

How do I connect to local SQL database using java jdbc

When I try something like this:
Class.forName("com.mysql.jdbc.Driver").newInstance();
DriverManager.getConnection("jdbc:mysql://192.168.2.116:3306/SocialFamilyTree");
I get an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Tried:
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
DriverManager.getConnection("jdbc:sqlserver://localhost:1433");
}catch(Exception e){
System.out.println("Couldn't get database connection.");
e.printStackTrace();
}
and got:
Couldn't get database connection.
Oct 06, 2012 11:15:37 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
if you are trying to connect to sql server your code is wrong , as in that you are trying to connect to mysql .
Use jtds or sql server driver to connect
Microsoft SQL Server 2005 JDBC Driver
DRIVER CLASS: com.microsoft.sqlserver.jdbc.SQLServerDriver
DRIVER LOCATION: Specify the location on your machine of the Microsoft SQL Server driver. See your Microsoft SQL Server driver documentation for more details. Certain versions of the Microsoft SQL Server driver require more than one jar file for the driver location. In this case, simply separate each file location by a semi-colon.
JDBC URL FORMAT: jdbc:sqlserver://<server_name>:<port>
The default port for Microsoft SQL Server is 1433. Usually, if the default port is being used by the database server, the : value of the JDBC url can be omitted.
Examples:
jdbc:sqlserver://neptune.acme.com:1433
jdbc:sqlserver://127.0.0.1:1433

SQL Server connection string issue with multiple instances

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

Categories