I am trying to insert data from a java file into the mysql database which is installed in another system.
This is my java file
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();
}
}
}
The given hostname is of the another system containing mysql database.
I am getting exception details in console
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
I have tried with IP address instead of hostname.
How can I establish the connection?
Please, help me out.
1) go to my.cnf
2) Comment bind-address(As it is accepting to local connections only)
"bind-address = 127.0.0.1"
to
"#bind-address = 127.0.0.1"
3) Restart mysql