您的位置:首页 > 编程语言 > Java开发

远程mysql_java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect

2013-10-09 22:15 751 查看
最近在做一个项目,里面要用到远程mysql数据库。

我想把我想要实现的功能说一下吧:

1 /**
2          * 是这样的功能:我的机器是A,我现在先利用我自己写的一个jdbc方法<br>
3          * 调用远程的机器B上安装的数据库<br>
4          * 然后把我想要CRUD操作B机器上的数据库<br>
5          * 或者简单一点,可以这样认为,我现在在机器A上面有一些数据<br>
6          * 我要把这些数据插入到远程的机器B的mysql数据库里面去<br>
7          * 就是这样的操作<br>
8          */


效果图如下:



下面是我在机器A上面写的测试类:

1 /**
2  *
3  */
4 package edu.gzucm.thms.utils;
5
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.PreparedStatement;
9 import java.sql.SQLException;
10 import java.util.Calendar;
11
12 /**
13  * @author Hongten</br>
14  * @date 2012-10-18
15  *
16  */
17 public class SendData {
18
19     public static int sendDate2Server(String ip, String port,
20             String databaseName, String userName, String passwrod,String receiver,String content) {
21         int flag = 0;
22         try {
23             // 加载MySql的驱动类
24             Class.forName("com.mysql.jdbc.Driver");
25         } catch (ClassNotFoundException e) {
26             System.out.println("找不到驱动程序类 ,加载驱动失败!");
27             e.printStackTrace();
28         }
29         // 连接MySql数据库,用户名和密码都是root
30         String url = "jdbc:mysql://"+ip+":"+port+"/"+databaseName;
31         System.out.println(url);
32         String username = "root";
33         String password = "root";
34         String sql = getSQL(receiver, content);
35         Connection conn = null;
36         PreparedStatement pstmt = null;
37         try {
38             conn = DriverManager.getConnection(url, username, password);
39             pstmt = conn.prepareStatement(sql);
40             flag = pstmt.executeUpdate(sql);
41         } catch (SQLException se) {
42             System.out.println("数据库连接失败!");
43             se.printStackTrace();
44         }
45         if (pstmt != null) { // 关闭声明
46             try {
47                 pstmt.close();
48             } catch (SQLException e) {
49                 e.printStackTrace();
50             }
51         }
52         if (conn != null) { // 关闭连接对象
53             try {
54                 conn.close();
55             } catch (SQLException e) {
56                 e.printStackTrace();
57             }
58         }
59         return flag;
60     }
61
62     public static String getSQL(String receiver,String content){
63         /**
64          * 下面是一些常量设置: retrytimes=0 pri=1 inpool=0 sendmode=1
65          * sendtime和inserttime的格式是:2011-05-03 18:55:20
66          */
67         String now = getNow();
68         return "insert into sms_boxsending(sender,receiver,content,sendtime,inserttime,retrytimes,pri,inpool,sendmode)values('system','"+receiver+"','"+content+"','"+now+"','"+now+"','0','1','0','1')";
69     }
70
71     /**
72      * 获取当前时间,并格式化时间
73      *
74      * @return 格式化当前的时间
75      */
76     public static String getNow() {
77         Calendar calendar = Calendar.getInstance();
78         int year = calendar.get(Calendar.YEAR);
79         int month = calendar.get(Calendar.MONTH);
80         int day = calendar.get(Calendar.DAY_OF_MONTH);
81         int hour = calendar.get(Calendar.HOUR_OF_DAY);
82         int min = calendar.get(Calendar.MINUTE);
83         int sec = calendar.get(Calendar.SECOND);
84
85         Object monthString = (month < 10) ? "0" + month : month;
86         Object dayString = (day < 10) ? "0" + day : day;
87         Object hourString = (hour < 10) ? "0" + hour : hour;
88         Object minString = (min < 10) ? "0" + min : min;
89         Object secString = (sec < 10) ? "0" + sec : sec;
90
91         return "" + year + "-" + monthString + "-" + dayString + " "
92                 + hourString + ":" + minString + ":" + secString;
93     }
94
95     public static void main(String[] args) {
96         //这里是调用远程机器B的mysql数据库
97         System.out.println(sendDate2Server("210.38.111.34", "3306", "messagecat", null, null,"1342360****","this is a test 短信"));
98         //这是测试本地的
99         System.out.println(sendDate2Server("localhost", "3308", "messagecat", null, null,"1342360****","this is a test 短信"));
100     }
101 }


结果是这样的:



那我们应该怎样解决这个问题呢?

我也是查了一下资料,才把这个问题给搞定的。。。。

其实道理很简单,也就是说,远程的机器B不允许机器A访问他的数据库。也就是说,我们要解决这个问题,就是要让机器B的数据库允许

机器A访问,就搞定啦;

操作步骤也是很简单的:

一:打开mysql控制台,输入:

1 use mysql;
2
3 show tables;




二:输入:

1 select host from user;
2
3 update user set host ='%' where user ='root';




三:进入计算机的服务界面,重新启动mysql服务就搞定啦。。



四:下面是运行效果图:



返回值都为1,说明,程序运行正常....同时也祝你好运....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