Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用jsp将多个文件同时上载到mysql数据库_Java_Jdbc - Fatal编程技术网

Java 无法使用jsp将多个文件同时上载到mysql数据库

Java 无法使用jsp将多个文件同时上载到mysql数据库,java,jdbc,Java,Jdbc,我试图在MySQL数据库中插入多条记录,但我的代码只在MySQL数据库中插入一条记录,即使我在一个循环中准备了多条语句。请帮帮我,我哪里做错了。我从两周前开始寻找这个答案 这是我的密码。。上传ServletClass.java String firstName = request.getParameter("firstname"); String lastName = request.getParameter("lastname"); String fileName = ""; String p

我试图在MySQL数据库中插入多条记录,但我的代码只在MySQL数据库中插入一条记录,即使我在一个循环中准备了多条语句。请帮帮我,我哪里做错了。我从两周前开始寻找这个答案

这是我的密码。。上传ServletClass.java

String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
String fileName = "";
String path = folderName + File.separator + fileName;

con = DB.getConnection();

List < Part > fileParts = request.getParts().stream().filter(part - > "file".equals(part.getName())).collect(Collectors.toList());

for (Part filePart: fileParts) {
    fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
    filePart.write(uploadPath + File.separator + fileName);
    InputStream ins = filePart.getInputStream();
    String sql = "insert into newfiles(firstname,lastname,filename,path) values(?,?,?,?)"; //inserting all values into database
    ps = con.prepareStatement(sql);
    Files.copy(ins, Paths.get(uploadPath + File.separator + fileName), StandardCopyOption.REPLACE_EXISTING);
}

ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, fileName);
ps.setString(4, path);
int status = ps.executeUpdate();

if (status > 0) {
    System.out.println("File Uploaded Successfully");
    System.out.println("Uploaded Path:" + uploadPath);
}
stringfirstname=request.getParameter(“firstName”);
字符串lastName=request.getParameter(“lastName”);
字符串fileName=“”;
字符串路径=folderName+File.separator+文件名;
con=DB.getConnection();
ListfileParts=request.getParts().stream().filter(Part->“file”.equals(Part.getName()).collect(Collectors.toList());
对于(部分文件部分:文件部分){
fileName=path.get(filePart.getSubmittedFileName()).getFileName().toString();
write(uploadPath+File.separator+fileName);
InputStream ins=filePart.getInputStream();
String sql=“insert into newfiles(firstname、lastname、filename、path)值(?,?,?)”;//将所有值插入数据库
ps=con.prepareStatement(sql);
Files.copy(ins,path.get(uploadPath+File.separator+fileName),StandardCopyOption.REPLACE_EXISTING);
}
ps.setString(1,名字);
ps.setString(2,姓氏);
ps.setString(3,文件名);
ps.setString(4,路径);
int status=ps.executeUpdate();
如果(状态>0){
System.out.println(“文件上传成功”);
System.out.println(“上传路径:+上传路径”);
}
请输入以下代码:

ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, fileName);
ps.setString(4, path);
int status = ps.executeUpdate();

if (status > 0) {
    System.out.println("File Uploaded Successfully");
    System.out.println("Uploaded Path:" + uploadPath);
}

在for循环中。

请有人帮忙……这不是JSP/Servlet文件上载问题。它工作得非常好。问题在于如何创建和执行SQL语句。您创建了多条语句,但只执行最后一条。我减少了代码中的噪音,使您的问题更加集中。你应该在下一个问题中自己回答。@BalusC谢谢你纠正我。但我不明白我的代码哪里错了。谢谢你。问题解决了。我还将我的“insert”语句移到List语句之后。:)