gpt4 book ai didi

java - com.mysql.cj.jdbc.exceptions.CommunicationsException : Communications link failure

转载 作者:行者123 更新时间:2023-11-29 11:21:10 34 4
gpt4 key购买 nike

我正在尝试将我的java应用程序与sql数据库连接,当我单击“保存”按钮时,它给出以下错误

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

这是我的代码

        name = full_name_input.getText();
Fname = father_name_input.getText();
cnic = father_cnic_input.getText();
DOB = Integer.parseInt(DOB_input.getText());
Class_V = Integer.parseInt(class_input.getText());
prsnt_add = present_add_input.getText();
city = city_input.getText();
province = radio_text;
Connection conn = null;
PreparedStatement pst = null;

try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///UserDetails", "root", "sahanj");
pst = conn.prepareStatement("insert into UserDetails values (?,?,?,?,?,?,?,?)");

pst.setString(1, name);
pst.setString(2, Fname);
pst.setString(3, cnic);`enter code here`
pst.setInt(4, DOB);
pst.setInt(5, Class_V);
pst.setString(6, prsnt_add);
pst.setString(7, city);
pst.setString(8, province);

int x = pst.executeUpdate();

if (x>0)
{
JOptionPane.showMessageDialog(null, "Record Saved !\n");
}
else
{
JOptionPane.showMessageDialog(null, "Data Not Saved !\n");
}
}

catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}

最佳答案

此错误通常是由以下原因造成的。1. JDBC URL 中的主机名不正确。2.端口号不正确。3.db服务器没有起来。对于这些采取以下步骤1. 提供完整的 JDBC url 或使用 ping 进行测试。2.查看mysql的my.cnf。3.启动数据库。

关于java - com.mysql.cj.jdbc.exceptions.CommunicationsException : Communications link failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909705/

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