2012-10-02 79 views
0

我正在創建一個java servlet並嘗試創建一個新的「實驗室」。但是,它沒有提交到數據庫。在頁面加載時查看控制檯將打印JDBC Driver Loaded,然後單擊「提交」時,將在控制檯中輸出'got connection'。當我檢查數據庫提交Servlet不發送表格數據到表

表我的信息處理不當嘗試提交是

lab - id, capacity, day, time, room, subject_id, user_id 

這裏是我的servlet代碼

import java.io.IOException; 


import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

    /** 
    * Servlet implementation class CreateLab 
    */ 
    @WebServlet("/CreateLab") 
    public class CreateLab extends HttpServlet { 
     private static final long serialVersionUID = 1L; 

     /** 
     * @see HttpServlet#HttpServlet() 
     */ 
     public CreateLab() { 
      super(); 
      // TODO Auto-generated constructor stub 
     } 

     private int capacity; 
     private String day = ""; 
     private String time = ""; 
     private String room = ""; 
     private int subject_id; 
     private int user_id; 

     public void init() { 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection con = 
       DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); 
      System.out.println("JDBC driver loaded"); 
      } 
      catch (ClassNotFoundException e) { 
      System.out.println(e.toString()); 
      } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

     /**Process the HTTP Get request*/ 
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
     ServletException,IOException { 
      sendPageHeader(response); 
      sendRegistrationForm(request, response, false); 
      sendPageFooter(response); 
     } 

     /**Process the HTTP Post request*/ 
     public void doPost(HttpServletRequest request, 
      HttpServletResponse response) 
      throws ServletException, IOException { 
      sendPageHeader(response); 


      day = request.getParameter("day"); 
      time = request.getParameter("time"); 
      room = request.getParameter("room"); 



      boolean error = false; 
      String message = null; 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection con = 
       DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); 
      System.out.println("got connection"); 
      System.out.println(capacity); 
      System.out.println(day); 
      System.out.println(time); 
      System.out.println(room); 
      System.out.println(subject_id); 
      System.out.println(user_id); 
      Statement s = con.createStatement(); 

      String sql = "SELECT id FROM user" + 
        " WHERE id='" + user_id + "'"; 
      ResultSet rs = s.executeQuery(sql); 
      if (rs.next()) { 
       rs.close(); 
       sql = "INSERT INTO lab" + 
         " (capacity, day, time, room, subject_id, user_id)" + 
         " VALUES" + 
         " ('" + capacity + "'," + 
         " '" + day + "'," + 
         " '" + time + "'," + 
         " '" + room + "','" + subject_id + 
         "'," + user_id + "')"; 


       int i = s.executeUpdate(sql); 
       if (i==1) { 
       message = "Successfully a new lab class."; 
       } 
      } 
       s.close(); 
       con.close(); 
      } 
      catch (SQLException e) { 
       message = "Error." + e.toString(); 
       error = true; 
      } 
      catch (Exception e) { 
       message = "Error." + e.toString(); 
       error = true; 
      } 
      if (message!=null) { 
       PrintWriter out = response.getWriter(); 
       out.println("<B>" + message + "</B><BR>"); 
       out.println("<HR><BR>"); 
      } 
      if (error==true) 
       sendRegistrationForm(request, response, true); 
      else 
       sendRegistrationForm(request, response, false); 
      sendPageFooter(response); 
      } 

      /** 
      * Send the HTML page header, including the title 
      * and the <BODY> tag 
      */ 
      private void sendPageHeader(HttpServletResponse response) 
      throws ServletException, IOException { 
      response.setContentType("text/html"); 
      PrintWriter out = response.getWriter(); 
      out.println("<HTML>"); 
      out.println("<HEAD>"); 
      out.println("<TITLE>Create Lab Page</TITLE>"); 
      out.println("</HEAD>"); 
      out.println("<BODY>"); 
      out.println("<CENTER>"); 
      } 

      /** 
      * Send the HTML page footer, i.e. the </BODY> 
      * and the </HTML> 
      */ 
      private void sendPageFooter(HttpServletResponse response) 
      throws ServletException, IOException { 
      PrintWriter out = response.getWriter(); 
      out.println("</CENTER>"); 
      out.println("</BODY>"); 
      out.println("</HTML>"); 
      } 
      /**Send the form where the user can type in 
      * the details for a new user 
      */ 
      private void sendRegistrationForm(HttpServletRequest request, 
      HttpServletResponse response, boolean displayPreviousValues) 
      throws ServletException, IOException { 

      PrintWriter out = response.getWriter(); 
      out.println("<BR><H2>Create A Lab</H2>"); 
      out.println("<BR>Please enter the lab details."); 
      out.println("<BR>"); 
      out.println("<BR><FORM METHOD=POST>"); 
      out.println("<TABLE>"); 
      out.println("<TR>"); 
      out.println("<TD>Class Capacity</TD>"); 
      out.print("<TD><INPUT TYPE=TEXT Name=capacity"); 

      if (displayPreviousValues) 
       out.print(" VALUE=\"" + capacity + "\""); 

      out.println("></TD>"); 
      out.println("</TR>"); 
      out.println("<TR>"); 
      out.println("<TD>Day</TD>"); 
      out.print("<TD><INPUT TYPE=TEXT Name=day"); 

      if (displayPreviousValues) 
       out.print(" VALUE=\"" + day + "\""); 

      out.println("></TD>"); 
      out.println("</TR>"); 
      out.println("<TR>"); 
      out.println("<TD>Time</TD>"); 
      out.print("<TD><INPUT TYPE=TEXT Name=time"); 

      if (displayPreviousValues) 
       out.print(" VALUE=\"" + time + "\""); 

      out.println("></TD>"); 
      out.println("</TR>"); 
      out.println("<TR>"); 
      out.println("<TD>Room</TD>"); 
      out.print("<TD><INPUT TYPE=TEXT Name=room"); 
      if (displayPreviousValues) 
       out.print(" VALUE=\"" + room + "\""); 
      out.println("></TD>"); 
      out.println("</TR>"); 
      out.println("<TR>"); 
      out.println("<TD>subject_id</TD>"); 
      out.print("<TD><INPUT TYPE=TEXT Name=subject_id"); 
      if (displayPreviousValues) 
       out.print(" VALUE=\"" + subject_id + "\""); 
      out.println("></TD>"); 
      out.println("</TR>"); 
      out.println("<TR>"); 
      out.println("<TD>user_id</TD>"); 
      out.print("<TD><INPUT TYPE=TEXT Name=user_id"); 
      out.println("></TD>"); 
      out.println("</TR>"); 

      if (displayPreviousValues) 
      out.print(" VALUE=\"" + user_id + "\""); 
      out.println("</TD>"); 
      out.println("</TR>"); 

      out.println("<TR>"); 
      out.println("<TD><INPUT TYPE=RESET></TD>"); 
      out.println("<TD><INPUT TYPE=SUBMIT></TD>"); 
      out.println("</TR>"); 
      out.println("</TABLE>"); 
      out.println("</FORM>"); 
      out.println("<BR>"); 
      out.println("<BR>"); 
      } 
      } 
