Skip to content
Advertisement

Can not connect to MySQL with Eclipse java IDE on linux ubuntu, but with a code that worked perfectly on windows

I was working on a small project (mostly for fun) on java with Eclipse Neon (and window builder), in which I had to connect to MySQL and retrieve certain info. In windows it worked great, I was able to connect, get the info, etc. But then I switched to Linux Ubuntu 16.04 and it just won’t connect to MySQL. I already have the connector’s path built and all that. I have no idea on what’s happening or how to fix it, since I’m quite new to java programming (or programming in general) and haven’t read any books about it. Below I’ll leave some of the code (at least what I think is important).

Edit:

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test extends JFrame {

    Connection Con;
    Statement Sent;

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/BDConductores?" + "user=root&password=PanXo_666");
                Statement st = con.createStatement();
                ResultSet result = st.executeQuery("select * from Proyectos");

                while (result.next()) {

                }

        } catch (ClassNotFoundException e) {
            System.out.println("Driver not found");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public Test() {
        PrepararBaseDatos();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    }

    void PrepararBaseDatos() {
        try {
            String controlador = "com.mysql.jdbc.Driver";
            Class.forName(controlador).newInstance();
            String servidor = "jdbc:mysql://localhost/BDConductores/";
            String usuarioDB = "root";
            String passwordDB = "PanXo_666";

            Con = DriverManager.getConnection(servidor, usuarioDB, passwordDB);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al cargar controlador");
        }
        try {
            Sent = Con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al cargar controlador");
        }
    }
}

and there’s no error message, (besides the one in the code) it just won’t load the controller and i have no idea what happens :c

Advertisement

Answer

at the end I was able to see what was wrong… I wrote the wrong server name. it wasn’t like this:

String servidor = "jdbc:mysql://localhost/BDConductores/";

it was like this:

String servidor = "jdbc:mysql://127.0.0.1:3306/BDConductores?";

So now my program connects to my database with no problems

Advertisement