即使我使用了inputstream,也没有为参数2指定值 并使用了setBlob方法- 即使我使用了inputstream,也没有为参数2指定值 和使用setBlob方法- 即使我使用了inputstream,也没有为参数2指定值 并使用了setBlob方法---
这是我的jsp代码:
<form method="post" enctype="multipart/form-data" action="insertbooks">
<p>Title<p>
<input type="text" style="width:300px;height:25px" name="title" placeholder="Type Book Title"/>
<p>Insert image</p>
<input name="image" type="file">
<p>Author</p>
<input type="text" style="width:300px;height:25px" name="author" placeholder="Author Name"/>
<p>Price</p>
<input type="text" style="width:300px;height:25px" name="price" placeholder="Enter price"/>
<br>
<br><br>
<input style="width:100px;height:35px;font-size:1.3em" type="submit" name="submitb" value="submit" />
</form>
这是我的insertbooks.java代码:
public class insertbooks extends HttpServlet {
private String dbURL = "jdbc:mysql://localhost:3306/bookecom";
private String dbUser = "root";
private String dbPass = "";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// for error handling in file
File file=new File("D://Study//programming//servlet//bookecom//insertbookserror.txt");
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
PrintWriter out = response.getWriter();
//retrieving image
InputStream inputStream = null; // input stream of the upload file
String Title = request.getParameter("title");
// obtains the upload file part in this multipart request
Part filePart = request.getPart("image");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
pw.println("Got photooo");
}
// System.out.println(" filename: "+filePart.getName());
// pw.println(filePart.getName());
String Author = request.getParameter("author");
String Pricef = request.getParameter("price");
//float Pricef = Float.valueOf(Price);
pw.println("Got parameters");
System.out.println("price value"+Pricef);
Connection conn = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
pw.println("Driver Loaded");
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
pw.println("Connection Established_1");
// String query = "Insert into books(title,image,author,price) values('"+Title +"','"+inputStream +"','"+Author+"','"+Pricef +"')" ;
String query = "Insert into books(title,image,author,price) values(?,?,?,?)" ;
PreparedStatement ps =conn.prepareStatement(query);
ps.setString(1,Title);
if(inputStream != null){
// fetches input stream of the upload file for the blob column
ps.setBlob(2, inputStream);
} else{ pw.println(" blob data not available"); }
ps.setString(3,Author);
ps.setString(4, Pricef);
// Sends the statement to the database server
int row= ps.executeUpdate();
if(row>0){
pw.println("data inserted");
}
} catch (SQLException ex) {
ex.getMessage();
// ex.printStackTrace();
ex.printStackTrace(pw);
pw.println("Connection not Established_1");
} catch (ClassNotFoundException ex) {
Logger.getLogger(insertbooks.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
System.out.println("Successfully");
// sets the message in request scope
// request.setAttribute("message", message);
// forwards to the message page
// getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
}
pw.close();
}
}
这些是错误的:
java.sql.SQLException: No value specified for parameter 2
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1132)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1057)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1377)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1042)
at insertbooks.doPost(insertbooks.java:90)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
答案 0 :(得分:0)
如果您确实需要将图像存储在数据库中,请尝试将所有代码放在if语句之后,询问if中的filePart是否为null,如下所示:
if (filePart != null) {
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
pw.println("Got photooo");
//PUT ALL THE CODE AFTER THIS IF STATEMENT HERE
}