+0

這可能是的Cuz在結果集中theres沒什麼。在執行選擇查詢並檢查後,嘗試打印這些內容。 – PermGenError

+0

當我點擊提交後打印出來的所有內容時,容量,user_id和subject_id都沒有被抓取。我該如何補救? – user1393064

回答

-1
import java.io.IOException; 


import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class CreateLab 
*/ 
@WebServlet("/CreateLab") 
public class CreateLab extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public CreateLab() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    int capacity; 
    String day = ""; 
    String time = ""; 
    String room = ""; 
    int subject_id; 
    int user_id; 

    public void init() { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = 
      DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); 
     System.out.println("JDBC driver loaded"); 
     } 
     catch (ClassNotFoundException e) { 
     System.out.println(e.toString()); 
     } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    /**Process the HTTP Get request*/ 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
    ServletException,IOException { 
     sendPageHeader(response); 
     sendRegistrationForm(request, response, false); 
     sendPageFooter(response); 
    } 

    /**Process the HTTP Post request*/ 
    public void doPost(HttpServletRequest request, 
     HttpServletResponse response) 
     throws ServletException, IOException { 
     sendPageHeader(response); 

     capacity = Integer.parseInt(request.getParameter("capacity")); 
     day = request.getParameter("day"); 
     time = request.getParameter("time"); 
     room = request.getParameter("room"); 
     user_id = Integer.parseInt(request.getParameter("user_id")); 
     subject_id = Integer.parseInt(request.getParameter("subject_id")); 


     boolean error = false; 
     String message = null; 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = 
      DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); 
     System.out.println("got connection"); 
     System.out.println(capacity); 
     System.out.println(day); 
     System.out.println(time); 
     System.out.println(room); 
     System.out.println(subject_id); 
     System.out.println(user_id); 
     Statement s = con.createStatement(); 

     String sql = "SELECT id FROM user" + 
       " WHERE id='" + user_id + "'"; 
     ResultSet rs = s.executeQuery(sql); 
     if (rs.next()) { 
      rs.close(); 
      sql = "INSERT INTO lab" + 
        " (capacity, day, time, room, subject_id, user_id)" + 
        " VALUES" + 
        " ('" + capacity + "'," + 
        " '" + day + "'," + 
        " '" + time + "'," + 
        " '" + room + "','" + subject_id + "','" + user_id + "')"; 

      System.out.println(sql); 
      int i = s.executeUpdate(sql); 
      if (i==1) { 
      message = "Successfully a new lab class."; 
      } 
     } 
      s.close(); 
      con.close(); 
     } 
     catch (SQLException e) { 
      message = "Error." + e.toString(); 
      error = true; 
     } 
     catch (Exception e) { 
      message = "Error." + e.toString(); 
      error = true; 
     } 
     if (message!=null) { 
      PrintWriter out = response.getWriter(); 
      out.println("<B>" + message + "</B><BR>"); 
      out.println("<HR><BR>"); 
     } 
     if (error==true) 
      sendRegistrationForm(request, response, true); 
     else 
      sendRegistrationForm(request, response, false); 
     sendPageFooter(response); 
     } 

     /** 
     * Send the HTML page header, including the title 
     * and the <BODY> tag 
     */ 
     private void sendPageHeader(HttpServletResponse response) 
     throws ServletException, IOException { 
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<HTML>"); 
     out.println("<HEAD>"); 
     out.println("<TITLE>Create Lab Page</TITLE>"); 
     out.println("</HEAD>"); 
     out.println("<BODY>"); 
     out.println("<CENTER>"); 
     } 

     /** 
     * Send the HTML page footer, i.e. the </BODY> 
     * and the </HTML> 
     */ 
     private void sendPageFooter(HttpServletResponse response) 
     throws ServletException, IOException { 
     PrintWriter out = response.getWriter(); 
     out.println("</CENTER>"); 
     out.println("</BODY>"); 
     out.println("</HTML>"); 
     } 
     /**Send the form where the user can type in 
     * the details for a new user 
     */ 
     private void sendRegistrationForm(HttpServletRequest request, 
     HttpServletResponse response, boolean displayPreviousValues) 
     throws ServletException, IOException { 

     PrintWriter out = response.getWriter(); 
     out.println("<BR><H2>Create A Lab</H2>"); 
     out.println("<BR>Please enter the lab details."); 
     out.println("<BR>"); 
     out.println("<BR><FORM METHOD=POST>"); 
     out.println("<TABLE>"); 
     out.println("<TR>"); 
     out.println("<TD>Class Capacity</TD>"); 
     out.print("<TD><INPUT TYPE=TEXT Name=capacity"); 

     if (displayPreviousValues) 
      out.print(" VALUE=\"" + capacity + "\""); 

     out.println("></TD>"); 
     out.println("</TR>"); 
     out.println("<TR>"); 
     out.println("<TD>Day</TD>"); 
     out.print("<TD><INPUT TYPE=TEXT Name=day"); 

     if (displayPreviousValues) 
      out.print(" VALUE=\"" + day + "\""); 

     out.println("></TD>"); 
     out.println("</TR>"); 
     out.println("<TR>"); 
     out.println("<TD>Time</TD>"); 
     out.print("<TD><INPUT TYPE=TEXT Name=time"); 

     if (displayPreviousValues) 
      out.print(" VALUE=\"" + time + "\""); 

     out.println("></TD>"); 
     out.println("</TR>"); 
     out.println("<TR>"); 
     out.println("<TD>Room</TD>"); 
     out.print("<TD><INPUT TYPE=TEXT Name=room"); 
     if (displayPreviousValues) 
      out.print(" VALUE=\"" + room + "\""); 
     out.println("></TD>"); 
     out.println("</TR>"); 
     out.println("<TR>"); 
     out.println("<TD>subject_id</TD>"); 
     out.print("<TD><INPUT TYPE=TEXT Name=subject_id"); 
     if (displayPreviousValues) 
      out.print(" VALUE=\"" + subject_id + "\""); 
     out.println("></TD>"); 
     out.println("</TR>"); 
     out.println("<TR>"); 
     out.println("<TD>user_id</TD>"); 
     out.print("<TD><INPUT TYPE=TEXT Name=user_id"); 
     out.println("></TD>"); 
     out.println("</TR>"); 

     if (displayPreviousValues) 
     out.print(" VALUE=\"" + user_id + "\""); 
     out.println("</TD>"); 
     out.println("</TR>"); 

     out.println("<TR>"); 
     out.println("<TD><INPUT TYPE=RESET></TD>"); 
     out.println("<TD><INPUT TYPE=SUBMIT></TD>"); 
     out.println("</TR>"); 
     out.println("</TABLE>"); 
     out.println("</FORM>"); 
     out.println("<BR>"); 
     out.println("<BR>"); 
     } 
     } 
