Home  >  Article  >  Database  >  远程mysql_java.sql.SQLException: null, message from server:_MySQL

远程mysql_java.sql.SQLException: null, message from server:_MySQL

WBOY
WBOYOriginal
2016-06-01 13:39:161373browse

bitsCN.com

最近在做一个项目,里面要用到远程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 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 <p>结果是这样的:</p><p><img src="http://img.bitscn.com/upimg/allimg/c140719/1405LL00093P-24B9.jpg" alt=""    style="max-width:90%"  style="max-width:90%"></p><p>那我们应该怎样解决这个问题呢?</p><p>我也是查了一下资料,才把这个问题给搞定的。。。。</p><p>其实道理很简单,也就是说,远程的机器B不允许机器A访问他的数据库。也就是说,我们要解决这个问题,就是要让机器B的数据库允许</p><p>机器A访问,就搞定啦;</p><p><strong>操作步骤也是很简单的:</strong></p><p>一:打开mysql控制台,输入:</p><pre class="brush:php;toolbar:false">1 use mysql;2 3 show tables;

二:输入:

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

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

四:下面是运行效果图:

返回值都为1,说明,程序运行正常....同时也祝你好运....

还有跟多方法:http://hi.baidu.com/soutnila/item/5356f2c0002781bf0d0a7bae

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn