mysql sqlexception_远程mysql_java.sql.SQLException: null, message from server: "Host 'xxx' is not allo...

1 /**

2 *3 */

4 packageedu.gzucm.thms.utils;5

6 importjava.sql.Connection;7 importjava.sql.DriverManager;8 importjava.sql.PreparedStatement;9 importjava.sql.SQLException;10 importjava.util.Calendar;11

12 /**

13 *@authorHongten14 * @date 2012-10-1815 *16 */

17 public classSendData {18

19 public static intsendDate2Server(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 returnflag;60 }61

62 public staticString getSQL(String receiver,String content){63 /**

64 * 下面是一些常量设置: retrytimes=0 pri=1 inpool=0 sendmode=165 * sendtime和inserttime的格式是:2011-05-03 18:55:2066 */

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 staticString 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 voidmain(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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.sql.SQLException: null, message from server: "Host '59.41.160.220' is not allowed to connect to this MySQL server"是一个错误消息,表示指定的主机(59.41.160.220)没有权限连接到MySQL服务器。这通常是由于MySQL服务器的访问控制列表(ACL)配置导致的。 要解决这个问题,可以采取以下步骤: 1. 登录到MySQL服务器。 2. 运行以下命令以授予指定主机的访问权限: GRANT ALL PRIVILEGES ON *.* TO 'username'@'59.41.160.220' IDENTIFIED BY 'password'; 其中,'username'是您希望使用的用户名,'59.41.160.220'是您希望授予访问权限的主机IP地址,'password'是您希望使用的密码。 3. 刷新MySQL权限以使更改生效: FLUSH PRIVILEGES; 4. 退出MySQL服务器。 通过执行上述步骤,您应该能够解决java.sql.SQLException: null, message from server: "Host '59.41.160.220' is not allowed to connect to this MySQL server"错误,并允许指定主机连接到MySQL服务器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [bug-linux-win(java.sql.SQLException: null, message from server: “Host ‘192.168.1.66‘ is not ...](https://blog.csdn.net/computer408/article/details/123920935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java.sql.SQLException: null, message from server: “Host ‘%’ is not allowed to connect to](https://download.csdn.net/download/weixin_38720653/13687787)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值