Java:连接到远程数据库时出现异常

时间:2017-05-16 13:57:46

标签: java mysql remote-access vps

我尝试使用使用JAVA构建的桌面应用程序连接到我的VPS上的远程数据库。
我通过在VPS中添加此行来授予访问我的IP地址的权限: enter image description here

之后我创建了一个JAVA类来测试连接:

package smt.agm.launcher;
import java.sql.*;
public class Test {

    // JDBC driver name and database URL
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
       static final String DB_URL = "jdbc:mysql://my_vps:port/my_database";

       //  Database credentials
       static final String USER = "user";
       static final String PASS = "password";

       public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");

          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);

          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
          sql = "SELECT * FROM table";
          ResultSet rs = stmt.executeQuery(sql);

          //STEP 5: Extract data from result set
          while(rs.next()){
             //Retrieve by column name
             String col1= rs.getString("column1");
             String col2= rs.getString("column2");

             //Display values
             System.out.print("column1: " + col1 + ", column2: "+col2);
          }
          //STEP 6: Clean-up environment
          rs.close();
          stmt.close();
          conn.close();
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println("Goodbye!");
    }//end main

}

但我遇到了这个例外:

PacketTooBigException: Packet for query is too large (5 526 600 > 65 535). You can change this value on the server by setting the 'max_allowed_packet' variable.

所以我做了一些研究,我找到了一种方法来使用ssh增加变量max_allowed_packet

vi /etc/my.cnf

我已将max_allowed_packet的值增加到 500M 不幸的是,这没有任何帮助,我也得到了相同的例外情况:

PacketTooBigException: Packet for query is too large (5 526 600 > 65 535). You can change this value on the server by setting the 'max_allowed_packet' variable.

我在这里缺少什么?

0 个答案:

没有答案