1

I am trying to connect a Java application to a SQL database that I set up on my local computer. There is no server so all of the answers im finding when trying to research it are not working or applicable. The error I get when I run this is:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Caused by: java.net.ConnectException: Connection refused: connect

I have verified the username and password for SQL and verified that the account has full admin rights. Any ideas? Im at a loss...

package database_console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnect {


    public static void main(String[] args) {
try{
        String host= "jdbc:mysql://[myIPaddress]/MTGDatabase";
       String uName = "Java";
       String uPass = "pass";
        Connection con = DriverManager.getConnection(host , uName, uPass );

    }  
    catch(Exception e){
    e.printStackTrace();    
    }
    }
}
shrek
  • 887
  • 6
  • 12
Mary Scott
  • 11
  • 1
  • 2
    the error means the SQL server is down, or you entered wrong IP/port. try `127.0.0.1` or `localhost` as your IP address. `jdbc:mysql://localhost:3306/MTGDatabase` or whatever port your SQL uses – user2914191 Jun 28 '18 at 23:16
  • Possible duplicate of https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database – Madushan Perera Jun 28 '18 at 23:18
  • Please add the exception stack trace to your question – Scary Wombat Jun 29 '18 at 02:05
  • I tried localhost:3306, localhost:1443, 127.0.0.1:3306, and 127.0.0.1:1443. None of those worked, i received the same error. I ran a query in SQL that provides the port number and it returned "NULL" as the port number. Also, I cannot comment the entire stack trace of the error because it has too many characters – Mary Scott Jun 30 '18 at 01:15

2 Answers2

1

Replace the [myIPaddress] with the either "127.0.0.1:3306" or "localhost:3306". '127.0.0.1' and 'localhost' are the same thing referring to your own local machine. 3306 is the port on which MqSQL is listening for new connections.

String host = "jdbc:mysql://localhost:3306/MTGDatabase"; 

or

String host = "jdbc:mysql://127.0.0.1:3306/MTGDatabase";

This should solve your current problem :)


But on a different node :

It is quite an old technique to obtain database connections via DriverManager. A more better way is to use DataSource, either by looking one up that your server container already configured for you:

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/MTGDatabase");

or instantiating and configuring one from your database driver directly:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("Java");
dataSource.setPassword("pass");
dataSource.setServerName("localhost");

and then obtain connections from it, same as above:

Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM MYTABLE");
...
rs.close();
stmt.close();
conn.close();
user1455719
  • 1,045
  • 4
  • 15
  • 35
-1

You need put the port String host= "jdbc:mysql://[myIPaddress]:[port]/MTGDatabase"; and create a Statement to execute querys Statement stmt=con.createStatement();

Pablo DbSys
  • 532
  • 1
  • 7
  • 17
  • Worth noting that the port is likely to be 1433, unless you've specifically set it to something different. – Dawood ibn Kareem Jun 28 '18 at 23:18
  • @DawoodibnKareem Is it worth noting that the port is unlikely to be 1433 for mysql – Scary Wombat Jun 29 '18 at 02:07
  • Oh dear, I misread that completely. For some reason, I thought OP had said they were using Microsoft SQL Server. So, @Scary and Helpful Wombat, are you going to tell us the port, or are you just going to narrow it down to "probably not 1433"? – Dawood ibn Kareem Jun 29 '18 at 02:10
  • (Likely to be 3306 by the way). – Dawood ibn Kareem Jun 29 '18 at 02:10
  • @DawoodibnKareem The other question and the above comment have already mentioned that the port is 3306, so I think I will not bother mentioning that the port is likely to be 3306. Maybe I should mention it just for completeness. – Scary Wombat Jun 29 '18 at 02:16
  • I have tried 1433 and 3306 as the port number and neither worked. I also ran a query in SQL that should tell me the port number being used and it came back with "NULL" – Mary Scott Jun 30 '18 at 01:11