0

i have a database for practice at phpMyAdmin. i use XAMPP. I am in very early stages of learing about the conection with a database. I followa tutorial and i think i am understanding the concept and everything is cool. But i stepped on a problem that despite the fact that there are answers on the internet, i cant solve it. here is my code:

import java.sql.*;

public class DBConnect {

    private Connection connection;
    private Statement statement;
    private ResultSet result;

    public DBConnect(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:1234/practicedb"); //code stucks here and after some minutes it is throwing an exception
            System.out.println("Connected");//this is never executed.
            statement = connection.createStatement();

        } catch (Exception ex) {
            System.out.print("Error in Constractor: "+ex);
        }
    }

    public void getData() {

        try {

            String query = "select * from cars";

            result = statement.executeQuery(query);
            while (result.next()) {
                String name = result.getString("carName");
                String id = result.getString("carID");
                String modelNum = result.getString("modelNumber");
                System.out.println("Car name: " + name + ", " + "Car ID: " + id + ", " + "Car model number: " + modelNum);
            }

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }
}

In the main class i create an instance of the class and then call the getData().

The code stucks in that line:

connection = DriverManager.getConnection("jdbc:mysql://localhost:1234/practicedb");

And it throws that:

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.java.lang.NullPointerException

This similar question was answered here: answer

But the suggestions are poor. I have tried flushing dns. I checked the URL and this is the one i connect to the database on phpmyadmin. i changed localhost to my ip adress. but all those just dont work.

I would really appreciate help. Is the first step on managing to receive that knowledge and i actually cant proceed at the moment. Thank you :)

  • i noticed that if i change the localhost:1234 to a random one like localhost:5432 it is throwing the error immediatelly. But when i have it on 1234(which i the port i have choosen through xampp config) then the programm runs for round about 5 minutes before it got terminated with the exception
Community
  • 1
  • 1
  • 5
    Why do you (try to) connect on port 1234? MySQL listens on 3306 – tkausl Sep 14 '16 at 14:01
  • Verify the database is started and run the following: netstat -ano | findstr 1234 (if on windows), if on linux netstat -anp | grep 1234. You see something like this "TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 1316" ( The last number is the PID, verify it's the same as the mysql server) – Tzach Solomon Sep 14 '16 at 14:10
  • Where are userName and Password in your connection string? Normally I use smth like this: `String url = "jdbc:mysql://127.8.92.130:3306/"; String dbName = "test"; String driver = "com.mysql.jdbc.Driver"; String userName = "admin"; String password = "pass"; String sslState="?useSSL=false"; con = DriverManager.getConnection(url+dbName+sslState, userName, password);` – Sergey Lotvin Sep 14 '16 at 14:19
  • it shows "tcp 0.0.0.0:1234 0.0.0.0:0 listening 6136" and "tcp [::]:1234 [::]:0 listening 6136 – Stamatis Stiliats Sep 14 '16 at 14:25
  • @SergeyLotvin i think this is a bit optional. i tried it and nothing changed. plus, what is this? " String sslState="?useSSL=false";" – Stamatis Stiliats Sep 14 '16 at 14:29
  • disable SSL and stop SSL errors – Sergey Lotvin Sep 14 '16 at 14:34
  • i see. Well so far nothing helped – Stamatis Stiliats Sep 14 '16 at 19:45
  • You've mentioned that you have configured MySQL to port 1234, I assume your `my.cnf` configuration file reflects this change of `port` from `3306` to `1234`. – N00b Pr0grammer Sep 15 '16 at 06:41
  • well i cant say for sure. You mean the my.ini from xampp folder? because i couldn't find my.cnf. I changed the ports from my.ini to 1234 wherever i found 3306. – Stamatis Stiliats Sep 15 '16 at 07:56

1 Answers1

-3

Usually MySQL listens on the default port 3306. If your database is named practicedb then your code should look like this:

private Connection connection;    

try {
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/practicedb");
    System.out.println("Connected");
} catch (Exception ex) {
    System.out.print("Error creating connection: "+ex);
}
  • i have changed the default port to 1234 because the default was having a conflict – Stamatis Stiliats Sep 14 '16 at 14:15
  • Welcome to StackOverflow! I'm not sure why you've been downvoted so severely; your answer could have included a bit more of an explanation but definitely was my first thought as well (though the poster's edit makes it seem like this is not the case). – Isaac Bennetch Sep 26 '16 at 18:49