2016-02-14 61 views
1

我不確定問題出在我的代碼中。我試圖在按下提交按鈕時將值添加到我的數據庫中。我還添加了一個println命令來打印出所請求的參數,但即使這個參數爲空。請幫忙。JSP頁面不會將值添加到我的數據庫中

<%@page import="java.sql.*"%> 
<% Class.forName("com.mysql.jdbc.Driver");%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Inserting data Check</title> 
    </head> 
    <body> 
     <h1>Checking data insertion</h1> 
     <%!public class Actor { 

       String URL = "jdbc:mysql://localhost:3306/sakila"; 
       String USERNAME = "root"; 
       String PASSWORD = "root"; 

       Connection connection = null; 
       PreparedStatement insertActors = null; 
       ResultSet resultSet = null; 

       public Actor() { 

        try { 
         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); 
         insertActors = connection.prepareStatement(
           "INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)" 
           + " VALUES (?, ?, ?, ?, ?)"); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
       } 

       public int setActors(String id, String last, String first, String address, String city) { 
        int result = 0; 
        try { 
         insertActors.setString(1, id); 
         insertActors.setString(2, last); 
         insertActors.setString(3, first); 
         insertActors.setString(4, address); 
         insertActors.setString(5, city); 
         result = insertActors.executeUpdate(); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
        return result; 
       } 
      } 
     %> 
     <% 
      int result = 0; 

      String id = new String(); 
      String last = new String(); 
      String first = new String(); 
      String add = new String(); 
      String c = new String(); 

      if (request.getParameter("id") != null) { 
       id = request.getParameter(id); 
       System.out.println("id" + id); 
      } 
      if (request.getParameter("last") != null) { 
       last = request.getParameter(last); 
       System.out.println("id" + last); 
      } 
      if (request.getParameter("first") != null) { 
       first = request.getParameter(first); 
       System.out.println("id" + first); 
      } 
      if (request.getParameter("address") != null) { 
       add = request.getParameter(add); 
       System.out.println("id" + add); 
      } 
      if (request.getParameter("city") != null) { 
       c = request.getParameter(c); 
       System.out.println("id" + c); 
      } 

     %> 
     <form name="myForm" action="insert.jsp" method="POST"> 
      <table> 
       <tbody> 
        <tr> 
         <td>Id</td> 
         <td><input type="text" name="id" value="" size="50" /></td> 
        </tr> 
        <tr> 
         <td>Last Name</td> 
         <td><input type="text" name="last" value="" size="50" /></td> 
        </tr> 
        <tr> 
         <td>First Name</td> 
         <td><input type="text" name="first" value="" size="50" /></td> 
        </tr> 
        <tr> 
         <td>Address</td> 
         <td><input type="text" name="address" value="" size="50" /></td> 
        </tr> 
        <tr> 
         <td>City</td> 
         <td><input type="text" name="city" value="" size="50" /></td> 
        </tr> 
       </tbody> 
      </table>  
      <input type="reset" value="Reset" name="reset" /> 
      <input type="submit" value="Submit" name="submit" /> 
     </form>   
    </body> 
</html> 
+0

你有公共法「setActors」,但你永遠不調用它。 –

+0

@KenBekov謝謝你的朋友。它現在像一種魅力。你是一個很好的幫助。 – user3285009

回答

0

看來,這是你的問題(至少當您檢查值):

if (request.getParameter("id") != null) { 
      id = request.getParameter(id); // change to request.getParameter("id"); 
      System.out.println("id" + id); 
     } 
     if (request.getParameter("last") != null) { 
      last = request.getParameter(last); // change to "last" 
      System.out.println("id" + last); 
     } 
     if (request.getParameter("first") != null) { 
      first = request.getParameter(first); // change to "first" 
      System.out.println("id" + first); 
     } 
     if (request.getParameter("address") != null) { 
      add = request.getParameter(add); // change to "address" 
      System.out.println("id" + add); 
     } 
     if (request.getParameter("city") != null) { 
      c = request.getParameter(c); // change to "city" 
      System.out.println("id" + c); 
     } 
+0

非常感謝您的快速回復。我做了必要的修改,現在System.out.println()向我顯示了我輸入的所有數據。但是,當我檢查數據庫時,它仍然是空的。還有什麼我失蹤的? – user3285009

+0

Maby嘗試使用connection.commit();然後insertActors.close(); connection.close()時;只是爲了看看它是否有幫助。 – MaxG

+0

另外,添加println(result);在executeUpdate()之後;什麼是價值? – MaxG

相關問題