gpt4 book ai didi

java - JDBC + MYSQL 通信链接失败

转载 作者:行者123 更新时间:2023-11-30 22:47:34 25 4
gpt4 key购买 nike

我肯定做错了什么或者我错过了什么但无法弄清楚那是什么。我安装 XAMPP Apache/MYSQL。我在本地连接,一切正常。我尝试更进一步并获得对数据库的远程访问。

问题

  1. 当我在浏览器中输入 myip/xampp 时,我只看到“连接已重置”,但对其他人来说还好吗? (Xampp欢迎页面)

  2. 无法通过 JDBC 连接 error com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 以前是工作的并且只更改主机名。

这是代码。

public DBConnect(String whichDataBase){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+whichDataBase+"?characterEncoding=UTF-8","monty","some_pass");
st = con.createStatement();
} catch (Exception e) {
System.out.println("error "+e);}
}

在数据库状态选项卡上,它看起来像是“连接”? (参见 host 但 command=sleep 并如上所述在 java 中出现错误)。但是当我使用 VPN 时,它的连接和工作正常吗? o_O

我做过的事情

  • 在路由器上转发端口 80 和 3306(在 http://www.yougetsignal.com/tools/open-ports/ 上测试)

  • my.ini 中更改了 socket = "TCP/IP"

  • 注释掉#enable-named-pipe#skip-networking

  • bind-address="0.0.0.0"(同时检查 127.0.0.1)

  • 数据库用户在 localhost%

    上获得了所有权限和 GRANT

我是初学者,非常感谢您的帮助!

编辑:

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 DBConnect.<init>(DBConnect.java:17)
at Poro.actionPerformed(Poro.java:297)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.addElement(Unknown Source)
at javax.swing.JComboBox.addItem(Unknown Source)
at Poro.<init>(Poro.java:231)
at Poro.main(Poro.java:287)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 24 more

最佳答案

尝试这样做:

public class MysqlConnect {

static String template = "jdbc:mysql://localhost/%s?useEncoding=true&characterEncoding=UTF-8&user=%s&password=%s";

public void connect(String database, String user, String password) {

Connection conn = null;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(String.format(template, database, user, password));
//...
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

所以调用 Class.forName("com.mysql.jdbc.Driver").newInstance() 并添加 usernamepassword 到你的连接字符串。

同时检查:Connecting to MySQL Using the JDBC

关于java - JDBC + MYSQL 通信链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29162791/

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