import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Calendar; import java.sql.*; public class EditSchedule2 extends HttpServlet{ protected Connection conn = null; public void init() throws ServletException{ String url = "jdbc:mysql://localhost/servletschedule"; String user = "scheduleuser"; String password = "schedulepass"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, user, password); }catch (ClassNotFoundException e){ log("ClassNotFoundException:" + e.getMessage()); }catch (SQLException e){ log("SQLException:" + e.getMessage()); }catch (Exception e){ log("Exception:" + e.getMessage()); } } public void destory(){ try{ if (conn != null){ conn.close(); } }catch (SQLException e){ log("SQLException:" + e.getMessage()); } } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ res.setContentType("text/html;charset=Shift_Jis"); PrintWriter out = res.getWriter(); int year = -1; int month = -1; int day = -1; int currentscheduleid; String currentStartTime = ""; String currentEndTime = ""; String currentSchedule = ""; String currentMemo = ""; String param = req.getParameter("ID"); if (param == null || param.length() == 0){ currentscheduleid = -1; }else{ try{ currentscheduleid = Integer.parseInt(param); }catch (NumberFormatException e){ currentscheduleid = -1; } } /* パラメータが不正な場合はトップページへリダイレクト */ if (currentscheduleid == -1){ res.sendRedirect("/schedule/top.html"); } /* ユーザー情報を取り出す */ HttpSession session = req.getSession(false); String username = (String)session.getAttribute("username"); String tmpuserid = (String)session.getAttribute("userid"); int userid = 0; if (tmpuserid != null){ userid = Integer.parseInt(tmpuserid); } try { String sql = "SELECT * FROM schedule WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, currentscheduleid); ResultSet rs = pstmt.executeQuery(); rs.next(); String scheduledate = rs.getString("scheduledate"); String yearStr = scheduledate.substring(0, 4); String monthStr = scheduledate.substring(5, 7); String dayStr = scheduledate.substring(8, 10); year = Integer.parseInt(yearStr); month = Integer.parseInt(monthStr) - 1; day = Integer.parseInt(dayStr); currentStartTime = rs.getString("starttime"); currentEndTime = rs.getString("endtime"); currentSchedule = rs.getString("schedule"); currentMemo = rs.getString("schedulememo"); rs.close(); pstmt.close(); }catch (SQLException e){ log("SQLException:" + e.getMessage()); } StringBuffer sb = new StringBuffer(); sb.append(""); sb.append(""); sb.append("
"); sb.append(""); sb.append(""); sb.append(username); sb.append("さんのスケジュールです"); sb.append("
"); sb.append(""); sb.append("スケジュールの変更 "); sb.append("[スケジュール表示に戻る]"); sb.append("
"); String[] scheduleArray = new String[49]; int[] widthArray = new int[49]; for (int i = 0 ; i < 49 ; i++){ scheduleArray[i] = ""; widthArray[i] = 0; } try { String sql = "SELECT * FROM schedule WHERE userid = ? and scheduledate = ? ORDER BY starttime"; PreparedStatement pstmt = conn.prepareStatement(sql); String startDateStr = year + "-" + (month + 1) + "-" + day; pstmt.setInt(1, userid); pstmt.setString(2, startDateStr); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ int id = rs.getInt("id"); String starttime = rs.getString("starttime"); String endtime = rs.getString("endtime"); String schedule = rs.getString("schedule"); if (starttime == null || endtime == null){ widthArray[0] = 1; StringBuffer sbSchedule = new StringBuffer(); sbSchedule.append(""); sbSchedule.append(schedule); sbSchedule.append(""); scheduleArray[0] = scheduleArray[0] + (new String(sbSchedule)) + "時刻 | 予定 | ||
未定 | "); sb.append(""); if (widthArray[0] == 1){ sb.append(scheduleArray[0]); } sb.append(" | ||
"); sb.append(i / 2); sb.append(":00 | "); }else{ sb.append("|||
"); } if (widthArray[i] != 0){ if (widthArray[i] != -1){ sb.append(" | "); sb.append(scheduleArray[i]); } }else{ if (i % 2 == 1){ sb.append(" | "); }else{ sb.append(" | "); } } sb.append(" |