-4

I'm trying to connect to database through java code.

Here is the snippet of my code:

import java.sql.*;

public class FirstExample {
 // JDBC driver name and database URL
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
 static final String DB_URL = "jdbc:mysql://hostname:port/ourDatabasename;integratedSecurity=true;";


 //  Database credentials
 static final String USER = "myUsername";
 static final String PASS = "myPass";

 public static void main(String[] args) {
 Connection conn = null;
 Statement stmt = null;
 try{
    //STEP 2: Register JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

//STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
.........................

and on the last row conn = DriverManager.getConnection(DB_URL, USER, PASS); the programm throws an exception:

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.

I have a ping to this hostname. Telnet to the hostname and the port is successful. I can log to this 'ourDatabasename' with Windows Credentials:myUsername/myPass through Microsoft SQL Server Management Studio, but can't connect through this java code. I have:'mysql-connector-java-5.1.40-bin' and add it to my project.

What am I missing? Do you have any ideas how to solve this issue?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Possible duplicate of [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – Gurwinder Singh Jan 27 '17 at 10:17
  • 5
    Are you sure you're not mixing up `MySQL` and `MSSQL`? – fvu Jan 27 '17 at 10:18
  • 2
    If you can connect to your database with Microsoft SQL Server Management Studio, then you are **not using MySQL**. You need to use the Microsoft SQL Server JDBC driver and the correct connection string. – Mark Rotteveel Jan 27 '17 at 10:20
  • What port are you using to test it with Telnet? – Steve Smith Jan 27 '17 at 10:21
  • Is that the actual code you're running? If so, you need to put proper values in the connection string instead of `hostname:port/ourDatabasename` – Jiri Tousek Jan 27 '17 at 10:22
  • 1
    if you are using mssql server then change JDBC_DRIVER to `com.microsoft.sqlserver.jdbc.SQLServerDriver` and DB_URL to `jdbc:sqlserver://server:port;DatabaseName=dbname` – yash Jan 27 '17 at 10:25
  • Try to put DBUrl as jdbc:mysql://hostname:port/ourDatabasename – Anil Agrawal Jan 27 '17 at 11:21
  • Yes, thanks. First I have to use connection string for mssql, not for mysql:jdbc:sqlserver://hostName:serverPort;databaseName=ourDatabaseName;integratedSecurity=true"; Besiders, have to install sqljdbc and set native laboratory to x64 auth folder. Thank you very much. – valentinvas Jan 27 '17 at 13:10

1 Answers1

-1

have you copied this line from somewhere?

static final String DB_URL = "jdbc:mysql://hostname:port/ourDatabasename;integratedSecurity=true;";

This Exception is generally observed when the database isn't reachable due to an issue with the hostname and port number. Are you using them properly? eg. localhost:3306 It seems like when you are passing the arguments from

conn = DriverManager.getConnection(DB_URL, USER, PASS);

the DB_URL variable doesn't contain the correct address to connect to the database. Hope this helps!!

Tushar Gandhi
  • 237
  • 4
  • 17
  • No, I have an existing hostname, for example:'ourHostname' and it is actual. 'port' i extract from our Microsoft SQL Server Management Studio where we have 'sql server,port'. 'ourDatabasename' i use actual name not this one. – valentinvas Jan 27 '17 at 10:29