gpt4 book ai didi

Java 连接 MySQL 失败

转载 作者:行者123 更新时间:2023-11-29 22:14:13 24 4
gpt4 key购买 nike

我已经在 hostinger.co.uk 创建了一个 MySQL 数据库现在我正在尝试在数据库和我的 Java 应用程序之间建立连接,为此我编写了以下 Java 代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DB_Connection {

public static void main(String arg[]) throws Exception {
makeConnection("u335300563_myName", "pass12345");
}

public static void makeConnection(String username, String password) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://u335300563_database",username,password);
PreparedStatement statement = connection.prepareStatement("select * from example");
ResultSet result = statement.executeQuery();
for(int i=1; result.next(); i++)
System.out.println(result.getString(i));
}
}

但这引发了一个异常:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DB_Connection.makeConnection(DB_Connection.java:14)
at DB_Connection.main(DB_Connection.java:9)
Caused by: java.net.UnknownHostException: u335300563_database
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:190)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 16 more

请告诉我问题是什么?

最佳答案

Caused by: java.net.UnknownHostException: u335300563_database

程序无法识别mysql数据库的主机。

在程序中,您必须传递托管 mySQL 的系统的正确主机名或 IP 地址:

以下行需要更正

DriverManager.getConnection("jdbc:mysql://u335300563_database"
,username,password);

DriverManager.getConnection("jdbc:mysql://<IP Address of MySQL DB server or correct hostname with port number >",
username,password);

关于Java 连接 MySQL 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com