我正在使用带有Java的MVC结构开发一个Web应用程序,它将从下拉菜单中的网页执行用户选择的查询,并将显示代码所连接的DB2数据库中的数据。
我已将代码设置为能够成功完成此操作;但是,只有我第一次建立连接。如果我要回浏览器并选择另一个命令,则网页不显示任何内容,我在Eclipse的控制台中收到以下错误:
SQLException:[jcc] [t4] [2034] [11148] [4.13.127]由于导致会话重新分配的分发协议错误,执行失败。 检测到DRDA数据流语法错误。原因:0x2110。 ERRORCODE = -4499,SQLSTATE = 58009 VendorError:-4499
这是代码: 我还没有实现从文件加载信息,但所有信息都可以通过单独的类作为静态常量获得。是的,dbURL的格式正确,适用于DB2数据库jdbc:db2:// dbURL:port#/ databaseName
包rpTool;
/ * *课程目的: *正确建立与服务器的连接 *从外部位置加载用户信息 *这种情况是'config.properties'文件。 *此类还可以关闭已建立的任何连接。 * /
公共类DConnection { private String jdbcDriver; //驱动程序名称 private String dbUrl; //数据库URL private String userID; //数据库访问的用户名 私有字符串密码; //数据库访问的用户密码
/*
* 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
} //结束课
请帮忙!