+0

你應該添加你的評論,你已經做了什麼來解決你的問題 –

0

我注意到,你永遠不分配您的subject_id,而不是在聲明時將其初始化爲0。它看起來像你有一個表單域,所以也許你想從請求中提取它,就像你使用daytimeroom。我承認我也不明白實施的邏輯(只有在實驗室已經存在的情況下才創建實驗室),但這並不是那裏或那裏。

對你的代碼的幾點反饋。

  1. Servlet不是線程綁定的。一個servlet類的實例可以用來處理同一個URL的多個併發請求。因此,您需要考慮線程安全。您的代碼絕對是而不是線程安全,因爲您正在服務方法中使用實例變量(capacity,day,time等)。
  2. 在Java代碼中打印HTML實際上並不是現在這樣做的。至少,您應該始終轉發到JSP頁面來呈現視圖。更常見的是使用基於MVC或基於組件的Web框架之一(Spring,Struts,JSF等等)。
  3. 使用有效的HTML。例如,沒有像<input type=int ...>那樣的東西。見http://www.w3schools.com/html/html_forms.asp
+0

我更新了我的代碼,關於第2點 - 我只是試圖在我嘗試轉發到jsp呈現視圖之前使功能正常工作。我們目前正在做一個uni任務,需要使用jsp/servlets和java beans。在更改我的代碼以符合您的建議後,它仍然不會在user.id = lab.user_id時在數據庫中創建實驗室的新實例。 – user1393064

+0

同樣,您從不讀取請求中的user_id。你需要調用'user_id = Integer.parseInt(request.getParameter(「user_id」));'。 – pap

+0

我修好了,會在2秒內發佈答案 – user1393064