Java - 尝试与远程SQL数据库建立连接

时间:2013-11-16 19:35:39

标签: java mysql jdbc

我正在尝试连接到我可以通过http://smart.ihu.edu.gr/phpmyadmin/通过网络访问的远程SQL数据库。内部的数据库是awesomedb/pwndoes'。这就是我想要使用的表格。

问题是,我对应用程序中的数据库/ mySQL连接完全不熟悉,即使我已经尝试过这里发布的示例:Connect Java to a MySQL database我还无法连接到数据库。

这是我的代码:

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

我确信我输入的网址错误。 请注意,数据库是远程的,我的计算机上没有运行任何mySQL软件的实例。是否可以通过这种方式将简单的Java应用程序与远程数据库连接起来?

注意2:我在smart.ihu.edu.gr链接的末尾尝试了:3306,但无济于事。感谢抬头,@ NickJ

顺便说一下,这是build + run的结果:

results

(完全解决方案:http://i.imgur.com/qlj6jJC.png

2 个答案:

答案 0 :(得分:3)

Java返回Unknown Database,因为它在/awesomedb/pwnodes上搜索数据库,但您提供的路径是表。
您必须向Java提供awesomedb的路径并连接到它 之后你发送类似的东西:

INSERT INTO pwnodes (col_name,...)

连接代码:

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 :(得分:2)

我认为您不必转到phpmyadmin网址路径。 我的假设是MySQL在该服务器上的端口3306上运行,但您不需要访问phpmyadmin

所以我尝试以下网址:jdbc:mysql://smart.ihu.edu.gr:3306/awesomedb/pwnodes

相关问题