2015-08-27 222 views
1

我有像下面的HTML表單。我想將數據添加到數據庫表itemspoinfo。我想要完成如下功能;插入數據到MySQL數據庫

  • 如果給出的第一個藥物名稱和數量將這些數據添加到數據庫中。
  • 如果給出的前2個藥物名稱和數量將這些名稱和數量添加到數據庫中。 (如2 PONO沿各自的記錄和日期)

  • 由於這長達5個記錄必須能夠在記錄到數據庫

我無法找到一個解決方案請幫幫我。

我迄今所做的;

<% 
    Class.forName("com.mysql.jdbc.Driver"); 
    conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); 

    if(request.getParameter("send")!=null){ 
     String scom=request.getParameter("scompany"); 
     String porderno=request.getParameter("pono"); 
     String bdate=request.getParameter("date"); 

     String drug1=request.getParameter("d1"); 
     String qty1=request.getParameter("q1"); 
     String drug2=request.getParameter("d2"); 
     String qty2=request.getParameter("q2"); 
     String drug3=request.getParameter("d3"); 
     String qty3=request.getParameter("q3"); 
     String drug4=request.getParameter("d4"); 
     String qty4=request.getParameter("q4"); 
     String drug5=request.getParameter("d5"); 
     String qty5=request.getParameter("q5"); 


     //getting todaydate 
     Date date = new Date(); 
     Timestamp timestamp = new Timestamp(date.getTime()); 

     String sql = "INSERT INTO purchaseorderinfo SET Supplier ='"+scom+"', PONo='"+porderno+"', ExpectedDate='"+bdate+"', PODate='"+timestamp+"' "; 
     pst=conn.prepareStatement(sql); 



     if((scom!=null && scom.length()>0) 
      && (porderno!=null && porderno.length()>0) 
      && (bdate!=null && bdate.length()>0) 
      && (drug1!=null && drug1.length()>0) 
      && (qty1!=null && qty1.length()>0)){ 


      pst.execute(); 

      String sql2="INSERT INTO itemspoinfo SET PODate=?, PONo=?, ItemName=?, Qty=?,"; 
      pst2=conn.prepareStatement(sql2); 
      pst2.setTimestamp(1, timestamp); 
      pst2.setString(2, porderno); 



      %> 
      <script language="javascript"> 
       alert("Sucessfuly Sent to "+scom); 
      </script> 
      <%  
     } 
    } 
%> 

enter image description here

+2

不要在jsp頁面中使用java代碼 – SpringLearner

+1

您正在使用'update'語句或'insert'語句嗎? – Srinu

+2

您可能想了解[Hibernate(ORM)](https://en.wikipedia.org/wiki/Hibernate_(Java)) –

回答

2

您可以使用addBatch()executeBatch()

試試下面的代碼:

String sql2="INSERT INTO itemspoinfo (PODate, PONo, ItemName,Qty) VALUES(?,?,?,?)"; 
     pst2=conn.prepareStatement(sql2); 
     // to check drugs details are added or not. 
     int noofdrugs = 0; 
     if (drug1!=null && drug1.length()>0) 
      && (qty1!=null && qty1.length()>0)) { 
      pst2.setTimestamp(1, timestamp); 
      pst2.setString(2, porderno); 
      pst2.setString(3, drug1); 
      pst2.setInt(4, Integer.parseInt(qty1)); 
      pst2.addBatch(); 

      noofdrugs++; 
     } 

     if (drug2!=null && drug2.length()>0) 
      && (qty2!=null && qty2.length()>0)) 
     { 
      pst2.setTimestamp(1, timestamp); 
      pst2.setString(2, porderno); 
      pst2.setString(3, drug2); 
      pst2.setInt(4, Integer.parseInt(qty2)); 
      pst2.addBatch(); 

      noofdrugs++; 
     } 

     if (drug3!=null && drug3.length()>0) 
      && (qty3!=null && qty3.length()>0)) 
     { 
      pst2.setTimestamp(1, timestamp); 
      pst2.setString(2, porderno); 
      pst2.setString(3, drug3); 
      pst2.setInt(4, Integer.parseInt(qty3)); 
      pst2.addBatch(); 

      noofdrugs++; 
     } 

     if (drug4!=null && drug4.length()>0) 
      && (qty4!=null && qty4.length()>0)) 
     { 
      pst2.setTimestamp(1, timestamp); 
      pst2.setString(2, porderno); 
      pst2.setString(3, drug4); 
      pst2.setInt(4, Integer.parseInt(qty4)); 
      pst2.addBatch(); 

      noofdrugs++; 
     } 

     if (drug5!=null && drug5.length()>0) 
      && (qty5!=null && qty5.length()>0)) 
     { 
      pst2.setTimestamp(1, timestamp); 
      pst2.setString(2, porderno); 
      pst2.setString(3, drug5); 
      pst2.setInt(4, Integer.parseInt(qty5)); 
      pst2.addBatch(); 

      noofdrugs++; 
     } 

     if (noofdrugs>0) { 
      pstmt.executeBatch(); 
     } 
