部分和字符串上的循环获取重复的JSP/JEE
我有一个输入表单,其中包含添加多个输入类型文本和类型文件的功能,我正在尝试获取所有值并将它们插入到我的数据库中。 你们能告诉我我的代码复制数据库中的所有值有什么问题吗部分和字符串上的循环获取重复的JSP/JEE,jsp,servlets,jakarta-ee,Jsp,Servlets,Jakarta Ee,我有一个输入表单,其中包含添加多个输入类型文本和类型文件的功能,我正在尝试获取所有值并将它们插入到我的数据库中。 你们能告诉我我的代码复制数据库中的所有值有什么问题吗 String [] inst=request.getParameterValues("inst"); List<Part> fileParts = request.getParts().stream() .filter(part -> "picture".equals(part.getName()))
String [] inst=request.getParameterValues("inst");
List<Part> fileParts = request.getParts().stream()
.filter(part -> "picture".equals(part.getName()))
.collect(Collectors.toList()); // Retrieves
<input type="file" name="file" multiple="true">
for (int i=0; i< inst.length; i++ ) {
/* Part filePart2=request.getPart("picture");
InputStream photo2 = null;
photo2=filePart2.getInputStream();*/
try {
// connects to the database
//Get all the parts from request and write it to the file on server
for (Part filePartt : fileParts) {
// String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
InputStream fileContent = filePartt.getInputStream();
// ... (do your job here)
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");
PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
pr.setString(1, inst[i]);
System.out.println("numero "+i);
if (fileContent != null) {
// fetches input stream of the upload file for the blob column
pr.setBlob(2, fileContent);
}
pr.setInt(3, idsub);
pr.executeUpdate();
}
} catch(Exception e){}
}
String[]inst=request.getParameterValues(“inst”);
List fileParts=request.getParts().stream()
.filter(part->“picture”.equals(part.getName()))
.collect(Collectors.toList());//检索
对于(int i=0;i
您有两个嵌套for循环。第一个从inst数组开始运行整个长度,并为其中的每一个插入所有零件。因此,如果inst的长度为3,而你有3个部分,那么每次外部循环变为+1时,你将插入3个部分。如果您知道列表中的顺序是正确的,您可以执行以下操作:
// ... (do your job here)
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");
for(int i = 0; i < fileParts.size(); i++) {
Part p = fileParts.get(i);
InputStream fileContent = p.getInputStream();
PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
pr.setString(1, String.valueOf(i));
System.out.println("numero "+i);
if (fileContent != null) {
// fetches input stream of the upload file for the blob column
pr.setBlob(2, fileContent);
}
// where idsub comes from??
pr.setInt(3, idsub);
pr.executeUpdate();
}
/。。。(在这里做你的工作)
Class.forName(“com.mysql.jdbc.Driver”);
java.sql.Connection cnn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/digitalrecipe“,”根“,”);
对于(int i=0;i
我试着对您的代码没有太多更改,但在for循环中启动连接似乎不正确:)我设法修复了它`String[]inst=request.getParameterValues(“inst”)
List fileParts=request.getParts().stream().filter(part->“picture”.equals(part.getName()).collect(Collectors.toList());//检索
Class.forName(“com.mysql.jdbc.Driver”);
java.sql.Connection cnn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/digitalrecipe“,”根“,”);
int i=0;
试一试{
//连接到数据库
//从请求中获取所有部分并将其写入服务器上的文件
对于(部分文件部分T:fileParts){
//字符串文件名=路径.get(filePart.getSubmittedFileName()).getFileName().toString();//MSIE修复。
InputStream fileContent=filePartt.getInputStream();
//…(在这里做你的工作)
PreparedStatement pr=(PreparedStatement)cnn.prepareStatement(“插入指令(仪表、照片、idsubc)值(?,?)”;
如果(i
任何编写}catch(例外e){}
的人都应该受到鞭打。谁在一块JDBC/SQL语句后编写它,有九条尾巴。更可取的方法是对多次插入使用批处理语句。您可以在循环中添加addBatch(),在循环中添加executeBatch(),在循环外部直接添加executeBatch()
List<Part> fileParts = request.getParts().stream().filter(part -> "picture".equals(part.getName())).collect(Collectors.toList()); // Retrieves <input type="file" name="file" multiple="true">
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");
int i=0;
try {
// connects to the database
//Get all the parts from request and write it to the file on server
for (Part filePartt : fileParts) {
// String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
InputStream fileContent = filePartt.getInputStream();
// ... (do your job here)
PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
if (i< inst.length){
pr.setString(1, inst[i]);
System.out.println("numero "+i);
}
i++;
if (fileContent != null) {
// fetches input stream of the upload file for the blob column
pr.setBlob(2, fileContent);
}
pr.setInt(3, idsub);
pr.executeUpdate();
}
}
catch(Exception e){}`