【问题标题】:Insertion of record in Remote sql server在远程 sql server 中插入记录
【发布时间】:2016-03-07 01:28:39
【问题描述】:

您好,下面是我的 jdbc 代码,我想在远程机器中插入一条记录,当我使用与本地数据库服务器相同的代码时,我能够成功插入记录。但是当我尝试使用远程机器时,它会抛出以下错误。

请告诉我为创建连接传递的参数是否足够需要添加一些其他东西。

错误:: 当我运行上面的代码时,遇到异常

如果我在 url 中传递的连接细节是否足以建立连接,是否需要添加其他内容。

为了连接到远程sql server,只需要jdbc代码就足够了,或者应该开发成web应用程序。

有人请告诉我我需要做什么来解决这个问题!

谢谢

         public class JdbcTestClass {
         public static void main(String [] args) throws SQLException
         {
          try {
         Class.forName("com.mysql.jdbc.Driver");
         Connection con= DriverManager.getConnection("jdbc:mysql://120....:3306/example","root","root");
         Statement st = con.createStatement();
         st.executeUpdate("insert into test.student values(9966,'pradeep',93);");  
         System.out.println("Record Inserted Successfuly :: ");
         if(st != null)
         {
         st.close();
         System.out.println("connection closed successfuly :: "+st);
         }
         if(con != null)
         {
         con.close();
         }
        } 
         catch (ClassNotFoundException e) {
         e.printStackTrace();
         System.out.println("Error In 1st try :: "+e);
         }
        }
        }

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago.

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    请参考这个网址的答案:

    Why I get this error 'Communications link failure The last packet sent...' when connecting to MySQL with Java (Netbeans)?

    The problem was with mysql configuration file on the server /etc/mysql/my.cnf
    
    the line : bind-address should point to your server's IP like in this example
    
    bind-address = 192.168.1.5
    
    and not
    
    bind-address = 127.0.0.1
    
    to allow remote access.
    

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2015-12-17
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多