НЕТ ЗНАЧЕНИЯ, заданного для параметра 2, даже если я использовал inputtream и использовал метод setBlob --- НЕТ ЗНАЧЕНИЯ, заданного для параметра 2, даже если я использовал inputsream и метод setBlob - НЕТ ЗНАЧЕНИЯ, указанного для параметра 2, даже если я использовал inputsream ииспользуется метод 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)