2017-03-10 8 views
0

javaファイルから別のシステムにインストールされているmysqlデータベースにデータを挿入しようとしています。リモート接続のjdbcとmysqlによる通信リンクのエラー

これは、与えられたホスト名は、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(); 
     } 
    } 
} 

です。

私は、IPアドレスの代わりにホスト名で試してみましたが、コンソールに

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 

を例外の詳細を取得しています。

どうすれば接続できますか?

私を助けてください。

+0

は 'を持つマシンですMySQLはあなたと同じネットワークにインストール? –

+0

参照http://stackoverflow.com/a/15707789/2310289 –

+0

両方のシステムを同じネットワークに接続する必要がありますか? – Shambhu

答えて

0

1)それだけでローカル接続を受け入れているように(

2)コメントバインドアドレスのmy.cnfに行く)

"バインドアドレス= 127.0.0.1"

"#バインドアドレス= 127.0.0.1"

3)再起動mysqlの

+0

ありがとうございますが、my.cnfはどこにありますか? – Shambhu

+0

このリンクを参照してくださいhttp://stackoverflow.com/questions/1712646/i-can-not-find-my-cnf-on-my-windows-computer –

+0

初めてですが、私はこの問題に直面しています。 my.cnfファイルを変更するには? – Shambhu