简体   繁体   English

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

[英]Java - Trying to establish a connection with a remote SQL Database

I'm trying to connect to a remote SQL database that I can access via web through http://smart.ihu.edu.gr/phpmyadmin/ . 我正在尝试连接到远程SQL数据库,该数据库可以通过http://smart.ihu.edu.gr/phpmyadmin/通过Web访问。 The database inside that is awesomedb/pwndoes' . 里面的数据库是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. 事实是,我对应用程序中的数据库/ mySQL连接是完全陌生的,即使我已经尝试通过此处发布的示例进行工作: 将Java连接到MySQL数据库,但是我仍然无法连接到数据库。

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. 我确信我输入的URL错误。 Note that the database is remote and I do not have an instance of any mySQL software running on my machine . 请注意,该数据库是远程数据库,并且我的计算机上没有运行任何mySQL软件的实例 Is it possible to just connect a simple java app with a remote DB this way? 这样可以将简单的Java应用程序与远程数据库连接吗?

Note 2: I have tried putting :3306 at the end of the smart.ihu.edu.gr link, to no avail. 注意2:我尝试将:3306放在smart.ihu.edu.gr链接的末尾,无济于事。 Thanks for the headsup, @NickJ 感谢您的注意,@ NickJ

By the way, here's the result of the build+run: 顺便说一下,这是build + run的结果:

结果

(Full resolution here: http://i.imgur.com/qlj6jJC.png ) (此处的完整分辨率: http : //i.imgur.com/qlj6jJC.png

Java returns Unknown Database because it´s searching for a database on /awesomedb/pwnodes but the path you give is a table. Java返回Unknown Database因为它正在/awesomedb/pwnodes上搜索数据库,但是您提供的路径是一个表。
You have to give Java the path to your awesomedb and connect to it. 您必须为Java提供awesomedb的路径并连接到它。
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!");
}
}
}

I don't think you have to go to phpmyadmin URL path. 我认为您不必转到phpmyadmin URL路径。 My assumption is that MySQL runs on port 3306 on that server, but you don't need to reach phpmyadmin . 我的假设是MySQL在该服务器上的端口3306上运行,但是您无需访问phpmyadmin

So I'd try the following URL: jdbc:mysql://smart.ihu.edu.gr:3306/awesomedb/pwnodes 因此,我尝试使用以下URL: jdbc:mysql://smart.ihu.edu.gr:3306/awesomedb/pwnodes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试建立与 MySQL 数据库的连接时出现 StackOverflowError - StackOverflowError when trying to establish connection to MySQL database 获取'java.sql.SQLRecoverableException:IO错误:网络适配器无法建立连接',同时连接oracle数据库 - Getting 'java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection' while connecting oracle database 无法在Eclipse IDE中使用Java建立与SQL Server 2008的数据库连接 - Unable to establish database connection to SQL Server 2008 using java in Eclipse IDE Maven 在 spring 项目构建中失败,试图与数据库建立连接 - maven fail in spring project build trying to establish connection with database 如何建立与数据库的连接 - How to establish connection with database 与数据库建立连接的要求 - Requirements to establish a connection with the database 尝试从Java客户端建立SSL连接时出错 - Error trying to establish SSL connection from java client 如何使用 JDBC Java 与 .xml 文件建立数据库连接 - how to establish a database connection with an .xml file using JDBC Java 使用Java在App Server和Oracle数据库之间建立连接 - Establish connection between app server and oracle database using java 如何在Java应用程序和数据库之间建立安全连接? - How to establish secure connection between java application and database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM