简体   繁体   中英

Java connection to mySQL database

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM