我正在尝试在Java和数据库之间建立连接。我正在使用Eclipse和xampp。我几乎确信我具有Eclipse和xampp的良好配置,但是也许我错过了一些东西。我在Internet上进行了很多搜索,但没有找到解决方案。
我的错误是:
SQLException:无法创建与数据库服务器的连接。尝试重新连接3次。放弃。 SQL状态:08001 VendorError:0
Xampp- Xampp config
Eclipse- 我的工作区文件夹中有jar文件。 Eclipse config
phpmyadmin- 我不需要密码即可登录localhost / phpmyadmin,并且在数据库中只有一条记录。
代码
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC2 {
static String daneZBazy;
static String polaczenieURL = "jdbc:mysql://localhost:3306/heroes.db?
autoReconnect=true&useSSL=false";
static String login = "root";
static String password = "root";
public static void main(String[] args) {
// Question to DB
String query = "Select * FROM heroestab";
Connection conn = null;
try {
// Connection parameters
conn = DriverManager.getConnection(polaczenieURL, login, password);
// MySQL Driver
Class.forName("com.mysql.jdbc.Driver");
// Start question to DB
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
wyswietlDaneZBazy(rs);
}
conn.close();
}
//throws exception
catch (ClassNotFoundException wyjatek) {
System.out.println("Driver error");
}
catch (SQLException wyjatek) {
wyjatek.printStackTrace();
System.out.println("Login problem. Check, username, password, DB name, IP adress");
System.out.println("SQLException: " + wyjatek.getMessage());
System.out.println("SQLState: " + wyjatek.getSQLState());
System.out.println("VendorError: " + wyjatek.getErrorCode());
}
}
static void wyswietlDaneZBazy(ResultSet rs) {
try {
daneZBazy = rs.getString(1);
System.out.println("\n" + daneZBazy + " ");
daneZBazy = rs.getString(2);
System.out.println(daneZBazy + " ");
daneZBazy = rs.getString(3);
System.out.println(daneZBazy);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
请确认用户名和密码均正确。
我需要来自以下位置的完整堆栈跟踪:
wyjatek.printStackTrace();
如果用户名(root)和密码不正确,您将在堆栈跟踪中看到以下行:
Access denied for user 'root'@'localhost' (using password: YES)
如果我没记错的话,使用默认的xampp配置,您可以尝试以下操作:
static String login = "root"; static String password = "";
答案 1 :(得分:0)
我可以看到我的数据库使用cmd:
- C:\ xampp \ mysql \ bin> mysql -u根-p
- 输入
- 显示数据库;然后我可以看到我在本地主机上拥有的所有数据库。当我 选择英雄,我可以从英雄中选择*;
图片: Database cmd