0
我是新來jsp.I插入團塊圖像數據庫有一個表名爲employee
現在我想插入圖片使用一個名稱,如datatase request
parameter.The表已經有name
,age
, id
,phonenumber
,email
password
作爲fields.image是我現在創建的新字段。當更新圖像字段與新的blob圖像時,它不顯示錯誤,但圖像沒有插入。這裏是我的嘗試:使用文件上傳在HTML/JSP
// My form to fileupload
<form name="frm" action="Image.jsp" method="post">
<center>Name:<input type="text" name="name"><br></center><br>
<center><br><br> <input type="submit" value="Submit"></center>
</form>
//(Image.jsp)My pgogram to get file from client and store in database
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<html>
<body>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/praveen";
PreparedStatement pstatement = null;
FileInputStream inputStream = null;
int insertQuery = 0;
byte[] b = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "bulbultara");
String sql = "update employee set image=? where name=? ";
pstatement = connection.prepareStatement(sql);
pstatement.setString(2, request.getParameter("name"));
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List items = sfu.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
b = item.get();
}
}
pstatement.setBytes(1, b);
insertQuery = pstatement.executeUpdate();
pstatement.close();
connection.close();
%>
<a href="Login.jsp"><input type="submit" value="Login"></a>
</body>
</html>
你知道'servlet'和'hibernate'。閱讀關於他們的東西,他們會讓你更輕鬆一些嗎? – KNU
你可能想看看它:http://www.codejava.net/coding/upload-files-to-database-servlet-jsp-mysql – KNU