我正在阅读一篇教程,试图用MySql构建一个非常简单的Web应用程序。我的IDE是NetBeans 8.0。使用我的index.jsp欢迎文件,用户应该能够在文本字段中输入查询,然后结果表将显示在下面。或者,将显示说明该语句会影响多少行的文本。当我单击“执行”按钮时,没有任何反应。
我已将MySQL驱动程序添加到我的库中,并在“服务”选项卡中注册了MySQL服务器。
我能够使用Netbeans的“执行命令”执行查询,但我的Java Servlet似乎没有做任何事情。
我的servlet:
package com.test;
import com.john.utils.SQLUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author me
*/
public class SqlServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sqlStatement = request.getParameter("sqlStatement");
String sqlResult = "";
try
{
// load DB driver
Class.forName("com.mysql.jdbc.Driver");
// get connection
String dbURL = "jdbc:mysql://localhost:3306/tester";
String username = "root";
String password = "broken1";
Connection connection = DriverManager.getConnection(dbURL, username, password);
// create statement
Statement statement = connection.createStatement();
// parse SQL string
sqlStatement = sqlStatement.trim();
if(sqlStatement.length() >= 6)
{
String sqlType = sqlStatement.substring(0, 6);
if(sqlType.equalsIgnoreCase("select"))
{
ResultSet resultSet = statement.executeQuery(sqlStatement);
sqlResult = SQLUtil.getHtmlTable(resultSet);
resultSet.close();
} else {
int i = statement.executeUpdate(sqlStatement);
if(i == 0) {
sqlResult = "<p>The statement executed successfully.</p>";
} else {
sqlResult = "<p>The statement executed successfully.</p>"
+ i + " row (s) affected.</p>";
}
}
}
statement.close();
connection.close();
} catch (ClassNotFoundException e){
sqlResult = "<p>Error loading th edatabase driver: </br>"
+ e.getMessage() + "</p>";
} catch (SQLException e){
sqlResult = "<p>Error executing the SQL statement: <br>"
+ e.getMessage() + "</p>";
}
HttpSession session = request.getSession();
session.setAttribute("sqlResult", sqlResult);
session.setAttribute("sqlStatement", sqlStatement);
String url = "/index.jsp";
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我的JSP:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Query Master</title>
</head>
<body>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${sqlStatement == null}">
<c:set var="sqlStatement" value="select * from user" />
</c:if>
<h1>Input a SQL Statement</h1>
<form action="SqlServlet" method="POST">
<textarea name="sqlStatement" cols="60" rows="8">${sqlStatement}</textarea>
<input type="submit" value="Execute"
</form>
<p>SQL Results</p>
${sqlResult}
</body>
</html>
答案 0 :(得分:0)
您忘记在sql查询中包含架构。写下你的查询:
SELECT * FROM Tester.User
还要尝试在输出窗口中查看Netbeans中的服务器日志。通常你可以找到他们的错误。