Question

I am trying to make a jsp take user input from a form on "update.jsp" and run an update statement to update the database entry and display the result on "display.jsp". am using eclipse as my IDE, and oracle as my database server
previously, i have successfully executed a select statement (as a test). so, am sure it is not something wrong with the connection to the DB
This is the code am using for "update.jsp" (the form)

<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>

And here is the code that i used in creating the database

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 
)

and now this is the "display.JSP" code am using

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) {
        }
    }
}
 %>
Was it helpful?

Solution

ResultSet rs = stmt.executeQuery(...)

should be replaced with

int results = stmt.executeUpdate(...)

executeQuery is used whenever you are executing a query (select) but executeUpdate is used whenever you are executing something that will change the DB (insert, update, delete).

OTHER TIPS

Try with stmt.executeUpdate();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top