1

I'm trying to connect to a remote SQL database that I can access via web through http://smart.ihu.edu.gr/phpmyadmin/. The database inside that is awesomedb/pwndoes'. That's where the tables that I want to use are.

The thing is, I'm completely new to Database/mySQL connectivity in applications and even though I've tried working through the examples posted here: Connect Java to a MySQL database I've been unable to connect to the DB yet.

Here's my code:

package thesistest;

import java.sql.*;


public class ThesisTest {


public static void main(String[] args) 
{   
    //-------------------------------------
    //  JDBC driver initialisation
    //-------------------------------------
    try
    {
        System.out.println("Loading driver...");
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded!");
    }
    catch(ClassNotFoundException e)
    {
        throw new RuntimeException("Cannot find the driver in the classpath!", e);
    }

    //-------------------------------------
    //  Driver loaded, let's try establishing a connection.
    //-------------------------------------
    String url = "jdbc:mysql://smart.ihu.edu.gr/phpmyadmin/awesomedb/pwnodes";
    String username = "root";
    String password = "[redacted]";
    Connection connection = null;
    try
    {
        System.out.println("Connecting database...");
        connection = DriverManager.getConnection(url, username, password);
        System.out.println("Database connected!");
    }
    catch (SQLException e)
    {
        throw new RuntimeException("Cannot connect the database!", e);
    } 
    finally 
    {
        System.out.println("Closing the connection.");
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }

}   //end of main

}   //end of class ThesisTest

I'm convinced that the URL I'm typing in is wrong. Note that the database is remote and I do not have an instance of any mySQL software running on my machine. Is it possible to just connect a simple java app with a remote DB this way?

Note 2: I have tried putting :3306 at the end of the smart.ihu.edu.gr link, to no avail. Thanks for the headsup, @NickJ

By the way, here's the result of the build+run:

results

(Full resolution here: https://i.stack.imgur.com/etHVv.png)

Community
  • 1
  • 1
Dimitris Sfounis
  • 2,400
  • 4
  • 31
  • 46

2 Answers2

3

Java returns Unknown Database because it´s searching for a database on /awesomedb/pwnodes but the path you give is a table.
You have to give Java the path to your awesomedb and connect to it.
After that you send something like:

INSERT INTO pwnodes (col_name,...)

Code for connecting:

package theistest;


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

   public class ThesisTest {

 public static void main(String[] argv) {

System.out.println("-------- MySQL JDBC Connection Testing ------------");

try {
Class.forName("com.mysql.jdbc.Driver");
 } catch (ClassNotFoundException e) {

System.out.println("Where is your MySQL JDBC Driver?");
    e.printStackTrace();
    return;
}

System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;

try {
    String url = "jdbc:mysql://smart.ihu.edu.gr:3306/awesomedb";
    String username = "root";
    String password = "[redacted]";
connection = DriverManager.getConnection(url, username, password);

} catch (SQLException e) {
    System.out.println("Connection Failed! Check output console");
    e.printStackTrace();
    return;
}

if (connection != null) {
    System.out.println("You made it, take control your database now!");
} else {
    System.out.println("Failed to make connection!");
}
}
}
  • 1
    Thank you for your answer. Your insight, combined with Andrei Nicusan's answer down there was the correct answer. With `String url = "jdbc:mysql://smart.ihu.edu.gr/awesomedb";` I managed to connect. Thank you both, a lot. – Dimitris Sfounis Nov 16 '13 at 19:57
  • Please edit out /phpmyadmin/ so that the url String looks like the above comment, so I can choose your answer as the correct solution :) – Dimitris Sfounis Nov 16 '13 at 20:01
1

I don't think you have to go to phpmyadmin URL path. My assumption is that MySQL runs on port 3306 on that server, but you don't need to reach phpmyadmin.

So I'd try the following URL: jdbc:mysql://smart.ihu.edu.gr:3306/awesomedb/pwnodes

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
  • It certainly cut down on the number of errors, but the connection still failed. Here's the build results, if you're interested: http://i.imgur.com/pgDbZKk.png – Dimitris Sfounis Nov 16 '13 at 19:50
  • you're missing the leading "j" in the URL – Andrei Nicusan Nov 16 '13 at 19:52
  • Yeah, came here to say just that. Even with jdbc:mysql://link restored, it still complains about `com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'awesomedb/pwnodes'` – Dimitris Sfounis Nov 16 '13 at 19:54
  • Here are the rules for building a MySQL JDBC URL: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html . Are you sure the DB name is correct? – Andrei Nicusan Nov 16 '13 at 19:57
  • The URL was pointing to a table instead of the actual db. Look at Schlenderman's answer. Combined with yours it gave me the solution ;) – Dimitris Sfounis Nov 16 '13 at 19:59