[英]StackOverflowError when trying to establish connection to MySQL database
[英]Java - Trying to establish a connection with a remote SQL Database
我正在嘗試連接到遠程SQL數據庫,該數據庫可以通過http://smart.ihu.edu.gr/phpmyadmin/通過Web訪問。 里面的數據庫是awesomedb/pwndoes'
。 那就是我要使用的表的位置。
事實是,我對應用程序中的數據庫/ mySQL連接是完全陌生的,即使我已經嘗試通過此處發布的示例進行工作: 將Java連接到MySQL數據庫,但是我仍然無法連接到數據庫。
這是我的代碼:
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
我確信我輸入的URL錯誤。 請注意,該數據庫是遠程數據庫,並且我的計算機上沒有運行任何mySQL軟件的實例 。 這樣可以將簡單的Java應用程序與遠程數據庫連接嗎?
注意2:我嘗試將:3306放在smart.ihu.edu.gr鏈接的末尾,無濟於事。 感謝您的注意,@ NickJ
順便說一下,這是build + run的結果:
(此處的完整分辨率: http : //i.imgur.com/qlj6jJC.png )
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!");
}
}
}
我認為您不必轉到phpmyadmin
URL路徑。 我的假設是MySQL在該服務器上的端口3306上運行,但是您無需訪問phpmyadmin
。
因此,我嘗試使用以下URL: jdbc:mysql://smart.ihu.edu.gr:3306/awesomedb/pwnodes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.