远程连接中与JDBC和MySQL的通信链接失败



我试图将数据从Java文件插入安装在另一个系统中的MySQL数据库中。

这是我的java文件

package serverDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Scanner;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class TestIn {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the values");
        System.out.println("id:  ");
        int id = sc.nextInt();
        System.out.println("name:  ");
        String name = sc.next();
        System.out.println("age:  ");
        int age = sc.nextInt();
        try {
            Connection con=DriverManager.getConnection("jdbc:mysql://192.168.43.116:3306/datacollect","root","spectrum7tech");
            System.out.println ("Database connection established");
            /*Statement stmt=(Statement) con.createStatement();
            stmt.executeUpdate("insert into ser_test_detail.details("+id+",'"+name+"',"+age+")");*/
            PreparedStatement st=(PreparedStatement) con.prepareStatement("INSERT INTO datacollect.namedetails(id, name, age) VALUES(?,?,?)");
            st.setInt(1,id);
            st.setString(2,name);
            st.setInt(3,age);
            st.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
             System.err.println("Got an exception! "); 
             System.err.println(e.getMessage()); 
             e.printStackTrace();
        }
    }
}

给定的主机名是包含mySQL数据库的另一个系统。

我在控制台中获得了例外详细信息

Enter the values
id:  
24
name:  
easrdtfghj
age:  
23
Got an exception! 
Communications link failure
Last packet sent to the server was 1 ms ago.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 1 ms ago.
    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:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    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:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at serverDemo.TestIn.main(TestIn.java:24)
Caused by: java.net.UnknownHostException: LAPTOP-6JMVNJOQ
    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:243)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2026)
    ... 12 more

我尝试使用IP地址而不是主机名。

如何建立连接?

请帮助我。

1)转到my.cnf

2)注释bind-address(仅接受本地连接)

" bind-address = 127.0.0.1"

to

"#bind-address = 127.0.0.1"

3)重新启动mysql

相关内容

  • 没有找到相关文章

最新更新