1

先寫一個單獨的機能的研究將項目插入到數據庫中。

void insertItemReord(timestamp, porderno, drug, qty) 
    { 
     // SQL STUFF 
    } 

然後適當地調用它。

 if((scom!=null && scom.length()>0) 
      && (porderno!=null && porderno.length()>0) 
      && (bdate!=null && bdate.length()>0) 
     { 
      pst.execute(); 

      if (drug1!=null && drug1.length()>0) 
       && (qty1!=null && qty1.length()>0)) 
      { 
       insertItemReord(timestamp, porderno, drug1, qty1); 
      } 

      if (drug2!=null && drug2.length()>0) 
       && (qty2!=null && qty2.length()>0)) 
      { 
       insertItemReord(timestamp, porderno, drug2, qty2); 
      } 

      if (drug3!=null && drug3.length()>0) 
       && (qty3!=null && qty3.length()>0)) 
      { 
       insertItemReord(timestamp, porderno, drug3, qty3); 
      } 

      if (drug4!=null && drug4.length()>0) 
       && (qty4!=null && qty4.length()>0)) 
      { 
       insertItemReord(timestamp, porderno, drug4, qty4); 
      } 

      if (drug5!=null && drug5.length()>0) 
       && (qty5!=null && qty5.length()>0)) 
      { 
       insertItemReord(timestamp, porderno, drug5, qty5); 
      } 

       %> 
       <script language="javascript"> 
        alert("Sucessfuly Sent to "+scom); 
       </script> 
       <%  
     } 
1

創建了一個叫做Utility,然後寫在程序包中的DbConnection代碼。

public class DBconnection { 

public static Connection getConnection() {// set the db connection 
    boolean status = false; 
    Connection conn = null; 
    PreparedStatement pst = null; 
    ResultSet rs = null; 

    String url = "jdbc:mysql://localhost:3306/"; 
    String dbName = "login"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "root"; 
    String password = ""; 

    try { 
     Class.forName(driver).newInstance(); 
     conn = DriverManager.getConnection(url + dbName, userName, password);   

    } catch (Exception e){ 
     System.out.println(e); 
    } finally { 
     if (conn != null) { 
      try { 

      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (pst != null) { 
      try { 

      } catch (SQLException e) { 
      e.printStackTrace(); 
      } 
     } 
     if (rs != null) { 
      try { 

      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return conn; 
} 

}

然後創建一個新的Java文件寫在另一個包查詢。

public class Registration { 
// new user registration method 

public static boolean register(String firstname,String lastname,String email,String username,String password) { 
    Connection conn = null; 
    PreparedStatement pst1, pst2; 
    int rs1, rs2; 

    boolean status = false; 
    try { 
     DBconnection dbops = new DBconnection(); 
     conn = dbops.getConnection();//create connection 
     System.out.println("--getconnection-->new user"); 
     //inert values to the register table 
     pst1 = conn.prepareStatement("INSERT INTO register(firstname,lastname,email,username,password) values(?,?,?,?,?)"); 
     pst1.setString(1, firstname); 
     pst1.setString(2, lastname); 
     pst1.setString(3, email); 
     pst1.setString(4, username); 
     pst1.setString(5, password); 
     rs1 = pst1.executeUpdate(); 

     System.out.println("--Execute Query-->new user"); 
     //System.out.println(rs1); 
     if (rs1 == 1) { 
      //insert values to users table 
      pst2 = conn.prepareStatement("INSERT INTO users(username,password) values(?,?)"); 
      pst2.setString(1, username); 
      pst2.setString(2, password); 

      rs2 = pst2.executeUpdate(); 
      if (rs2 == 1) { 
       System.out.println("inserted succesfully"); 
       status = true; 
      } else { 
       System.out.println("not insert to users table - something went wrong"); 
      } 
     } else { 
      System.out.println("not insert to register table - username error"); 
      status = false; 
     } 

    }catch(SQLException se) { 
     //Handle errors for JDBC 
     se.printStackTrace(); 
    } catch (Exception e) { 
     //Handle errors for Class.forName 
     e.printStackTrace(); 
    }finally { 
     try { 
      conn.close(); 
     } catch (Exception e) { 
     } 
    } 
    return status; 
} 

這個函數被一個servlet調用並返回一個布爾值。

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 

    String firstname = request.getParameter("firstname"); 
    String lastname = request.getParameter("lastname"); 
    String email = request.getParameter("email"); 
    String username = request.getParameter("username"); 
    String password = request.getParameter("password"); 

    HttpSession session = request.getSession(false); 

    if (Registration.register(firstname,lastname,email,username,password)) { 
     out.print("<p style=\"color:red\">Account Created</p>"); 
     RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); 
     rd.forward(request, response); 
    } else { 
     out.print("<p style=\"color:red\">Error Occured In User Registration</p>"); 
     RequestDispatcher rd = request.getRequestDispatcher("newuser.jsp"); 
     rd.include(request,response); 
    } 

jsp是視圖,servelet是控制器,java文件是模型。 我想你明白了。