0

我使用 jsp 中的表单来获取用户输入以更新数据库。我刚开始学习jsp和java。我非常感谢您提供的任何帮助。

这是表格的报价:

<FORM ACTION="display.jsp" method ="get">
  <label>Semester:</label>
    <select name="semester">
    <option selected="selected">Semester Select</option>
              <option value='Spring'>Spring 2007 </option>
              <option value='January Intersession'>January Intersession 2007 </option>
              <option value='Fall'>Fall 2006 </option>
              <option value='Summer ALL'>Summer ALL 2006 </option>
              <option value='Summer00'>Summer00 2006 (06/01/2006 to 08/17/2006)</option>
              <option value='Summer02'>Summer02 2006 (07/10/2006 to 08/17/2006)</option>
              <option value='Summer01'>Summer01 2006 (06/01/2006 to 07/06/2006)</option>
    </select>
<br/><br/>
<label>Start Date:</label>
  <INPUT TYPE="TEXT" NAME="startDate"><br/><br/>
<label>End Date:</label>
  <INPUT TYPE="TEXT" NAME="endDate"><br/><br/>
  <INPUT TYPE="SUBMIT" value="Update Schedule" name="submitButton"> <!-- Press this button to submit form -->
</FORM>

这是jsp的代码:

<%@ page import="java.sql.*" %>
<%
   Connection conn = null;
   try
   {
       // 1. Load the driver
       String driver = "oracle.jdbc.OracleDriver";
       Class.forName(driver);

      // 2. Define the connection URL
      String url = "..."; //orcl is the SID
      String myusername = "..."; // Your DB login ID
      String mypassword = "..."; //Your Db pass

      // 3. Establish the connection
      conn = DriverManager.getConnection(url, myusername,mypassword);
      conn.setAutoCommit(false);

      // 4. Create a statement object
      Statement stmt = conn.createStatement();
      PreparedStatement ps;

      // 5. Execute a query
      String startDate = request.getParameter("startDate");
      String endDate = request.getParameter("endDate");
      String semester = request.getParameter("semester");
      String updateQuery = "UPDATE semester_schedule_reader SET start_date = ?, end_date = ? WHERE semester = ?";
      ps = conn.prepareStatement(updateQuery);
      ps.setString(1, startDate);
      ps.setString(2, endDate);
      ps.setString(3, semester);
      ps.executeUpdate();
      conn.commit();


      ResultSet rs = stmt.executeQuery("SELECT SEMESTER_ID, START_DATE, END_DATE, SEMESTER_NAME, SEMESTER FROM SEMESTER_SCHEDULE_READER");




      //Print start of table and column headers
      out.println("<table width=600 border=1>");
      out.println("<tr><th>Semester ID</th><th>Start Date</th><th>End Date</th><th>Semester Name</th><th>Semester</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_ID") + "</TD>");
         out.println("<TD>" + rs.getString("START_DATE") + "</TD>");
         out.println("<TD>" + rs.getString("END_DATE") + "</TD>");
         out.println("<TD>" + rs.getString("SEMESTER_NAME") + "</TD>");
         out.println("<TD>" + rs.getString("SEMESTER") + "</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) {}
      }

      if(ps != null)
      {
          ps.close();
      }
   }
%>
4

0 回答 0