Je veux savoir comment je peux insérer des données en vrac à la fois à la base de données MySQL Je ne sais pas comment le faire.Insérer les données de la table lorsque vous cliquez sur soumettre le bouton selon mvc.Java: Insérer plusieurs lignes dans MySQL
farmer id
et name
sont venus d'une autre table, je veux ajouter toutes les données dans une autre table, farmer id
et name
sont en lecture seule
milkload page jsp
<form action="MilkloadAdd" method="post">
<table id="example" class="display" width="100%" cellspacing="0">
<%
List<Farmer> farmer = (List<Farmer>)
request.getAttribute("Farmer_list");
for (Farmer emp1 : farmer) {
%>
<tr>
<td> <input type="text" name="f_id" value="<%=emp1.getfId()%>" readonly="readonly" required/></td>
<td> <input type="text" name="fname" value="<%=emp1.getfFname()%>" readonly="readonly"required/></td>
<td><input type="text" name="lactometer" value="" required onkeyup="snf();"/></td>
<td><input type="text" name="fatvalue" value="" required onkeyup="snf();"/></td>
<td><input type="text" name="weights" value=""required/> </td>
<td><input type="text" name="snf" value=""required readonly="readonly"/></td>
</tr>
<% } %>
milkload modèle classe
package com.nestle.model;
public class Milkload {
public String getSnf() {return snf;}
public void setSnf(String snf) {this.snf = snf;}
public String getFid() {return fid;}
public void setFid(String fid) {this.fid = fid;}
public String getLactometer() {return lactometer;}
public void setLactometer(String lactometer) {this.lactometer = lactometer;}
public String getFatvalue() {return fatvalue;}
public void setFatvalue(String fatvalue) {this.fatvalue = fatvalue;}
public String getWeights() {return weights;}
public void setWeights(String weights) {this.weights = weights;}
public String getDate() {return date;}
public void setDate(String date) {this.date = date;}
public String getCpid() {return cpid;}
public void setCpid(String cpid) {this.cpid = cpid;}
public String getMilkindex() {return milkindex;}
public void setMilkindex(String milkindex) {this.milkindex = milkindex;}
private String fid;
private String lactometer;
private String fatvalue;
private String weights;
private String date;
private String cpid;
private String milkindex;
private String snf;
}
MilkLoadDAO Classe
package com.nestle.db;
public class MilkloadDAO {
private static final String SQL_INSERT_MILKLOAD="INSERT INTO
milk_load(Date,Cp_Id,F_Id,Fat,Weight,Lactormeter) VALUES (?,?,?,?,?,?)";
public static void insertMilkLoad(Milkload milkload) {
try (Connection connection = DbConnect.getConnection()) {
PreparedStatement statement =
connection.prepareStatement(SQL_INSERT_MILKLOAD);
statement.setString(1, milkload.getDate());
statement.setString(2, milkload.getCpid());
statement.setString(3, milkload.getFid());
statement.setString(4, milkload.getFatvalue());
statement.setString(5, milkload.getWeights());
statement.setString(6, milkload.getLactometer());
System.out.println(SQL_INSERT_MILKLOAD);
statement.execute();
} catch (Exception e) {
System.out.println("MILKLOAD DAO error"+ e.getMessage());
}
}
}
de servlet milkload
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String fid = request.getParameter("f_id");
String lactometer = request.getParameter("lactometer");
String fatvalue = request.getParameter("fatvalue");
String weights = request.getParameter("weights");
String date = request.getParameter("Date");
String Cp_Id = "1";
Milkload milkload =new Milkload();
milkload.setCpid(Cp_Id);
milkload.setDate(date);
milkload.setFatvalue(fatvalue);
milkload.setFid(fid);
milkload.setLactometer(lactometer);
milkload.setWeights(weights);
MilkloadDAO.insertMilkLoad(milkload);
}
Vous voulez ajouter farmerid et le nom avec les autres détails dans le formulaire? –
oui dans une autre table appelée milkoad .vous pouvez voir cette table dans MilkLoadDAO Classe –
Désolé requête est privé statique final Chaîne SQL_INSERT_MILKLOAD = "INSÉRER dans la charge de lait (Date, Cp_Id, F_Id, Fat, Poids, Lactormeter) VALEURS (?,?,? ,?,?,?) "; –