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?