以下是我的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.
答案 0 :(得分:1)
请参阅此网址的答案:
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.