データベースを更新するために、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();
}
}
%>