簡體   English   中英

使用Java連接到MySQL數據庫

[英]Connecting to a MySQL Database with Java

我想用Java連接到我的MySQL數據庫。

我正在使用JDBC,並且已安裝驅動程序。 (com.mysql.jdbc.Driver)

唯一的問題是我不斷收到錯誤消息:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException成功發送到服務器的最后一個數據包是0毫秒前。

這是我的代碼:

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password");

我對如何撰寫URL(以及在哪里獲取用戶名和密碼)並不滿意,但是我已經進行了大量研究。

我是唯一訪問我的數據庫和域的人,因此詢問管理員沒有任何用處。

我使用phpMyAdmin創建數據庫並進行管理。 我使用phpMyAdmin用戶名和密碼還是什么?

順便說一句,我的網站托管在Yahoo!上。 小本生意。

所以我的問題是:

  • 如何建立連接網址?
  • 我的用戶名和密碼是什么?

我想說的是您的網址缺少正斜杠。

Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password");

或者我覺得您的訪問權限有問題。 我也發生了同樣的事情,這是防火牆阻止服務器端口的問題。 因此,驗證情況並非如此。

Load the drivers for mysql
 Class.forName("com.mysql.jdbc.Driver");
connect with the data base
 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");               
.....jdbc:mysql://this is use as it is
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of      "127.0.0.1"
.....dbuser is the data base user
.....dbpassword is the password of the dbuser
.....3306 is the default port used for database

如何建立連接網址?

您是否在網址中缺少斜線? 我本來以為是這樣的:

jdbc:mysql://server/database

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM