0

我正在使用带有 Java 的 MVC 结构开发一个 Web 应用程序,它将在下拉菜单中执行用户从网页中选择的查询,并将显示代码所连接的 DB2 数据库中的数据。

我已经设置我的代码能够成功地做到这一点;但是,仅在我第一次建立连接时。如果我要回击浏览器并选择另一个命令,则网页不会显示任何内容,并且我在 Eclipse 的控制台中收到以下错误:

SQLException: [jcc][t4][2034][11148][4.13.127] 由于分发协议错误导致会话解除分配,因此执行失败。检测到 DRDA 数据流语法错误。原因:0x2110。ERRORCODE=-4499, SQLSTATE=58009 VendorError: -4499

这是代码:我还没有实现从文件中加载信息,但是所有信息都可以通过单独的类作为静态常量使用。是的,对于 DB2 数据库 jdbc:db2://dbURL:port#/databaseName,dbURL 的格式是正确的

包rpTool;

/* * 类目的:* 建立与服务器的连接,同时正确地从外部位置加载用户信息,在本例中为“config.properties”文件。* 这个类也可以关闭任何已建立的连接。*/

公共类 DConnection { 私有字符串 jdbcDriver; // 驱动程序名称 private String dbUrl; // 数据库 URL 私有字符串 userID; // 数据库访问用户名 private String 密码;// 访问数据库的用户密码

/*
 * loads database and user credentials to access data in code
 * from a separate .properties file and assigns those values to
 * 'jdbcDriver', 'dbUrl', 'userID', 'password'.
 */

public void loadProperties(){


   try {

    // database credentials from static constants in another class
            // This is only temporary
    System.out.println("getting jdbc url constants: " );
    jdbcDriver = EmdDBConstants.dbDriver;
    dbUrl =  EmdDBConstants.databaseUrl;
    userID =  EmdDBConstants.userID;
    password =  EmdDBConstants.password;

    System.out.println(jdbcDriver);
    System.out.println("dbUrl = " + dbUrl);


    //createConnection();
   } 
   catch (IOException ex) {
    ex.printStackTrace();
       }        
    }

/*
 * Establishes a connection to the database and returns it
 * so that it can be used to verify a vailid connection
 * has been made.
 */
public Connection getConnection(){

   try{ 
    // the driver allows you to query the database with Java.
    // forName dynamically loads the class for you
    System.out.println("getConnection:    getting jdbc url constant: " );
    Class.forName(jdbcDriver);


    return DriverManager.getConnection(dbUrl, userID, password);
   }
   catch(SQLException ex){

    System.out.println("SQLException: " + ex.getMessage());

    System.out.println("VendorError: " + ex.getErrorCode());
    return null;
   }
   catch(ClassNotFoundException e){
    // logs if the driver can't be found
    e.printStackTrace();
    return null;
   }
}// end getConnection

// close a connection
public void closeConnection(Connection con){
    try {

        if(con == null){
            System.out.println("No Connection...");
        }
        else{
            con = getConnection();
            con.close();
        }
    }
    catch (SQLException e) {

        e.printStackTrace();
    }
}// end close connection

}

这是建立连接并从网页执行用户选择的命令的 Java servlet:

公共类 ProcessQuery 扩展 HttpServlet { private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ProcessQuery() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

    //String sqlStatement = request.getParameter("sqlStatement");
    String url = "/test.jsp";
    String sqlStatement = request.getParameter("querySelector");
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    String sqlResult;
    System.out.println("ProcessQuery doPost entry: " );

    try 
    {

        DConnection conn = new DConnection();
        conn.loadProperties();
        Connection connect = null;
        connect = conn.getConnection();
        if(connect != null)
        {

            System.out.println("good connection...");

            Statement statement = connect.createStatement();

            // parse the SQL string
            if (sqlStatement != null)
            {
                  sqlStatement = sqlStatement.trim();
                  if(sqlStatement.length() >= 6)
                  {

                      String sqlType = sqlStatement.substring(0, 6);

                      if(sqlType.equalsIgnoreCase("select")){

                          // create the HTML for the result set
                          ResultSet resultSet = statement.executeQuery(sqlStatement);
                          sqlResult = SQLUtil.getHtmlTable(resultSet);
                          resultSet.close();
                          request.setAttribute("variable", sqlResult);
                          dispatcher.forward(request, response);
                      } // end if 
                      else
                      {

                          int i = statement. executeUpdate(sqlStatement);
                          if(i==0){ // a DDL statement
                              sqlResult = "The statement executed successfully.";
                              request.setAttribute("variable", sqlResult);
                              dispatcher.forward(request, response);
                          } // end if
                          else{ // an INSERT, UPDATE, or DELETE statement
                              sqlResult = "The statement executed successfully.<br" +
                                      + i + " row(s) affected.";
                              request.setAttribute("variable", sqlResult);
                              dispatcher.forward(request, response);
                          } // end else
                      } // end else
                } // end if
                else
                {
                  request.setAttribute("variable", "sqlStatement is null");
                  dispatcher.forward(request, response);
                }   // end else
                    statement.close();
                    connect.close();
            }// end if
        }// end if
        else{
            System.out.println("no connection...");
            request.setAttribute("variable", "no connection...");
            dispatcher.forward(request, response);              
        }
    }// end try
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }// end catch   
}// end doPost

}//结束类

请帮忙!

4

0 回答 0