【问题标题】:I cannot get the jsp to execute a SQL update statement我无法让 jsp 执行 SQL 更新语句
【发布时间】:2014-06-05 21:17:13
【问题描述】:

我正在尝试让 jsp 从“update.jsp”上的表单中获取用户输入并运行更新语句来更新数据库条目并在“display.jsp”上显示结果。 我使用 eclipse 作为我的 IDE,使用 oracle 作为我的数据库服务器
以前,我已经成功执行了一个 select 语句(作为测试)。所以,我确信与 DB 的连接没有问题
这是用于“update.jsp”(表单)的代码

<FORM ACTION="display.jsp">
    <label>Semester:
        <select name="semester_form">
            <option selected="semester">Semester Select</option>
            <%//public String input_semester=semester; %>

            <option value='Spring'>Springy</option>
            <option value='January Intersession'>January Intersession</option>
            <option value='Fall'>Fall</option>
            <option value='Summer ALL'>Summer ALL</option>
            <option value='Summer00'>Summer00</option>
            <option value='Summer02'>Summer02</option>
            <option value='Summer01'>Summer01</option>

这是我在创建数据库时使用的代码

CREATE TABLE SEMESTER_SCHEDULE_READER ( 
    SEMESTER_ID     VARCHAR2(2) NULL,
    START_DATE      DATE NULL,
    END_DATE        DATE NULL,
    SEMESTER_NAME   VARCHAR2(25) NULL,
    SEMESTER        VARCHAR2(8) NULL 
)

现在这是正在使用的“display.JSP”代码

Connection conn = null;
try {
    // 1. Load the driver
    Class.forName(driver);

    // myusername and mypassword are declared in a file called username.jsp

    // 2. Define the connection URL
    // 3. Establish the connection
    conn = DriverManager.getConnection(
            url,
            myusername,
            mypassword);


    // 4. Create a statement object
    Statement stmt = conn.createStatement();
    PreparedStatement pstatement = null;
    int updateQuery = 0;

    // 5. Execute a query

    String semester = request.getParameter("semester_form");
    session.putValue("semester_form", semester);
    String start_date = request.getParameter("startDate_form");
    String end_date = request.getParameter("endDate_form");
    //ResultSet rs = stmt.executeQuery("select * from SEMESTER_SCHEDULE_READER  where semeStER_NAME='"+semester+"'");
    conn.setAutoCommit(false);

    ResultSet rs = stmt.executeQuery("UPDATE SEMESTER_SCHEDULE_READER SET START_DATE= to_date('" + start_date + "', 'DD-MON-YY') WHERE SEMESTER='" + semester + "'");

    conn.commit();


    // printing a welcom message 
    out.println("<H1>You have successfully updated the semester schedule</H1><br>");

    //Print start of table and column headers
    out.println("<TABLE width = 600 CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"2\">");
    out.println("<TR><TH>Semester Name</TH><TH>Start date</TH><TH>End date</TH></TR>");

    // 6. Process result
    //column names and Data types has to match Db columns
    while (rs.next()) {
        out.println("<TR>");
        out.println("<TD>" + rs.getString("SEMESTER_NAME") + "</TD>");
        out.println("<TD>" + rs.getString("START_DATE") + "</TD>");
        out.println("<TD>" + rs.getString("END_DATE") + "</TD>");


        out.println("</TR>");
    }

    out.println("</TABLE>");
} catch (SQLException e) {
    // Do exception catch such as if connection is not made or 
    // query is not set up properly
    out.println("SQLException: " + e.getMessage() + "<BR>");
    while ((e = e.getNextException()) != null)
        out.println(e.getMessage() + "<BR>");
} catch (ClassNotFoundException e) {
    out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
} finally {
    // 7. Close connection; Clean up resources
    if (conn != null) {
        try {
            conn.close();
        } catch (Exception ignored) {
        }
    }
}
 %>

【问题讨论】:

  • 您永远不应该在 JSP 页面中运行这样的 Java 代码。那是非常糟糕的编码风格。并且每次显示 JSP 页面时打开一个新连接的可扩展性不是很高。将 SQL 代码移动到某个后端服务(或 servlet)并使用连接池来管理您的数据库连接。

标签: java sql jsp web-applications oracle11g


【解决方案1】:
ResultSet rs = stmt.executeQuery(...)

应该替换为

int results = stmt.executeUpdate(...)

executeQuery 在您执行查询(选择)时使用,但executeUpdate 在您执行将更改数据库的操作(插入、更新、删除)时使用。

【讨论】:

  • 感谢您的快速响应。不幸的是,当我尝试键入 stmt.executeUpdate() 时,我收到语法错误 Multiple annotations found at this line: - Type mismatch: cannot convert from int to ResultSet
  • @EmadMahdy executeUpdate() 不会返回 ResultSet(想想看,您是在更新表,而不是查询表),请检查 Javadoc 以了解它实际为您提供的内容。
【解决方案2】:

试试stmt.executeUpdate();

【讨论】:

  • 感谢您的快速响应。不幸的是,当我尝试输入 'stmt.executeUpdate()' 时,我收到一个语法错误 Multiple annotations found at this line: - Type mismatch: cannot convert from int to ResultSet – @niiraj874u
猜你喜欢
  • 1970-01-01
  • 2021-12-02
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 2022-11-09
相关资源
最近更新 更多