import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Calendar; import java.sql.*; public class NewSchedule4 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; int month; int day; String param = req.getParameter("YEAR"); if (param == null || param.length() == 0){ year = -999; }else{ try{ year = Integer.parseInt(param); }catch (NumberFormatException e){ year = -999; } } param = req.getParameter("MONTH"); if (param == null || param.length() == 0){ month = -999; }else{ try{ month = Integer.parseInt(param); }catch (NumberFormatException e){ month = -999; } } param = req.getParameter("DAY"); if (param == null || param.length() == 0){ day = -999; }else{ try{ day = Integer.parseInt(param); }catch (NumberFormatException e){ day = -999; } } /* パラメータが指定されていない場合は本日の日付を設定 */ if (year == -999 || month == -999 || day == -999){ Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); day = calendar.get(Calendar.DATE); } StringBuffer sb = new StringBuffer(); sb.append(""); sb.append(""); 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, 1); pstmt.setString(2, startDateStr); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ String starttime = rs.getString("starttime"); String endtime = rs.getString("endtime"); String schedule = rs.getString("schedule"); if (starttime == null || endtime == null){ widthArray[0] = 1; scheduleArray[0] = scheduleArray[0] + schedule + "時刻 | 予定 | ||
未定 | "); 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(" |